-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEnemy.cpp
More file actions
138 lines (123 loc) · 3.72 KB
/
Copy pathEnemy.cpp
File metadata and controls
138 lines (123 loc) · 3.72 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
#include "Enemy.h"
#include "SDL.h"
#include "InputHandler.h"
Enemy::Enemy() : SDLGameObject()
{
m_velocity.setY(2);
m_velocity.setX(0.001f);
}
void Enemy::draw()
{
SDLGameObject::draw(); //now we use SDLGameObject
}
//Update function for Enemy object
void Enemy::update()
{
//The following code handles joystick input
//Uncomment to enable the functionality
/*
m_velocity.setX(0);
m_velocity.setY(0);
m_acceleration.setX(0);
m_acceleration.setY(0);
handleInput(); //handles the input from joystick [2nd controller]
*/
m_currentFrame = int(((SDL_GetTicks() / 100) % m_numFrames));
//Code to move the Enemy up and down
if (m_position.getY() < 0)
{
m_velocity.setY(2);
}
else if (m_position.getY() > 400)
{
m_velocity.setY(-2);
}
SDLGameObject::update();
}
void Enemy::clean()
{
//blank clean() function
}
//Enemy class uses the Second Joystick
void Enemy::handleInput()
{
//check if a second controller is available and use it for enemy object
if ((TheInputHandler::Instance()->joysticksInitialised()) && (SDL_NumJoysticks() > 1))
{
//for analog sticks of the second joystick
if ((TheInputHandler::Instance()->xvalue(1, 1) > 0) || (TheInputHandler::Instance()->xvalue(1, 1) < 0))
{
//if we are moving the player with analog stick while pressing Y
//it will get an acceleration of 2 in that direction
m_velocity.setX((float)1 * TheInputHandler::Instance()->xvalue(1, 1));
if (TheInputHandler::Instance()->getButtonState(0, 3)) //Button 3(Yellow)
{
//give the player some acceleration
m_acceleration.setX((float)2 * TheInputHandler::Instance()->xvalue(1, 1));
}
}
if ((TheInputHandler::Instance()->yvalue(1, 1) > 0) || (TheInputHandler::Instance()->yvalue(1, 1) < 0))
{
m_velocity.setY((float)1 * TheInputHandler::Instance()->yvalue(1, 1));
if (TheInputHandler::Instance()->getButtonState(1, 3))
{
m_acceleration.setY((float)2 * TheInputHandler::Instance()->yvalue(1, 1));
}
}
if ((TheInputHandler::Instance()->xvalue(1, 2) > 0) || (TheInputHandler::Instance()->xvalue(1, 2) < 0))
{
m_velocity.setX((float)1 * TheInputHandler::Instance()->xvalue(1, 2));
if (TheInputHandler::Instance()->getButtonState(1, 3))
{
m_acceleration.setX((float)2 * TheInputHandler::Instance()->xvalue(1, 1));
}
}
if ((TheInputHandler::Instance()->yvalue(1, 2) > 0) || (TheInputHandler::Instance()->yvalue(1, 2) < 0))
{
m_velocity.setY((float)1 * TheInputHandler::Instance()->yvalue(1, 2));
if (TheInputHandler::Instance()->getButtonState(1, 3))
{
m_acceleration.setY((float)2 * TheInputHandler::Instance()->yvalue(1, 1));
}
}
}
//no mouse control for enemy
//keyboard A W S D for movement, Right Shift for acceleration
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_D))
{
m_velocity.setX(2);
//check if Right Ctrl is pressed, provide acc. if true
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LSHIFT))
{
m_acceleration.setX(2.5);
}
}
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_A))
{
m_velocity.setX(-2);
//check if Right Ctrl is pressed, provide acc. if true
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LSHIFT))
{
m_acceleration.setX(-2.5);
}
}
//remember the cartesian plane is inverted along Y Axis
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_W))
{
m_velocity.setY(-2);
//check if Right Ctrl is pressed, provide acc. if true
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LSHIFT))
{
m_acceleration.setY(-2.5);
}
}
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_S))
{
m_velocity.setY(2);
//check if Right Ctrl is pressed, provide acc. if true
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LSHIFT))
{
m_acceleration.setY(2.5);
}
}
}