-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine_AIComponents.cpp
155 lines (138 loc) · 3.23 KB
/
Engine_AIComponents.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 "stdafx.h"
#include "Map.h"
#include <iostream>
#include "Constants.h"
#include "Player.h"
#include "Enumerations.h"
#include "GeneralFunctions.h"
#include "Enemy.h"
#include "Engine.h"
#include "Structs.h"
void Engine::Patrol(float ElapsedTime, EnemyObject &enemy, sf::Vector2f point1, sf::Vector2f point2)
{
float leftPoint;
float rightPoint;
float destination;
if (point1.x < point2.x)
{
leftPoint = point1.x;
rightPoint = point2.x;
}
else
{
leftPoint = point2.x;
rightPoint = point1.x;
}
switch (enemy.GetState().Facing)
{
case RIGHT:
destination = rightPoint;
if (enemy.GetPosition().x < destination)
{
enemy.MoveRight(ElapsedTime);
}
else
{
enemy.flo_VisionAngle -= 180;
}
break;
case LEFT:
destination = leftPoint;
if (enemy.GetPosition().x > destination)
{
enemy.MoveLeft(ElapsedTime);
}
else
{
enemy.flo_VisionAngle += 180;
}
break;
}
}
void Engine::Flee(float ElapsedTime, EnemyObject &enemy, float playerAngle)
{
if (playerAngle > 0 && playerAngle < 180)
{
enemy.MoveRight(ElapsedTime);
}
else
{
enemy.MoveLeft(ElapsedTime);
}
}
void Engine::Chase(float ElapsedTime, EnemyObject &enemy, float playerAngle)
{
if (playerAngle > 0 && playerAngle < 180)
{
enemy.MoveLeft(ElapsedTime);
}
else
{
enemy.MoveRight(ElapsedTime);
}
}
void Engine::AttackPlayer(float ElapsedTime, EnemyObject &enemy, Attack attack, Player &player)
{
if (enemy.sta_Current.Shooting == false && enemy.sta_Current.Meleeing == false)
{
enemy.EngageAttack(attack, player);
}
enemy.ProcessAttack(ElapsedTime, attack);
}
bool Engine::CheckWalls(EnemyObject enemy, Player player)
{
// This function tests the intersection of the line between enemy and player against the line of a
// collisions trueA vector and trueB vector. This function should return true if no single barrier
// hinders sight of the enemy.
bool output = true;
LinearFunc Enemy_Player = LinearOf(enemy.GetPosition(), player.GetPosition());
for (int count = 0; count < map_Selected.int_NumColl; count++)
{
if (DetectObstruction(enemy, player, Enemy_Player, map_Selected.col_CollisionData[count]) == true)
{
return false;
}
}
return output;
}
bool Engine::DetectObstruction(EnemyObject enemy, Player player, LinearFunc enemy_Player, CollisionObject barrier)
{
// This function tests a single collision object against the positions of a player and enemy to detect if it is in the way.
// This function will return false if there was no obstruction.
bool output;
LinearFunc CollisionPoints = LinearOf(barrier.vec_TrueA,
barrier .vec_TrueB);
sf::Vector2f intersect = IntersectOf(enemy_Player, CollisionPoints);
if (intersect.x >= 0 && intersect.y >= 0)
{
if (intersect.x >= barrier.vec_TrueA.x && intersect.x <= barrier.vec_TrueB.x)
{
if (intersect.y >= barrier.vec_TrueA.y && intersect.y <= barrier.vec_TrueB.y)
{
float playerDistance = DistanceOf(enemy.GetPosition(), player.GetPosition());
float intersectDis = DistanceOf(enemy.GetPosition(), intersect);
if (intersectDis < playerDistance)
{
output = true;
}
else
{
output = false;
}
}
else
{
output = false;
}
}
else
{
output = false;
}
}
else
{
output = false;
}
return output;
}