-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurpleQueen.as
120 lines (106 loc) · 3.71 KB
/
PurpleQueen.as
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
import messagingSystem;
import StateMachineClass;
import PatrolPoint;
import void ChangeToPatrolState(APurpuleQueen queen) from "QueenStates.QPatrolState";
import void ChangeToDeadState(APurpuleQueen queen) from "QueenStates.QDeadState";
import void ChangeToIdleState(APurpuleQueen queen) from "QueenStates.QIdleState";
class APurpuleQueen : AMessageCharacter
{
float health = 10;
stateMachine fsm;
/// Patrolling ---------------------
int currentPatrollPoint = 0;
UPROPERTY(EditAnywhere, Category = "Patrol")
TArray<APatrolPoint> patrollPoints;
// Determines how far from a patrol point the guard will accelerate to and from a halt.
UPROPERTY(EditAnywhere, Category = "Patrol")
float distanceToStartTurning = 100.f;
// Seconds before turning and walking to next patrol point
UPROPERTY(EditAnywhere, Category = "Patrol")
float timeToStandStill = 2.f;
UPROPERTY(EditAnywhere, Category = "Patrol")
float rotationSpeed = 0.7;
/// Animations -------------------
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Animation")
bool isTurning;
default isTurning = false;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Animation")
bool isTurningLeft;
default isTurningLeft = false;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Animation")
bool isTurningRight;
default isTurningRight = false;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Animation")
bool isDying;
default isDying = false;
APurpuleQueen()
{
fsm = stateMachine(this);
}
UFUNCTION(BlueprintOverride)
void BeginPlay()
{
ChangeToPatrolState(this);
}
UFUNCTION(BlueprintOverride)
void Tick(float DeltaSeconds)
{
fsm.Update(DeltaSeconds);
}
void handleMessage(Telegram telegram)// override
{
if (telegram.msg == messegeEnum::Damage)
{
health -= 10.f;
if (health <= 0)
{
ChangeToDeadState(this);
//endLevel();
return;
}
//Print("health: " + health);
}
}
UFUNCTION(BlueprintEvent)
void endLevel(){
}
// Must be used in update/execute/tick. Returns true when rotation is done.
bool turn(FRotator newRotation, float DeltaSeconds)
{
// if yaw is negative, convert it to positive (should become a value between 180 and 359)
float newRotationYaw = newRotation.Yaw;
if(newRotation.Yaw < 0)
newRotationYaw = 360 + newRotation.Yaw;
float actorYaw = ActorRotation.Yaw;
if(ActorRotation.Yaw < 0)
actorYaw = 360 + ActorRotation.Yaw;
// if true, turn right. If false, turn left.
if(((actorYaw - newRotationYaw + 360) % 360) > 180) {
isTurningRight = true;
ActorRotation += FRotator(0, rotationSpeed, 0); // right
}
else{
isTurningLeft = true;
ActorRotation -= FRotator(0, rotationSpeed, 0); // left
}
// if difference in rotation is < 1 degrees, return true ❤️
if(ActorRotation.Equals(newRotation, 1))
return true;
else
return false;
}
// Must be used in update/execute/tick. Returns true when rotation is done.
bool turn(AActor destination, float DeltaSeconds)
{
// make it actually turn towards the destination
FVector newLocation = destination.ActorLocation - ActorLocation;
FRotator newRotation = FRotator(0, newLocation.Rotation().Yaw, 0);
return turn(newRotation, DeltaSeconds);
}
UFUNCTION(BlueprintCallable)
void lookAtPlayer()
{
ChangeToIdleState(this);
isTurning = true;
}
}