void sys_draw_thrusters::update(entity &e) {
   sys::update(e);
   
   auto &c = e.C(com_control);
   auto &b = e.C(com_box);
      if (c.bl) {
         pool::get()
            .draw("thruster" + to_string(cur_frame) + ".png")
            .pos(b.pos + rotate(vec2(-b.size.w / 2, -b.size.h / 2) + vec2(10, 10), b.angle))
            .index(4)
            .rotation(b.angle + M_PI);
      }
      if (c.br) {
         pool::get()
         .draw("thruster" + to_string(cur_frame) + ".png")
            .pos(b.pos + rotate(vec2(b.size.w / 2, -b.size.h / 2) + vec2(-10, 10), b.angle))
            .index(4)
            .rotation(b.angle + M_PI);
      }
      if (c.tl) {
         pool::get()
         .draw("thruster" + to_string(cur_frame) + ".png")
            .pos(b.pos + rotate(vec2(-b.size.w / 2, b.size.h / 2) + vec2(13, -40), b.angle))
            .index(4)
            .rotation(b.angle);
      }
      if (c.tr) {
         pool::get()
         .draw("thruster" + to_string(cur_frame) + ".png")
            .pos(b.pos + rotate(vec2(b.size.w / 2, b.size.h / 2) + vec2(-13, -40), b.angle))
            .index(4)
            .rotation(b.angle);
      }
}
void sys_beam_laser::update(entity &e) {
   if (e.C(com_laser).is_beaming) {
      auto &b = e.C(com_box);
      vec2 laser_size = pool::get().texture_size("laser.png");
      // audio_engine::play_sound("lasersound.wav");

      for (int i = 0; i < 30; ++i) {
         vec2 pos = b.pos + rotate(vec2(0, laser_size.y * i), b.angle);
         com_box cur_box(pos, laser_size);
         if (hitting_boundary(cur_box)) break;

         pool::get()
            .draw("laser" + to_string(cur_frame) + ".png")
            .pos(pos)
            .rotation(b.angle)
            .index(4);

         bool done = false;
         vector<entity *> surrounding = w->get_entities("enemy");
         for (entity *s : surrounding) {
            if (s->C(com_box).collides_box(cur_box)) {
               s->to_delete = true;
               done = true;
            }
         }
         if (done) {
            e.C(com_laser).is_beaming = false;
            break;
         }
      }
   }
}
void sys_orbit_weapon::update(entity &e) {
   auto &w = e.C(com_orbit_weapon);
   auto &b = e.C(com_box);

   vec2 diff = w.pos - b.pos;
   w.pos.x = cos(0.01) * diff.x - sin(0.01) * diff.y + b.pos.x;
   w.pos.y = sin(0.01) * diff.x + cos(0.01) * diff.y + b.pos.y;
   // cout << w.pos << endl;
   pool::get().draw("moon.png").pos(w.pos);
   // auto &b = e.C(com_box);
   // pool::get().draw("radius.png").pos(b.pos);
}
void sys_control_movement::update(entity &e) {
   auto &m = e.C(com_movement);
   auto &c = e.C(com_control);
   auto &b = e.C(com_box);
   auto &g = e.C(com_gravity);

   // control com_movement
   float speed = 0.2;
   float save_speed = 0;
   const float backwards = 0.6;
      if (save_speed != 0) {
         speed = save_speed;
         save_speed = 0;
      }
      if (g.in_gravity) speed *= 2.5;

      if (c.bl) {
         m.vel.x += sin(b.angle + M_PI) * speed;
         m.vel.y += cos(b.angle) * speed;
      }
      if (c.br) {
         m.vel.x += sin(b.angle + M_PI) * speed;
         m.vel.y += cos(b.angle) * speed;
      }
      if (c.tl) {
         m.vel.x -= sin(b.angle + M_PI) * speed * backwards;
         m.vel.y -= cos(b.angle) * speed * backwards;
      }
      if (c.tr) {
         m.vel.x -= sin(b.angle + M_PI) * speed * backwards;
         m.vel.y -= cos(b.angle) * speed * backwards;
      }

      if (g.in_gravity) {
         m.friction = 0.9;
      } else {
         if (!c.bl && !c.br && !c.tl && !c.tr) {
            m.friction = 0.92f;
         } else {
            m.friction = 1.0f;
         }
         float max_speed = 5.0f;
         if ((c.tl && c.tr) || (c.br && c.bl)) max_speed *= 1.4;
         if (c.tl || c.tr) max_speed *= backwards;

         float speed = vdistance(vec2(0), m.vel);
         float boost_speed = vdistance(vec2(0), g.boost_vel);
         if (boost_speed > max_speed) max_speed = boost_speed;
         if (speed > max_speed) m.vel = normalize(m.vel) * max_speed;
         g.boost_vel = normalize(g.boost_vel) * (boost_speed - 0.22);
      }
   }
void sys_control_rotation::update(entity &e) {
   auto &m = e.C(com_movement);
   auto &c = e.C(com_control);

   // control rotation
   const int invert_roation = 1; // -1 for invert
   const float aspeed = 0.006 * invert_roation;

      if (c.bl) m.angular_velocity += aspeed;
      if (c.br) m.angular_velocity -= aspeed;
      if (c.tl) m.angular_velocity -= aspeed;
      if (c.tr) m.angular_velocity += aspeed;

      if ((c.br && c.bl) || (c.tl && c.tr)) {
         if (abs(m.angular_velocity) < 0.00001f) {
            m.angular_velocity = 0;
         } else {
            if (m.angular_velocity < 0) {
               m.angular_velocity += aspeed;
               if (m.angular_velocity > 0) m.angular_velocity = 0;
            } else {
               m.angular_velocity -= aspeed;
               if (m.angular_velocity < 0) m.angular_velocity = 0;
            }
         }
      } else {
         if (!c.bl && !c.br && !c.tl && !c.tr) {
            m.angular_friction = 0.8f;
         } else {
            m.angular_friction = 1.0f;
         }

         float max_angular_speed = 0.05;
         if ((c.tl && c.br) || (c.tr && c.bl)) max_angular_speed *= 1.4;
         if (m.angular_velocity >= max_angular_speed) {
            m.angular_velocity = max_angular_speed;
         } else if (m.angular_velocity <= -max_angular_speed) {
            m.angular_velocity = -max_angular_speed;
         }
      }
}
void sys_orbit_weapon::init(entity &e) {
   auto &w = e.C(com_orbit_weapon);
   auto &b = e.C(com_box);

   w.pos = b.pos + 50;
}
void sys_homing_missiles::update(entity &e) {
   auto &b = e.C(com_box);
   radius_angle -= 0.05;
   pool::get().draw("radius2.png").pos(b.pos).rotation(radius_angle).alpha(0.0);
}