-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategy.cpp
More file actions
150 lines (117 loc) · 3.52 KB
/
Strategy.cpp
File metadata and controls
150 lines (117 loc) · 3.52 KB
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
#include<iostream>
#include<string>
using namespace std;
/*
PlayerController controls Player. Player can be a skeleton or a mower. Skeleton and mower shares the same movement logic, but they have different abilities.
Skeleton and Mower moves on different kind of platforms. Each platform defines a type of movement, and accepts only some inputs.
There are two types of platforms:
* PlatformRotate: can move only left or right, rotates 90 degrees in that direction
* PlatformForward: can only go back and forward, moves by speed
*/
class IMoveBehavior;
class PlayerModel{
public:
int speed;
float rotation;
IMoveBehavior *moveBehavior = nullptr;
PlayerModel();
void move();
virtual void ability() = 0;
};
class IMoveBehavior{
public:
PlayerModel *player = nullptr;
virtual void move() = 0;
};
class PlatformForwardBehavior : public IMoveBehavior{
public:
PlatformForwardBehavior(PlayerModel *pl){
this->player = pl;
}
void move() override{
string key;
while(key != "exit"){
cout << "Listening for forward platform inputs" << endl;
cin >> key;
if(key == "w"){
cout << "Moving " << player->speed << " meters forward";
}else if(key == "s"){
cout << "Moving " << player->speed << " meters backward";
}
}
}
};
class PlatformRotateBehavior : public IMoveBehavior{
public:
PlatformRotateBehavior(PlayerModel *pl){
this->player = pl;
}
void move() override{
string key;
while(key != "exit"){
cout << "Listening for rotate platform inputs" << endl;
cin >> key;
if(key == "a"){
cout << "Rotating " << player->rotation << " degrees left";
}else if(key == "d"){
cout << "Rotating " << player->rotation << " degrees right";
}
}
}
};
class Skeleton : public PlayerModel{
public:
Skeleton() : PlayerModel(){
speed = 1;
rotation = 0;
}
void ability() override{
cout << "Skeleton ability" << endl;
}
};
class Mower : public PlayerModel{
public:
Mower() : PlayerModel(){
speed = 3;
rotation = 0;
}
void ability() override{
cout << "Mower ability" << endl;
}
};
class PlayerController{
public:
int energy = 0;
PlayerModel *player = nullptr;
PlayerController(int energy){
this->energy = energy;
if(this->energy > 50){
player = new Mower();
}else{
player = new Skeleton();
}
}
void forwardPlatformMovement(){
player->moveBehavior = new PlatformForwardBehavior(player);
player->move();
}
void rotatePlatformMovement(){
player->moveBehavior = new PlatformRotateBehavior(player);
player->move();
}
};
// PlayerModel definition
PlayerModel::PlayerModel(){
//default moveBehavior
moveBehavior = new PlatformForwardBehavior(this);
}
void PlayerModel::move(){
moveBehavior->move();
}
int main(){
PlayerController *pc = new PlayerController(100);
pc->forwardPlatformMovement();
pc->rotatePlatformMovement();
pc->forwardPlatformMovement();
return 0;
}