-
Notifications
You must be signed in to change notification settings - Fork 0
/
mover.cpp
142 lines (125 loc) · 2.71 KB
/
mover.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
/*
Group #: 3
Members: Chris Prosser, Jacob Gearhart, Kory Kappel,
Course: COMP 322, Advanced Programming
Date: 28 October 2013
Description: This file is inherited from the skeleton
provided by Charles Kelly, heavily modified by Chris
for COMP 441 Program 1.
*/
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// Chapter 6 version 1.0
#include "mover.h"
using namespace moverNS;
//=============================================================================
// default constructor
//=============================================================================
Mover::Mover() : Object()
{
spriteData.x = moverNS::X; // location on screen
spriteData.y = moverNS::Y;
radius = moverNS::COLLISION_RADIUS;
startFrame = moverNS::START_FRAME; // first frame of ship animation
endFrame = moverNS::END_FRAME; // last frame of ship animation
setCurrentFrame(startFrame);
distToMove = 0;
}
void Mover::chooseDir(GridLoc start, GridLoc end)
{
if(end.x > start.x)
{
if(end.y > start.y)
{
setDir(DOWN_RIGHT);
}
else if(end.y < start.y)
{
setDir(UP_RIGHT);
}
else
{
setDir(RIGHT);
}
}
else if(end.x < start.x)
{
if(end.y > start.y)
{
setDir(DOWN_LEFT);
}
else if(end.y < start.y)
{
setDir(UP_LEFT);
}
else
{
setDir(LEFT);
}
}
else
{
if(end.y > start.y)
{
setDir(DOWN);
}
else if(end.y < start.y)
{
setDir(UP);
}
}
}
void Mover::setDir(moverNS::DIR d){
if (distToMove > 0) return;
GridLoc temp = getGridLoc();
int x = temp.x; int y = temp.y;
switch(d){
case LEFT: setPosRaw(x-1,y); break;
case RIGHT: setPosRaw(x+1,y); break;
case UP: setPosRaw(x,y-1); break;
case DOWN: setPosRaw(x,y+1); break;
case UP_RIGHT: setPosRaw(x+1,y-1); break;
case UP_LEFT: setPosRaw(x-1,y-1); break;
case DOWN_LEFT: setPosRaw(x-1,y+1); break;
case DOWN_RIGHT:setPosRaw(x+1,y+1); break;
}
dir = d;
distToMove = GRID_SIZE*getScale();
}
void Mover::move(float frameTime){
if (distToMove <= 0) return;
float change = PIXELS_PER_SECOND*frameTime;
if (distToMove < PIXELS_PER_SECOND*frameTime) change = distToMove;
switch(dir){
case LEFT:
setX(getX() - change);
break;
case RIGHT:
setX(getX() + change);
break;
case UP:
setY(getY() - change);
break;
case DOWN:
setY(getY() + change);
break;
case UP_RIGHT:
setY(getY() - change);
setX(getX() + change);
break;
case UP_LEFT:
setY(getY() - change);
setX(getX() - change);
break;
case DOWN_LEFT:
setY(getY() + change);
setX(getX() - change);
break;
case DOWN_RIGHT:
setY(getY() + change);
setX(getX() + change);
break;
}
distToMove -= change;
}