-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameobjects.cpp
153 lines (109 loc) · 3.85 KB
/
gameobjects.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "gameobjects.h"
GameObject::GameObject(int x, int y, int width, int height, int objID)
{
objectID = objID;
Position.X = x;
Position.Y = y;
Position.Z = 0;
Velocity.X = 0;
Velocity.Y = 0;
Shape = 0;
Size = 0;
Width = width;
HalfWidth = Width / (double)2;
Height = height;
Acceleration.X = 0;
Acceleration.Y = 0;
Drag = 0.3;
ScrollX = 0;
ScrollY = 0;
frameCounter = 0;
// Default Blank Animation
currentAnimation = new Animation(1, 1);
currentAnimation->Frames[0] = 128;
Draw();
}
void GameObject::Draw()
{
//Convert world coordinates to screen coordinates - object needs to know scrolling offset.
ScreenPositionX = (int)Position.X - ScrollX;
if(facingLeft == true)
{
ScreenPositionX--;
}
ScreenPositionY = (int)Position.Y - ScrollY;
// Objects are given an ID on spawning - all objects are spawned through an object creator.
// The object creator won't allow an ID greater of 128.
if(facingLeft == true)
{
SetObject(objectID, ATTR0_SHAPE(Shape) | ATTR0_8BPP | ATTR0_REG | ATTR0_Y(ScreenPositionY),
ATTR1_SIZE(Size) | ATTR1_X(ScreenPositionX) | ATTR1_HFLIP,
ATTR2_ID(currentAnimation->Frames[currentAnimation->CurrentFrame]));
}
else
{
SetObject(objectID, ATTR0_SHAPE(Shape) | ATTR0_8BPP | ATTR0_REG | ATTR0_Y(ScreenPositionY),
ATTR1_SIZE(Size) | ATTR1_X(ScreenPositionX),
ATTR2_ID(currentAnimation->Frames[currentAnimation->CurrentFrame]));
}
}
void GameObject::Update()
{
//Update Velocity using Acceleration.
Velocity = AddVectors2D(Velocity, Acceleration);
//Clip Velocity to Max Velocity
if(Velocity.X < -MaxVelocity.X)
{
Velocity.X = -MaxVelocity.X;
}
if(Velocity.X > MaxVelocity.X)
{
Velocity.X = MaxVelocity.X;
}
if(Velocity.Y < -MaxVelocity.Y)
{
Velocity.Y = -MaxVelocity.Y;
}
if(Velocity.Y > MaxVelocity.Y)
{
Velocity.Y = MaxVelocity.Y;
}
if(fabs(Acceleration.X) < (double)0.1f) // If there is no force being applied, drag will slow us down.
{
Velocity.X *= Drag;
}
// Check for collisions before updating position with velocity.
// CheckCollision will alter the velocity to prevent a collision before it happens.
CheckCollision();
//Update Position using Velocity.
Position = AddVectors2D(Position, Velocity);
// Update Animation & frameCounter
currentAnimation->Update(frameCounter);
frameCounter++;
if(frameCounter > 60)
{
frameCounter = 0;
}
}
u16 GameObject::GetMapTileAt(int x, int y, int screenblock = 28)
{
// Retrieve map tile on current level at this world coordinate.
int TileX, TileY;
// Tiles are 8x8, so world -> tile is just a division by 8
TileX = x / 8;
TileY = y / 8;
// Tilemap is 32x32, so we add the X-coordinate multiplied by 32 to get the right place in memory.
return se_mem[screenblock][TileX + (TileY*32)];
}
void GameObject::CheckCollision()
{
// Have to check X and Y axes separately to prevent "stickiness"
if(GetMapTileAt(Position.X + Velocity.X, Position.Y + Height) != TILE_EMPTY || GetMapTileAt(Position.X + Velocity.X, Position.Y) != TILE_EMPTY || GetMapTileAt(Position.X + Velocity.X + Width, Position.Y) != TILE_EMPTY || GetMapTileAt(Position.X + Velocity.X + Width, Position.Y + Height) != TILE_EMPTY ) // Have to check all 4 corners for X axis
{
Velocity.X = 0;
}
if(GetMapTileAt(Position.X, Position.Y + Velocity.Y + Height) != TILE_EMPTY || GetMapTileAt(Position.X + Width, Position.Y + Velocity.Y) != TILE_EMPTY || GetMapTileAt(Position.X, Position.Y + Velocity.Y) != TILE_EMPTY || GetMapTileAt(Position.X + Width, Position.Y + Velocity.Y + Height) != TILE_EMPTY ) // Have to check all 4 corners for Y axis
{
Velocity.Y = 0;
}
}