-
Notifications
You must be signed in to change notification settings - Fork 3
/
object.cpp
136 lines (123 loc) · 2.27 KB
/
object.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "object.h"
Object::Object(Type type)
: type(type)
{
spd.x = 0;
spd.y = 0;
rem.x = 0;
rem.y = 0;
flip.x = false;
flip.y = false;
}
Object::~Object()
{
}
void Object::setPosition(int new_x, int new_y)
{
x = new_x;
y = new_y;
}
void Object::init()
{
}
void Object::update()
{
move(spd.x, spd.y);
}
void Object::draw()
{
drawSprite(spr, x, y, flip.x, flip.y);
}
bool Object::is_solid(int ox, int oy)
{
if (oy > 0 && !check(platform, ox, 0) && check(platform, ox, oy))
{
return true;
}
return solid_at(x + hitbox.x + ox, y + hitbox.y + oy, hitbox.w, hitbox.h)
|| check(fall_floor, ox, oy)
|| check(fake_wall, ox, oy);
}
bool Object::is_ice(int ox, int oy)
{
return ice_at(x + hitbox.x + ox, y + hitbox.y + oy, hitbox.w, hitbox.h);
}
Object * Object::collide(Type type, int ox, int oy)
{
for (int i = 0; i < objects_count; i++)
{
Object * other = objects[i];
if (other && other->type == type && other != this && other->collideable
&& other->x + other->hitbox.x + other->hitbox.w > x + hitbox.x + ox
&& other->y + other->hitbox.y + other->hitbox.h > y + hitbox.y + oy
&& other->x + other->hitbox.x < x + hitbox.x + hitbox.w + ox
&& other->y + other->hitbox.y < y + hitbox.y + hitbox.h + oy)
{
return other;
}
}
return NULL;
}
bool Object::check(Type type, int ox, int oy)
{
return collide(type, ox, oy) != NULL;
}
void Object::move(float ox, float oy)
{
rem.x += ox;
float amount = floor(rem.x + 0.5f);
rem.x -= amount;
move_x(amount, 0);
rem.y += oy;
amount = floor(rem.y + 0.5f);
rem.y -= amount;
move_y(amount);
}
void Object::move_x(float amount, float start)
{
if (solids)
{
int step = sign(amount);
for (int i = start; i <= abs(amount); i++)
{
if (!is_solid(step, 0))
{
x += step;
}
else
{
spd.x = 0;
rem.x = 0;
break;
}
}
}
else
{
x += amount;
}
}
void Object::move_y(float amount)
{
if (solids)
{
int step = sign(amount);
for (int i = 0; i <= abs(amount); i++)
{
if (!is_solid(0, step))
{
y += step;
}
else
{
spd.y = 0;
rem.y = 0;
break;
}
}
}
else
{
y += amount;
}
}