This repository has been archived by the owner on Mar 6, 2018. It is now read-only.
/
PlayerOctopus.cpp
190 lines (162 loc) · 4.73 KB
/
PlayerOctopus.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "PlayerOctopus.h"
#include "ServiceLocator.h"
Texture PlayerOctopus::_basic_player;
double PlayerOctopus::_ratio;
Point PlayerOctopus::PADDINGFaceLeft;
Point PlayerOctopus::PADDINGFaceRight;
PlayerOctopus::PlayerOctopus(Character* c, GridOctopus* grid)
: OctopusBaby(PLAYER_WIDTH, PLAYER_HEIGHT), _character(c), _grid(grid), destination_cell(_character->getCell())
{
if (!_basic_player.valid())
{
_basic_player = (*ServiceLocator::getTextureManager())["knight"];
// compute ratio
double ratiox = (double)_relative_rect.w / _basic_player.getWidth();
double ratioy = (double)_relative_rect.h / _basic_player.getHeight();
_ratio = min(ratioy, ratiox);
// calculate padding
int xpadding = -5;
int ypadding = 7;
PADDINGFaceLeft = Point(-_basic_player.getWidth() * _ratio / 2 + xpadding, -_basic_player.getHeight() * _ratio + ypadding);
PADDINGFaceRight = Point(PADDINGFaceLeft.x -2*xpadding, PADDINGFaceLeft.y);
}
setBgColor(Color::TRANSPARENT);
// event registration
EventService* evs = EVENTSERVICE;
evs->listen(typeid(MoveEvent), [=](Event* e) {
MoveEvent* ev = dynamic_cast<MoveEvent*>(e);
if (ev && ev->getCharacter() == _character)
addMove(ev->destination, ev->move_wanted);
}
);
}
PlayerOctopus::~PlayerOctopus()
{
}
void PlayerOctopus::update()
{
if (isActive() && _character)
{
// update position based on cell
//LOGINFO << "Position: " << getPosition() << endl;
if (!real_cell)
teleport(real_cell = _character->getCell());
else if (!move_wanted)
{
teleport(real_cell = destination_cell);
moving = false;
pathNext();
}
else if (real_cell != destination_cell && _land_time + move_delay < TIMESERVICE->time())
{
//LOGINFO << "Moving" << endl;
// delta time in milliseconds
Uint32 dt = (TIMESERVICE->time() - movement_begin);
Point origin_pos = toContainerCoordinates(_grid->toAbsoluteCoordinates(_grid->getCellCenter(real_cell)));
Point dest_pos = toContainerCoordinates(_grid->toAbsoluteCoordinates(_grid->getCellCenter(destination_cell)));
Point delta = abs(origin_pos - dest_pos);
Point middle = (origin_pos - dest_pos) / 2;
Point step = ((dest_pos - origin_pos) * dt)/move_time;
// jump
Point jump(0, sqrt(pow(jump_height, 2) - (pow(jump_height,2) * abs(delta.y - step.y))/pow( delta.y, 2)));
Point pos = origin_pos + step - jump;
Point newPos = pos + getPadding();
step = abs(step);
if (step.x >= delta.x && step.y >= delta.y)
{
//LOGINFO << "Landing on new cell" << endl;
newPos = dest_pos + getPadding();
real_cell = destination_cell;
moving = false;
_grid->unmark(destination_cell->getNumber());
pathNext();
}
setPosition(newPos);
}
// update z-index
setZIndex(_grid->getZIndexFromCell(destination_cell->getNumber()));
}
}
void PlayerOctopus::internalRender(SDL_Renderer* r, bool force)
{
if (isActive())
{
bool d = (force || isDirty());
if (d)
{
SDL_Rect dst = _relative_rect;
dst.w = int(_ratio * _basic_player.getWidth());
dst.h = int(_ratio * _basic_player.getHeight());
dst.x = 0;
dst.y = 0;
SDL_RenderCopyEx(r, (*ServiceLocator::getTextureManager())[_character->getType()], nullptr, &dst, 0.0, nullptr, (SDL_RendererFlip)orientation);
}
}
}
void PlayerOctopus::addMove(Cell* c, bool moveWanted)
{
if (path.empty() || (c->adjacent(path.back()) && moveWanted))
{
path.push_back(c);
move_anims.push_back(moveWanted);
_grid->mark(c->getNumber(), Color(127,127,127,50));
// _character->displayBasic(LOGINFO);
// LOGINFO << " Adding move !" << endl;
pathNext();
}
}
void PlayerOctopus::pathNext()
{
if (!moving && !path.empty())
{
// set the next destination
destination_cell = path.front();
move_wanted = move_anims.front();
// orientation
if (destination_cell->getPosX() > real_cell->getPosX() || (destination_cell->getPosX() == real_cell->getPosX() && destination_cell->getPosY() < real_cell->getPosY()))
{
orientation = RIGHT;
}
else
orientation = LEFT;
path.pop_front();
move_anims.pop_front();
moving = true;
movement_begin = TIMESERVICE->time() + move_delay;
_land_time = TIMESERVICE->time();
}
}
void PlayerOctopus::teleport(const Cell* c)
{
if (c)
{
Point cellCenter = toContainerCoordinates(_grid->toAbsoluteCoordinates(_grid->getCellCenter(c)));
Point pos = cellCenter + getPadding();
if ((Point)pos != getPosition())
{
setPosition(int(pos.x), int(pos.y));
}
}
}
void PlayerOctopus::setPosition(Point pos)
{
if (pos != getPosition())
{
OctopusBaby::setPosition((int)pos.x, (int)pos.y);
}
}
Point PlayerOctopus::getPadding() const
{
switch (orientation)
{
case PlayerOctopus::LEFT:
return PADDINGFaceLeft;
break;
case PlayerOctopus::RIGHT:
return PADDINGFaceRight;
break;
default:
break;
}
return PADDINGFaceRight;
}