entity.cxx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // entity.cxx
  3. // engine
  4. //
  5. // Created by Sam Jaffe on 5/20/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include "game/engine/entity.hpp"
  9. #include <json/json.h>
  10. #include "game/engine/serial.hpp"
  11. #include "game/engine/time.hpp"
  12. #include "game/graphics/material.hpp"
  13. using namespace engine;
  14. static unsigned int next_id() {
  15. static unsigned int id{0};
  16. return ++id;
  17. }
  18. entity::entity(Json::Value const & json)
  19. : identity<entity>(next_id()), in_scene(), last_position({-1, -1}),
  20. velocity(to_vec2(json["velocity"])), acceleration(),
  21. angular_velocity(0.f), render_info(to_object(json)), frame_index(0),
  22. frame_texture_coords({make_vector(0.f, 0.f)}),
  23. scale(json["size"].asFloat()), collides_with(0), collides_as(0) {
  24. render_info.location.size *= scale;
  25. render_info.frame.origin = frame_texture_coords[frame_index];
  26. }
  27. void entity::update(tick const & tk) {
  28. last_position = render_info.location.origin;
  29. float delta = tk.since.count();
  30. auto delta_pos = velocity * delta;
  31. render_info.location.origin += delta_pos;
  32. render_info.points.ll += delta_pos;
  33. render_info.points.lr += delta_pos;
  34. render_info.points.ur += delta_pos;
  35. render_info.points.ul += delta_pos;
  36. velocity += acceleration * delta;
  37. // render_info.angle += angular_velocity * delta;
  38. }