-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityPlayer.cpp
175 lines (142 loc) · 3.13 KB
/
EntityPlayer.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "EntityPlayer.h"
EntityPlayer::EntityPlayer(TypePlayer playerType)
{
position = playerType.startPos;
rotation = playerType.rotation;
acceleration = playerType.acceleration;
maxSpeed = playerType.maxSpeed;
size = playerType.size;
texture = playerType.texture;
online = false;
bb = new RectangleShape(new glm::vec2(0, 0), 0.0, new glm::vec2(playerType.size.x, playerType.size.y/4)); //TODO BOUNDINGBOXES
inventoryOpen = false;
}
EntityPlayer::~EntityPlayer(void)
{
inventory.reset();
}
void EntityPlayer::setInventory(std::shared_ptr<Inventory> inv)
{
inventory = inv;
}
void EntityPlayer::toggleInventory(void)
{
inventoryOpen = !inventoryOpen;
}
void EntityPlayer::update(float delta, const Uint8* keyCode)
{
if(keyCode[SDL_SCANCODE_W])
velocity.y += acceleration;
if(keyCode[SDL_SCANCODE_S])
velocity.y -= acceleration;
if(keyCode[SDL_SCANCODE_A])
velocity.x -= acceleration;
if(keyCode[SDL_SCANCODE_D])
velocity.x += acceleration;
if(abs(velocity.x) > maxSpeed)
{
if(velocity.x > 0)
velocity.x = maxSpeed;
else if(velocity.x < 0)
velocity.x = -maxSpeed;
}
if(abs(velocity.y) > maxSpeed)
{
if(velocity.y > 0)
velocity.y = maxSpeed;
else if(velocity.y < 0)
velocity.y = -maxSpeed;
}
if(!keyCode[SDL_SCANCODE_A] && !keyCode[SDL_SCANCODE_D])
{
if(velocity.x > 0)
velocity.x -= friction*delta;
else if(velocity.x < 0)
velocity.x += friction*delta;
}
if(!keyCode[SDL_SCANCODE_W] && !keyCode[SDL_SCANCODE_S])
{
if(velocity.y > 0)
velocity.y -= friction*delta;
else if(velocity.y < 0)
velocity.y += friction*delta;
}
if(abs(velocity.x) < 0.5)
velocity.x = 0;
if(abs(velocity.y) < 0.5)
velocity.y = 0;
position.x += velocity.x * delta;
position.y += velocity.y * delta;
}
void EntityPlayer::updateNoKey(float delta)
{
if(abs(velocity.x) > maxSpeed)
{
if(velocity.x > 0)
velocity.x = maxSpeed;
else if(velocity.x < 0)
velocity.x = -maxSpeed;
}
if(abs(velocity.y) > maxSpeed)
{
if(velocity.y > 0)
velocity.y = maxSpeed;
else if(velocity.y < 0)
velocity.y = -maxSpeed;
}
if(velocity.x > 0)
velocity.x -= friction*delta;
else if(velocity.x < 0)
velocity.x += friction*delta;
if(velocity.y > 0)
velocity.y -= friction*delta;
else if(velocity.y < 0)
velocity.y += friction*delta;
if(abs(velocity.x) < 0.5)
velocity.x = 0;
if(abs(velocity.y) < 0.5)
velocity.y = 0;
position.x += velocity.x * delta;
position.y += velocity.y * delta;
}
glm::vec2 EntityPlayer::getCenterPosition(void)
{
return glm::vec2(position.x + bb->getWidth()/2, position.y + bb->getHeight()/2);
}
void EntityPlayer::setId(int idIn)
{
id = idIn;
online = true;
}
int EntityPlayer::getId(void)
{
return id;
}
bool EntityPlayer::isOnline(void)
{
return online;
}
glm::vec2 EntityPlayer::getSize(void)
{
return size;
}
RectangleShape* EntityPlayer::getBB(void)
{
return bb;
}
std::string EntityPlayer::getTexture(void)
{
return texture;
}
void EntityPlayer::setFriction(float frictionIn)
{
friction = frictionIn;
}
bool EntityPlayer::hasInventoryOpen(void)
{
return inventoryOpen;
}
std::shared_ptr<Inventory> EntityPlayer::getInventory(void)
{
return inventory;
}