-
Notifications
You must be signed in to change notification settings - Fork 4
/
base_behavior.cs
105 lines (79 loc) · 2.35 KB
/
base_behavior.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class base_behavior : MonoBehaviour
{
public int team;
//links to the different behavior components
public idle_script idle;
public seek_script seek;
public attack_script attack;
//gps is our general pathfinding script
//public general_pathfinding gps;
//intelligent movement scripts
public Agent agentScript;
public Seek seekScript;
public Flee fleeScript;
public float maxSpeed;
public GameObject target;
public UnitFSM state;
public enum UnitFSM //states
{
Attack,
Seek,
Idle
}
// Start is called before the first frame update
void Start()
{
agentScript = gameObject.AddComponent<Agent>(); //add agent
agentScript.maxSpeed = maxSpeed;
changeState(UnitFSM.Idle);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space"))
{
if(state == UnitFSM.Idle)
{
changeState(UnitFSM.Seek);
}
else
{
changeState(UnitFSM.Idle);
}
}
}
public void changeState(UnitFSM new_state)
{
state = new_state;
switch (new_state)
{
case UnitFSM.Idle:
if(gameObject.GetComponent<idle_script>() == null)
{
idle = gameObject.AddComponent<idle_script>();
}
DestroyImmediate(seek);
DestroyImmediate(attack);
break;
case UnitFSM.Seek:
if (gameObject.GetComponent<seek_script>() == null)
{
seek = gameObject.AddComponent<seek_script>();
}
DestroyImmediate(idle);
DestroyImmediate(attack);
break;
case UnitFSM.Attack:
if (gameObject.GetComponent<attack_script>() == null)
{
attack = gameObject.AddComponent<attack_script>();
}
DestroyImmediate(seek);
DestroyImmediate(idle);
break;
}
}
}