-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
212 lines (173 loc) · 4.23 KB
/
main.cpp
File metadata and controls
212 lines (173 loc) · 4.23 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "stdafx.h"
#include <stdlib.h>
#include <SFML\Graphics.hpp>
#include <stdlib.h>
#include <iostream>
#include "predator.h"
#include "Prey.h"
using namespace sf;
using namespace std;
//void simplechase(Prey, predator);
//void collisionPred(predator);
void predWall(predator&, FloatRect&, FloatRect&, FloatRect&, FloatRect&, FloatRect&);
void preyWall(Prey&, FloatRect&, FloatRect&, FloatRect&, FloatRect&, FloatRect&);
int _tmain(int argc, _TCHAR* argv[])
{
RenderWindow window(VideoMode(800, 600), "Prey v Pred");//declares window
// initializes all shapes required
Prey prey1;
predator pred1;
RectangleShape top(Vector2f(800, 1));
RectangleShape right(Vector2f(1, 600));
RectangleShape left(Vector2f(1, 600));
RectangleShape bot(Vector2f(800, 1));
bot.setPosition(0, 600);
right.setPosition(800, 0);
int x;
int y;
while (window.isOpen()) // loops untill the window is closed ex. event loop
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
window.setFramerateLimit(60);
//sets bounding boxes for the edges and both forms of objects
FloatRect topBox = top.getGlobalBounds();
FloatRect rightBox = right.getGlobalBounds();
FloatRect leftBox = left.getGlobalBounds();
FloatRect botBox = bot.getGlobalBounds();
FloatRect prey1Box = prey1.getGlobalBounds();
FloatRect pred1Box = pred1.getGlobalBounds();
srand(time(NULL));
x = rand() % 800;
y = rand() % 600;
//logic for the predator simple chase **MOVE TO FUNCTION**
if (prey1.getPosition().x > pred1.getPosition().x)
{
pred1.move(1.0, 0);
}
if (prey1.getPosition().x < pred1.getPosition().x)
{
pred1.move(-1.0, 0);
}
if (prey1.getPosition().y > pred1.getPosition().y)
{
pred1.move(0, 1.0);
}
if (prey1.getPosition().y < pred1.getPosition().y)
{
pred1.move(0, -1.0);
}
//logic for prey simple evade **MOVE TO FUNCTION**
if (prey1.getPosition().x > pred1.getPosition().x)
{
prey1.move(0.9, 0);
}
if (prey1.getPosition().x < pred1.getPosition().x)
{
prey1.move(-0.9, 0);
}
if (prey1.getPosition().y > pred1.getPosition().y)
{
prey1.move(0, 0.9);
}
if (prey1.getPosition().y < pred1.getPosition().y)
{
prey1.move(0, -0.9);
}
if ((pred1Box.intersects(prey1Box)))
{
prey1.setPosition(x, y);
}
predWall(pred1, topBox, botBox, leftBox, rightBox, pred1Box);
preyWall(prey1, topBox, botBox, leftBox, rightBox, prey1Box);
window.clear();
window.draw(prey1);
window.draw(pred1);
window.display();
}
return 0;
}
void simplechase(Prey prey1, predator pred1)
{
if (prey1.getPosition().x > pred1.getPosition().x)
{
pred1.move(1, 0);
}
if (prey1.getPosition().x < pred1.getPosition().x)
{
pred1.move(-1, 0);
}
if (prey1.getPosition().y > pred1.getPosition().y)
{
pred1.move(0, 1);
}
if (prey1.getPosition().y < pred1.getPosition().y)
{
pred1.move(0, -1);
}
}
void predWall(predator & pred1, FloatRect & topBox, FloatRect & botBox, FloatRect & leftBox, FloatRect & rightBox, FloatRect & pred1Box)
{
if ((pred1Box.intersects(topBox)))
{
cout << "Pred:Hit top" << endl;
pred1.move(0, 1);
}
if ((pred1Box.intersects(botBox)))
{
cout << "Pred:Hit bot" << endl;
pred1.move(0, -1);
}
if ((pred1Box.intersects(rightBox)))
{
cout << "Pred:Hit right" << endl;
pred1.move(-1, 0);
}
if ((pred1Box.intersects(leftBox)))
{
cout << "Pred:Hit left" << endl;
pred1.move(1, 0);
}
}
void preyWall(Prey & prey1, FloatRect & topBox, FloatRect & botBox, FloatRect & leftBox, FloatRect & rightBox, FloatRect & prey1Box)
{
if ((prey1Box.intersects(topBox)))
{
cout << "Prey:Hit top" << endl;
prey1.move(0, 0.9);
}
if ((prey1Box.intersects(botBox)))
{
cout << "Prey:Hit bot" << endl;
prey1.move(0, -0.9);
}
if ((prey1Box.intersects(rightBox)))
{
cout << "Prey:Hit right" << endl;
prey1.move(-0.9, 0);
if ((prey1Box.intersects(topBox)))
{
prey1.move(-1, 1);
}
}
if ((prey1Box.intersects(leftBox)))
{
cout << "Prey:Hit left" << endl;
prey1.move(0.9, 0);
if ((prey1Box.intersects(topBox)))
{
prey1.move(1, 1);
}
}
}
/********YET TO IMPLEMENT*******
* Corner Control
* A* pathing
* Functionalizing
* Clean up Classes, add more functionallity
* Implement Classes in a better way
*/