-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cs
More file actions
94 lines (80 loc) · 2.9 KB
/
Enemy.cs
File metadata and controls
94 lines (80 loc) · 2.9 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
using State.State;
using System;
namespace State
{
public class Enemy
{
public string Name { get; private set; }
public float Health { get; private set; }
public float DetectionRange { get; private set; }
public float AttackRange { get; private set; }
public int Damage { get; private set; }
public float MoveSpeed { get; private set; }
public float X { get; private set; } = 0;
public float Y { get; private set; } = 0;
public IdleState IdleState { get; private set; }
public ChaseState ChaseState { get; private set; }
public AttackState AttackState { get; private set; }
private EnemyState currentState;
public Enemy(string name, float health, float detectionRange, float attackRange,int damage, float moveSpeed)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Health = health;
DetectionRange = detectionRange;
AttackRange = attackRange;
Damage = damage;
MoveSpeed = moveSpeed;
IdleState = new IdleState(this);
ChaseState = new ChaseState(this);
AttackState = new AttackState(this);
currentState = IdleState;
currentState.Enter();
}
public void ChangeState(EnemyState newState)
{
currentState.Exit();
currentState = newState;
currentState.Enter();
}
public void Update(Player player)
{
currentState.Update(player);
}
public void MoveTowardsPlayer(Player player)
{
float deltaX = player.X - X;
float deltaY = player.Y - Y;
float distance = (float)Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > 0.01f)
{
float step = Math.Min(MoveSpeed / 2, distance);
X += (deltaX / distance) * step;
Y += (deltaY / distance) * step;
}
}
public void AttackPlayer(Player player)
{
player.TakeDamage(Damage);
}
public float GetDistanceToPlayer(Player player)
{
double deltaX = player.X - X;
double deltaY = player.Y - Y;
return (float)Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
}
public string CurrentStateName()
{
switch (currentState)
{
case IdleState IdleState:
return "Покой";
case ChaseState ChaseState:
return "Преследование";
case AttackState AttackState:
return "Атака";
default:
return "Unknown";
}
}
}
}