]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/entity.cpp
Some structure Changes
[dragonblocks-bedrock.git] / src / entity.cpp
1 #include <string>
2 #include <math.h>
3 #include "game.h"
4 #include "entity.h"
5 #include "texture.h"
6 #include "graphics.h"
7 #include <iostream>
8 using namespace std;
9 Entity *Entity::list[MAX_ENTITIES];
10 double Entity::t = 0;
11 int Entity::count;
12 Entity::Entity(){
13         if(Entity::count < MAX_ENTITIES){
14                 Entity::list[Entity::count] = this;
15                 Entity::count++;
16         }
17         else 
18                 for(int i = 0; i < MAX_ENTITIES; i++)
19                         if(Entity::list[i] == NULL)
20                                 Entity::list[i] = this;
21 }
22 void Entity::physics_all(){
23         for(int i = 0; i < count; i++)
24                 if(list[i] == NULL)
25                         continue;
26                 else
27                         list[i]->physics();
28 }
29 void Entity::spawn(double sx, double sy, bool gravity){
30         x = sx;
31         y = sy;
32         ax = 0;
33         ay = 0;
34         if(gravity)
35                 ay = GRAVITY;
36         physics_reset_x();
37         physics_reset_y();
38         Game::log((string)"Spawning entity '" + name + "' at (" + to_string(x) + ", " + to_string(y) + ")", INFO);
39 }
40 bool Entity::physics_check_x(){
41         if(x < 0)
42                 return false;
43         if(x + width > MAPWIDTH)
44                 return false;
45         return physics_check_both();
46 }
47 bool Entity::physics_check_y(){
48         if(y < 0)
49                 return false;
50         if(y + height > MAPHEIGHT)
51                 return false;
52         return physics_check_both();
53 }
54 bool Entity::physics_check_both(){
55         for(int ix = floor(x); ix <= ceil(x + width - 0.01) - 1; ix++)
56                 for(int iy = floor(y); iy <= ceil(y + height - 0.01) - 1; iy++)
57                         if(Game::map->getNode(ix, iy)->stable)
58                                 return false;
59         return true;
60 }
61 void Entity::physics_reset_x(){
62         vx = 0;
63         tx0 = Entity::t;
64         x0 = x;
65         x = (double)(round(x * 10)) / 10;
66 }
67 void Entity::physics_reset_y(){
68         vy = 0;
69         ty0 = Entity::t;
70         y0 = y;
71         y = (double)(round(y * 10)) / 10;
72 }
73 void Entity::physics(){
74         double old_x = x;
75         double dtx = Entity::t - tx0;
76         if(ax)
77                 x = ax * dtx * dtx / 2 + vx * dtx + x0;
78         else if(vx)
79                 x = vx * dtx + x0;
80         if(!physics_check_x())
81         {
82                 x = old_x;
83                 physics_reset_x();
84         }
85         double old_y = y;
86         double dty = Entity::t - ty0;
87         if(ay)
88                 y = ay * dty * dty / 2 + vy * dty + y0;
89         else if(vy)
90                 y = vy * dty + y0;
91         if(!physics_check_y())
92         {
93                 y = old_y;
94                 physics_reset_y();
95         }
96 }