-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor.cpp
155 lines (143 loc) · 3.36 KB
/
actor.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
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "actor.h"
#include "map.h"
#include "enums.h"
using namespace sf;
Actor::Actor(){
if (!playerTexture.LoadFromFile("textures/player.png"))
{
std::cout << "Cannot load player" << std::endl;
}
sprite.SetImage(playerTexture);
x=0;
y=0;
}
void Actor::put(int x, int y){
this->x = x;
this->y = y;
update();
}
void Actor::move(Direction dir, Map *map){
getPosition(map);
Tile ¤t = map->tile[y][x];
if(dir == Left){
Tile &next = map->tile[y][x-1];
Tile &nextNext = map->tile[y][x-2];
if(performMove(¤t, &next, &nextNext)) x-=1;
}else if(dir == Up){
Tile &next = map->tile[y-1][x];
Tile &nextNext = map->tile[y-2][x];
if(performMove(¤t, &next, &nextNext)) y -= 1;
}else if(dir == Right){
Tile &next = map->tile[y][x+1];
Tile &nextNext = map->tile[y][x+2];
if(performMove(¤t, &next, &nextNext)) x+= 1;
}else if(dir == Down){
Tile &next = map->tile[y+1][x];
Tile &nextNext = map->tile[y+2][x];
if(performMove(¤t, &next, &nextNext)) y+= 1;
}
if(x>(map->getSizeX()-1)) x=map->getSizeX()-1;
if(y>(map->getSizeY()-1)) y=map->getSizeY()-1;
if(x<0) x=0;
if(y<0) y=0;
update();
if(isFinished(map)){
std::cout<< "Level finished" << std::endl;
map->nextLevel();
}
}
void Actor::update(){
sprite.SetPosition(x*RESOLUTION, y*RESOLUTION);
//std::cout << "x=" << x <<"; y=" << y <<std::endl;
}
bool Actor::performMove(Tile *current, Tile *next, Tile *nextNext){
if(*next!=Wall){
if(*next==Box ){ //Box meeting
if(*nextNext==Exit){ //When we successfully moving box to the exit
*next=GrassPlayer;
*nextNext=ExitBox;
if(*current==GrassPlayer){
*current=Grass;
}else {
*current=Exit;
}
return true;
}else if(*nextNext==Grass){
*nextNext=Box;
*next=GrassPlayer;
if(*current==GrassPlayer){
*current=Grass;
}else {
*current=Exit;
}
return true;
}
}else if(*next==ExitBox ){ //ExitBox meeting
if(*nextNext==Exit){ //When we successfully moving Exitbox to the exit
*next=ExitPlayer;
*nextNext=ExitBox;
if(*current==GrassPlayer){
*current=Grass;
}else {
*current=Exit;
}
return true;
}else if(*nextNext==Grass){
*nextNext=Box;
*next=ExitPlayer;
if(*current==GrassPlayer){
*current=Grass;
}else {
*current=Exit;
}
return true;
}
}else if(*next==Exit){ // When we going to the Exit
*next=ExitPlayer;
if(*current==GrassPlayer){
*current=Grass;
}else {
*current=Exit;
}
return true;
}else{ // Just regular step to the grass
*next=GrassPlayer;
if(*current==GrassPlayer){
*current=Grass;
}else {
*current=Exit;
}
return true;
}
}
return false;
}
bool Actor::isFinished(Map *map){
int sizeX = map->getSizeX();
int sizeY = map->getSizeY();
int freeBoxes=0;
for(int i=0; i<sizeY; i++){
for(int j=0; j<sizeX; j++){
if(map->tile[i][j]==Box) freeBoxes++;
//std::cout << "i=" << i << "; j=" << j << "; boxes="<< freeBoxes<< std::endl;
}
}
if(freeBoxes==0) return true;
return false;
}
void Actor::getPosition(Map *map){
int sizeX = map->getSizeX();
int sizeY = map->getSizeY();
for(int i=0; i<sizeY; i++){
for(int j=0; j<sizeX; j++){
if(map->tile[i][j]==GrassPlayer || map->tile[i][j]==ExitPlayer) {
x=j;
y=i;
}
}
}
}