-
Notifications
You must be signed in to change notification settings - Fork 0
/
playerentity.cpp
154 lines (120 loc) · 3.35 KB
/
playerentity.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
//
// File.cpp
// swarm
//
// Created by Laszlo Blazovics on 1/16/13.
//
//
#include "playerentity.h"
#include "place.h"
#include "field.h"
PlayerEntity::PlayerEntity() : Entity()
{
this->targetPlace = NULL;
this->destinationPlace = NULL;
}
void PlayerEntity::setTargetPlace(Place *targetPlace)
{
if(targetPlace == NULL || this->currentPlace == NULL)
return;
this->targetPlace = targetPlace;
Place** routePlaces = NULL;
unsigned routeLength = 0;
Field* currentField = this->targetPlace->getParentField();
currentField->getPathBetweenPlaces(this->currentPlace, targetPlace, &routePlaces, routeLength);
if(routeLength == 0)
{
//TODO: Unable to go there!
return;
}
this->desiredPlace = routePlaces[0];
free(routePlaces);
}
bool PlayerEntity::selfWander()
{
//choose a neighbor
unsigned freeNbSize = 0;
char* neighbors = NULL;
this->currentPlace->getFreeNeighbors(&neighbors,freeNbSize);
//genrate random number
if (freeNbSize > 0){
int randomNumber = rand();
unsigned index = randomNumber % freeNbSize;
unsigned newID = neighbors[index];
Place* newPlace = currentPlace->getNeighbor(newID);
Point2i p = newPlace->getPosition();
std::cout<<"possible places: "<<freeNbSize<<" randomNum: "<<randomNumber<<" index: "<<index<<" new place: "<<newID<<" "<<p.x<<" "<<p.y<<std::endl;
this->desiredPlace = newPlace;
delete [] neighbors;
return true;
}
return false;
}
bool PlayerEntity::moveTowardsTheTarget()
{
if(this->targetPlace == NULL)
return false;
Place** routePlaces = NULL;
unsigned routeLength = 0;
Field* currentField = this->targetPlace->getParentField();
currentField->getPathBetweenPlaces(this->currentPlace, targetPlace, &routePlaces, routeLength);
if(routeLength == 0)
{
//TODO: Failed
return false;
}
if(routeLength < 2)
{
this->desiredPlace = routePlaces[0];
//TODO: Reached
return false;
}
else
{
this->desiredPlace = routePlaces[1];
}
free(routePlaces);
return true;
}
void PlayerEntity::Step()
{
if(!moveTowardsTheTarget())
{
if(!selfWander())
{
//TODO: GAME OVER
}
}
}
void PlayerEntity::Draw(float dTime)
{
Animate();
if(this->currentPlace == NULL)
return;
float unit = this->currentPlace->getParentField()->getUnit();
float height = sqrt(3)/2*unit;
Point2f placePos;
if(animating)
{
placePos = this->animationPosition;
}
else
{
placePos = this->currentPlace->getOnScreenPos();
}
Point2i offset = this->currentPlace->getParentField()->getOffset();
#ifdef MARMALADE_UI
CIwSVec2 Position(placePos.x-offset.x, placePos.y-offset.y);
// Build the transform
// Set the rotation transform
Transform.SetRot(0);
// Scale the transform
Transform.ScaleRot(IW_GEOM_ONE);
// Translate the transform
Transform.SetTrans(Position);
// Set this transform as the active transform for Iw2D
Iw2DSetTransformMatrix(Transform);
Iw2DSetColour(0xff00ffff); // Set red
Iw2DFillArc(CIwSVec2(height, unit), CIwSVec2((unit/2), (unit/2)), 0, IW_ANGLE_2PI);
#endif
}