-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.h
145 lines (113 loc) · 3.68 KB
/
Entity.h
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
//
// Created by flo on 10/28/2021.
//
#ifndef NOMPROJET_ENTITY_H
#define NOMPROJET_ENTITY_H
#include <iostream>
#include <cmath>
#include "LetterMap.h"
#include "Camera2D.h"
using namespace std;
typedef pair<int,int> pi;
// Base class Interface
class Entity {
private:
LetterMap map;
int delay_move; // millisec w8ing delay b4 moving again
int visibility_ray;
int localPosInPath=0;
vector<pi> path; // percorso che deve seguire corrente
public:
int row_pos,col_pos;
float real_row,real_col; // slerp smooth row and col (pixels)
int targetRow,targetCol;
float smoothness = 0.25;
Entity(LetterMap &map,int visibility_ray,int delay_move){
this->map=map;
this->visibility_ray=visibility_ray;
this->delay_move=delay_move;
//changeRandomTargetPos();
}
virtual void draw(RenderWindow &window,int pixePerUnit,Camera2D &cam)=0;
void setRow(int r) {this->row_pos = r;}
void setCol(int c) {this->col_pos = c;}
int getRow(){return row_pos;}
int getCol(){return col_pos;}
int getVisibilityRay(){return visibility_ray;}
int getMillisecDelayMove(){return delay_move;}
bool nextStep(){
if(path.size()<=1)return false;
localPosInPath++;
//std::cout << "passo numero : " << localPosInPath << " \n";
pi next = path[1];
path.erase(path.begin());
setRow(next.first);
setCol(next.second);
return true;
}
double calcDist(int row, int col, int x, int y) {
return ((double) sqrt((row - x) * (row - x) + (col - y) * (col - y)));
}
bool searchTargetAround(int x,int y){
float distPlayer = calcDist(x,y,getCol(),getRow());
cout << "distance : " << distPlayer << "\n";
if(distPlayer<=visibility_ray){
cout << "oddio sei entrato assurdo";
targetPos(x,y);
return true;
}
return false;
}
void targetPos(int targetRow,int targetCol){
//if(targetRow==this->getCol() && targetCol==this->getRow())return; // seems bugged, but it isnt :/
nextStep();
path.clear();
setTargetRow(targetRow);
setTargetCol(targetCol);
//cout << "troviamo shortest path!!\n";
if(map.path_find(this->getCol(),this->getRow(),targetCol,targetRow,this->path)){
int c = path.size();
// cout << " cost path : " << c << "\n";
}else{
// cout << "problema!\n";
}
}
void changeRandomTargetPos(){
localPosInPath=0;
pi spawn = this->map.getValidSpawn();
targetPos(spawn.first,spawn.second);
}
void setTargetRow(int targetRow){
this->targetRow=targetRow;
}
void setTargetCol(int targetCol){
this->targetCol=targetCol;
}
vector<pair<int,int>> getFollowingPath(){
vector<pair<int,int>> rispath;
for(pair<int,int> p:path)
rispath.push_back({p.first,p.second});
return rispath;
}
float getRealRow(){return real_row;}
float getRealCol(){return real_col;}
void setRealRow(float newX){real_row=newX;}
void setRealCol(float newY){real_col=newY;}
void move(float offsetX,float offsetY){
setRealRow(real_row+offsetX);
setRealCol(real_col+offsetY);
}
void slerpFollowPosition(){
move((row_pos-real_row)*smoothness,(col_pos-real_col)*smoothness);
}
};
//aStarSearch(grid, src, dest,ans);
/*
point s(this->row,this->col), e(targetRow,targetCol);
if(as.search(s,e,m)){
int c = as.path(path);
cout << "cost path : " << c << "\n";}
else
cout << "smth wrong \n";
*/
#endif //NOMPROJET_ENTITY_H