-
Notifications
You must be signed in to change notification settings - Fork 33
/
Tower.cs
170 lines (149 loc) · 5.03 KB
/
Tower.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
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
using System;
using System.Collections.Generic;
using CreepyTowers.Content;
using CreepyTowers.Effects;
using CreepyTowers.Enemy.Creeps;
using CreepyTowers.Stats;
using DeltaEngine.Content;
using DeltaEngine.Datatypes;
using DeltaEngine.Entities;
using DeltaEngine.Extensions;
using DeltaEngine.Multimedia;
using DeltaEngine.Rendering3D;
using DeltaEngine.Rendering3D.Particles;
using DeltaEngine.Rendering3D.Shapes;
namespace CreepyTowers.Towers
{
/// <summary>
/// Towers shoot at creeps. Their stats and behavior vary depending on their type
/// </summary>
public sealed class Tower : Agent
{
public Tower(TowerType type, Vector3D position, float rotationZ = 0.0f)
{
Type = type;
CreateStats(type, ContentLoader.Load<TowerPropertiesXml>(Xml.TowerProperties.ToString()));
Position = position;
ScaleFactor = 1.0f;
RotationZ = rotationZ;
SetMultipliersFromAvatar();
Cooldown = 1 / GetStatValue("AttackFrequency");
}
private float Cooldown { get; set; }
public TowerType Type { get; private set; }
public bool IsOnCooldown
{
get { return Cooldown > 0; }
}
private void CreateStats(TowerType type, TowerPropertiesXml properties)
{
TowerData towerData = properties.Get(type);
Name = towerData.Name;
AttackType = towerData.AttackType;
CreateStat("Range", towerData.Range);
CreateStat("AttackFrequency", towerData.AttackFrequency);
CreateStat("Power", towerData.BasePower);
CreateStat("Cost", towerData.Cost);
}
public AttackType AttackType { get; private set; }
private void SetMultipliersFromAvatar()
{
var avatar = Player.Current.Avatar;
ApplyBuff(new BuffEffect(avatar.GetType().Name + "AttackFrequencyMultiplier"));
ApplyBuff(new BuffEffect(avatar.GetType().Name + "RangeMultiplier"));
ApplyBuff(new BuffEffect(avatar.GetType().Name + "PowerMultiplier"));
}
protected override void InitializeModel()
{
ContentLoader.Load<Sound>("Tower" + Type + "Built").Play();
GetAndAttachAttackEffect();
//RenderDebugAttackRange();
if (WasBuilt != null)
WasBuilt();
}
public static event Action WasBuilt;
private void GetAndAttachAttackEffect()
{
if (AttackType == AttackType.DirectShot)
return;
attackEffect = EffectLoader.GetAttackEffect(Type, AttackType);
AddChild(attackEffect);
}
public ParticleSystem attackEffect;
private void RenderDebugAttackRange()
{
var range = GetStatValue("Range");
if (AttackType == AttackType.Circle)
DebugRange.Add(new Circle3D(Position, range, Color.Red));
else if (AttackType == AttackType.DirectShot)
DebugRange.Add(new Line3D(Position, Position + Vector3D.UnitX * range, Color.Red));
else
{
var leftPoint = (Vector3D.UnitX * range).RotateAround(Vector3D.UnitZ, 15);
var rightPoint = (Vector3D.UnitX * range).RotateAround(Vector3D.UnitZ, -15);
DebugRange.Add(new Line3D(Position, Position + leftPoint, Color.Red));
DebugRange.Add(new Line3D(Position, Position + rightPoint, Color.Red));
}
}
public readonly List<Entity3D> DebugRange = new List<Entity3D>();
protected override void OnOrientationChanged()
{
if (Model != null)
Model.Orientation = Orientation;
base.OnOrientationChanged();
}
public override void Update()
{
base.Update();
if (Cooldown > 0)
Cooldown -= Time.Delta;
}
public void FireAtCreep(Creep creep)
{
PlayFireSound();
VisualizeAttackDependingOnType(creep);
TimeOfLastAttack = Time.Total;
Cooldown = 1 / GetStatValue("AttackFrequency");
}
public float TimeOfLastAttack { get; private set; }
private void PlayFireSound()
{
string fileName = "Tower" + Type + "Attack";
var sound = ContentLoader.Load<Sound>(fileName);
sound.Play();
}
private void VisualizeAttackDependingOnType(Creep targetCreep)
{
if (attackEffect != null)
attackEffect.FireBurstOfAllEmitters(); //ncrunch: no coverage
if (AttackType == AttackType.DirectShot || Type == TowerType.Slice)
new InterpolatingProjectile(this, targetCreep,
EffectLoader.GetProjectileEffect(Type, AttackType.DirectShot));
}
public void AimToTarget(Vector3D target)
{
var direction = target - Position;
RotationZ = MathExtensions.Atan2(direction.Y, direction.X) + 90;
//if (AttackType == AttackType.DirectShot)
// ((Line3D)DebugRange[0]).EndPoint = target;
//else if (AttackType == AttackType.Cone)
// AddConeDebugLines(direction);
}
private void AddConeDebugLines(Vector3D direction)
{
direction.Normalize();
var range = GetStatValue("Range");
var leftPoint = (direction * range).RotateAround(Vector3D.UnitZ, 15);
var rightPoint = (direction * range).RotateAround(Vector3D.UnitZ, -15);
((Line3D)DebugRange[0]).EndPoint = Position + leftPoint;
((Line3D)DebugRange[1]).EndPoint = Position + rightPoint;
}
public override void Dispose()
{
foreach (var entity in DebugRange)
entity.Dispose();
DebugRange.Clear();
base.Dispose();
}
}
}