-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
executable file
·144 lines (106 loc) · 3.95 KB
/
Player.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
#include "Player.h"
#include "Game.h"
Player::Player(Game& game, const Position& position, const Target& target) : MapComponent(game, position), target(target), current(0)
{
}
void Player::update(float elapsed)
{
// Pfad wird je nach Spielgeschwindigkeit aktualisiert
delta += elapsed * game.getGameSpeed();
if (delta > 0)
generatePath(target.position);
delta -= (int)delta;
}
void Player::draw()
{
CL_Sizef fieldSize = map.getFieldSize();
//for (nodeMap::iterator it = todo.begin(); it != todo.end(); ++it)
// CL_Draw::circle(window.get_gc(), map.getCenter(it->second.position), 10.0f, CL_Colorf::blue);
//for (nodeMap::iterator it = finalized.begin(); it != finalized.end(); ++it)
// CL_Draw::circle(window.get_gc(), map.getCenter(it->second.position), 10.0f, CL_Colorf::black);
// wenn Pfad berechnet wurde
if (current != 0)
{
const Node* node = current->next;
// solange aktuelles Feld ungleich Zielfeld
while (node->position != target.position)
{
// orangen Kreis am aktuellen Feld zeichnen
CL_Draw::circle(window.get_gc(), map.getCenter(node->position), 10.0f, CL_Colorf::orange);
// zum nächsten Feld am Pfad gehen
node = node->next;
}
}
// gelben Kreis an der Position des Spielers zeichnen
CL_Draw::circle(window.get_gc(), map.getCenter(position), min(fieldSize.width, fieldSize.height) / 3.0f, CL_Colorf::yellow);
MapComponent::draw();
}
void Player::generatePath(const Position& target)
{
// todo und finalized liste leeren
todo.clear();
finalized.clear();
// Ziel-Knoten in finalized Liste einfügen
finalized.insert(mapElement(target, Node(target, 0)));
// aktuellen Knoten auf Ziel-Knoten setzen
current = &finalized.at(target);
// rekursive Funktion aufrufen
calculateStep(target);
}
void Player::calculateStep(const Position& target)
{
// Abbruchbedienung (Spieler-Position erreicht)
if (current != 0 && current->position == position)
return;
// Nachbarn des aktuellen Knoten beziehen
std::list<Node> neighbors = map.getNeighbors(current->position);
for (std::list<Node>::iterator it = neighbors.begin(); it != neighbors.end(); ++it)
{
Node& neighbor = *it;
// wenn Kosten größer 0 und noch nicht finalized
if (neighbor.costs > 0 && finalized.find(neighbor.position) == finalized.end())
{
// in todo Liste nach Nachbarn suchen
nodeMap::iterator todoIt = todo.find(neighbor.position);
// wenn noch nicht in todo Liste oder aktuelle Kosten kleiner als Kosten in todo Liste
if (todoIt == todo.end() || todoIt->second.costs > neighbor.costs + current->costs)
{
// wenn in todo Liste, Nachbarn aus todo Liste löschen
if (todoIt != todo.end())
todo.erase(todoIt);
// Nachfolger auf aktuellen Knoten setzen
neighbor.next = current;
// Kosten um bisherige Kosten erhöhen
neighbor.costs += current->costs;
// in todo Liste einfügen
todo.insert(mapElement(neighbor.position, neighbor));
}
}
}
current = &(todo.begin()->second);
int currentHeuristic = getHeuristic(current->position, position);
// in todo Liste nach billigstem Knoten suchen
for (nodeMap::iterator it = todo.begin(); it != todo.end(); ++it)
{
Node& node = it->second;
// Heuristik berechnen
int heuristic = getHeuristic(node.position, position);
// Gesamtkosten vergleichen
if (node.costs + heuristic < current->costs + currentHeuristic)
{
currentHeuristic = heuristic;
current = &node;
}
}
// Knoten in die finalized List einfügen
finalized.insert(mapElement(current->position, *current));
current = &finalized.at(current->position);
// Knoten aus der todo Liste löschen
todo.erase(current->position);
calculateStep(target);
}
// berechnet die Distanz zwischen zwei Feldern (Manhatten Methode)
int Player::getHeuristic(const Position& from, const Position& to) const
{
return std::abs(from.row - to.row) + std::abs(from.col - to.col);
}