-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteModel.cpp
More file actions
203 lines (146 loc) · 4 KB
/
SpriteModel.cpp
File metadata and controls
203 lines (146 loc) · 4 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* SpriteModel.cpp
Robert Kirk DeLisle
30 July 2006
Purpose: Data model for a sprite.
Modificaton History:
*/
#include "SpriteModel.h"
#include "Utilities\\ExceptionRKD.h"
#include <stdlib.h>
#include <fstream>
using namespace std;
void SpriteModel::Move(bool loggit)
{
/* Purpose: move the sprite based on the current speed with some bounds checking
bondary behavior determined by m_BoundaryWrap - either crisp or wrapping
Parameters: none
Return: none
Exceptions: none
*/
//store previous state
m_PreviousState = m_State;
//store previous speed
m_PreviousSpeedX = m_SpeedX;
m_PreviousSpeedY = m_SpeedY;
//store the position before changing it
m_PreviousX = m_CurrentX;
m_PreviousY = m_CurrentY;
//make the move
m_CurrentX += m_SpeedX;
m_CurrentY += m_SpeedY;
//check boundary conditions
if ( m_BoundaryWrap )
{
if ( m_CurrentX < m_MinX )
m_CurrentX = m_MaxX-abs(m_SpeedX);
else if ( m_CurrentX > m_MaxX-abs(m_SpeedX) )
m_CurrentX = m_MinX;
if ( m_CurrentY < m_MinY )
m_CurrentY = m_MaxY-abs(m_SpeedY);
else if ( m_CurrentY > m_MaxY-abs(m_SpeedY) )
m_CurrentY = m_MinY;
}
else
{
if ( m_CurrentX < m_MinX )
m_CurrentX = m_MinX;
else if ( m_CurrentX > m_MaxX )
m_CurrentX = m_MaxX;
if ( m_CurrentY < m_MinY )
m_CurrentY = m_MinY;
else if ( m_CurrentY > m_MaxY )
m_CurrentY = m_MaxY;
}
//this block is used to monitor details during different runs for each call to this function
//I commented it out to eliminate an extra comparison every time
// if (loggit)
// {
// fstream lf;
// lf.open("InOut.csv", ios::out|ios::app);
// lf << m_CurrentX << "," << m_CurrentY << endl;
// lf.close();
// }
return;
}
void SpriteModel::SetPosition(long &X, long &Y)
{
/* Purpose: set the sprite to a specific position
and return the old position in the parameters
Parameters: X,Y - target position
Return: old X and Y in parameter
Exceptions: Target position out of bounds
*/
long tempX, tempY; //swap variables
//do some bounds checking
if ( (X > m_MaxX) || (X < m_MinX) || (Y > m_MaxY) || (Y < m_MinY) )
throw ExceptionRKD(201,"SpriteModel::SetPosition","Target position out of bounds.");
tempX = m_CurrentX;
tempY = m_CurrentY;
m_CurrentX = X;
m_CurrentY = Y;
X = tempX;
Y = tempY;
//hold onto the position as "previous" for erase purposes
m_PreviousX=m_CurrentX;
m_PreviousY=m_CurrentY;
return;
}
void SpriteModel::SetSpeed(long &XSpeed, long &YSpeed)
{
/* Purpose: set the speed variables and return the old X and Y speed in the parameters
Parameters: XSpeed, YSpeed - new speeds
Return: old X and YSpeed in parameters
Exceptions: none
*/
long tempX, tempY;
tempX = m_SpeedX;
tempY = m_SpeedY;
m_SpeedX = XSpeed;
m_SpeedY = YSpeed;
XSpeed = tempX;
YSpeed = tempY;
m_PreviousSpeedX = tempX;
m_PreviousSpeedY = tempY;
}
void SpriteModel::SetState(long &newState)
{
/* Purpose: set the state variable and return the old state in the parameter
Parameters: newState - the new state of the sprite
Return: old state in parameters
Exceptions: none
*/
long tempState;
tempState = m_State;
m_State = newState;
newState = tempState;
m_PreviousState = tempState;
}
void SpriteModel::RevertSpeed()
{
/* Purpose: go back to the previous speed
if the previous is the same as the current - full stop
Parameters: none
Return: none
Exceptions: none
*/
if ( m_SpeedX == m_PreviousSpeedX && m_SpeedY == m_PreviousSpeedY)
{
m_SpeedX = 0;
m_SpeedY = 0;
}
else
{
m_SpeedX = m_PreviousSpeedX;
m_SpeedY = m_PreviousSpeedY;
}
return;
}
void SpriteModel::RevertState()
{
/* Purpose: go back to the previous state
Parameters: none
Return: none
Exceptions: none
*/
m_State = m_PreviousState;
}