-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.cpp
More file actions
163 lines (111 loc) · 4.39 KB
/
Player.cpp
File metadata and controls
163 lines (111 loc) · 4.39 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
#include "Player.h"
#include "Game.h"
Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams){}
void Player::draw()
{
//override SDLGameObject draw function so we can flip the image corresponding to X incrementation
//if the player is moving
if(m_velocity.getX() != 0 || m_velocity.getY() != 0)
{
//if the player is moving to the left, flip the image
if(m_velocity.getX() < 0 && m_velocity.getY() == 0)
{
TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow + 1, m_currentFrame, TheGame::Instance()->getRenderer(),SDL_FLIP_HORIZONTAL);
}
//if the player is moving to the right, use the normal "right facing" sprite row
else if(m_velocity.getX() > 0 && m_velocity.getY() == 0)
{
TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow + 1, m_currentFrame, TheGame::Instance()->getRenderer());
}
//if the player is moving up
if(m_velocity.getY() < 0)
{
TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame + 3, TheGame::Instance()->getRenderer());
}
else if(m_velocity.getY() > 0)
{
TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer());
}
}
//if the player is not moving, use the first sprite row images, so he will face us now
else
{
TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer());
}
}
void Player::update(){
//set up starting velocity
m_velocity.setX(0);
m_velocity.setY(0);
//override currentRow (don't need this currently)
//m_currentRow = 1;
handleInput(); // update movement
//start: frame 1, continue 3 more frames:
//animation speed: 100
m_currentFrame = int(((SDL_GetTicks() / 100) % 3));
//update
SDLGameObject::update();
}
void Player::handleInput()
{
/* ----------------------------------- */
/* CONTROLLER (need fix)*/
if(TheInputHandler::Instance()->joysticksInitialised())
{
/* CONTROLLER STICK*/
if(TheInputHandler::Instance()->xvalue(0, 1) > 0 || TheInputHandler::Instance()->xvalue(0, 1) < 0)
{
m_velocity.setX(1 * TheInputHandler::Instance()->xvalue(0,1));
}
if(TheInputHandler::Instance()->yvalue(0, 1) > 0 || TheInputHandler::Instance()->yvalue(0, 1) < 0)
{
m_velocity.setY(1 * TheInputHandler::Instance()->yvalue(0,1));
}
if(TheInputHandler::Instance()->xvalue(0, 2) > 0 || TheInputHandler::Instance()->xvalue(0, 2) < 0)
{
m_velocity.setX(1 * TheInputHandler::Instance()->xvalue(0,2));
}
if(TheInputHandler::Instance()->yvalue(0, 2) > 0 || TheInputHandler::Instance()->yvalue(0, 2) < 0)
{
m_velocity.setY(1 * TheInputHandler::Instance()->yvalue(0,2));
}
/* CONTROLLER BUTTONS */
//testing button input, not working either tho...
if(TheInputHandler::Instance()->getButtonState(0, 1))
{
m_velocity.setX(1);
}
}
/* ----------------------------------- */
/* MOUSE BUTTON EVENTS */
//left button move object left
/* if(TheInputHandler::Instance()->getMouseButtonState(LEFT))
{
m_velocity.setX(-1);
}
//right button move object right
if(TheInputHandler::Instance()->getMouseButtonState(RIGHT))
{
m_velocity.setX(1);
}
*/
/* ----------------------------------- */
/* KEYBOARD */
if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_RIGHT))
{
m_velocity.setX(2);
}
if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LEFT))
{
m_velocity.setX(-2);
}
if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_UP))
{
m_velocity.setY(-2);
}
if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_DOWN))
{
m_velocity.setY(2);
}
}
void Player::clean(){}