-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreature.cpp
89 lines (76 loc) · 1.61 KB
/
creature.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
#define _USE_MATH_DEFINES
#include <utility>
#include <cmath>
#include "creature.h"
#include <iostream>
// Difference between current position and the tile center.
static const double CENTER_DIM = 0.05;
Creature::Creature(int x_b, int y_b, float z_b)
{
x = x_b;
y = y_b;
z = z_b;
tileX = x_b;
tileY = y_b;
atCenter = true;
}
std::pair<int,int> Creature::calcCurrentTile()
{
if ((angle == 0) && ((x - (int)x) > 0.5))
tileX = (int)ceil(x);
if ((angle == 180) && ((x - (int)x) < 0.5))
tileX = (int)floor(x);
if ((angle == 90) && ((y - (int)y) > 0.5))
tileY = (int)ceil(y);
if ((angle == 270) && ((y - (int)y) < 0.5))
tileY = (int)floor(y);
std::pair<int, int> p = { tileX,tileY };
return p;
}
bool Creature::isCenterTile()
{
return (
((angle == 0) && (x - (int)x > 1 - CENTER_DIM)) ||
((angle == 180) && (x - (int)x < CENTER_DIM)) ||
((angle == 90) && (y - (int)y > 1 - CENTER_DIM)) ||
((angle == 270) && (y - (int)y < CENTER_DIM))) ? true : false;
}
void Creature::Reassign(int X, int Y)
{
x = X;
y = Y;
}
// Basic movement algorithm.
std::pair<int, int> Creature::Move()
{
if (moving)
{
x += speed*cos(M_PI/180*angle);
y += speed*sin(M_PI/180*angle);
if (!atCenter && isCenterTile())
{
atCenter = true;
onTileCenter();
}
}
int oldTileX = tileX;
int oldTileY = tileY;
std::pair<int,int> p = calcCurrentTile();
//std::cout<<p.first<<" "<<p.second<<"\n";
if (tileX != oldTileX || tileY != oldTileY)
{
atCenter = false;
onTileChange();
}
return p;
}
void Creature::Pad()
{
x = (int)x;
y = (int)y;
}
void Creature::PadToCenter()
{
x = tileX;
y = tileY;
}