/
Enemy.cs
168 lines (138 loc) · 5.38 KB
/
Enemy.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
//---------------------------------------------------------------------------------
// Ported to the Atomic Game Engine
// Originally written for XNA by Michael Hoffman
// Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
//----------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using AtomicEngine;
namespace AtomicBlaster
{
class Enemy : Entity
{
public static Random rand = new Random();
private List<IEnumerator<int>> behaviours = new List<IEnumerator<int>>();
private int timeUntilStart = 60;
public bool IsActive { get { return timeUntilStart <= 0; } }
public int PointValue { get; private set; }
public Enemy(CustomSprite image, Vector2 position)
{
this.image = image;
Position = position;
Radius = image.Width / 2f;
color = Color.Transparent;
PointValue = 1;
}
public static Enemy CreateSeeker(Vector2 position)
{
var enemy = new Enemy(Art.Seeker, position);
enemy.AddBehaviour(enemy.FollowPlayer(0.9f));
enemy.PointValue = 2;
return enemy;
}
public static Enemy CreateWanderer(Vector2 position)
{
var enemy = new Enemy(Art.Wanderer, position);
enemy.AddBehaviour(enemy.MoveRandomly());
return enemy;
}
public override void Update()
{
if (timeUntilStart <= 0)
ApplyBehaviours();
else
{
timeUntilStart--;
float mul = (1 - timeUntilStart / 60f);
color = new Color(mul, mul, mul, mul);
}
Position += Velocity;
Position = Vector2.Clamp(Position, Size / 2, GameRoot.ScreenSize - Size / 2);
Velocity *= 0.8f;
}
public override void Draw(/*SpriteBatch spriteBatch*/)
{
if (timeUntilStart > 0)
{
// Draw an expanding, fading-out version of the sprite as part of the spawn-in effect.
float factor = timeUntilStart / 60f; // decreases from 1 to 0 as the enemy spawns in
CustomRenderer.Draw(image, Position, Color.White * factor, Orientation, Size / 2f, 2 - factor, 0);
}
base.Draw();
}
private void AddBehaviour(IEnumerable<int> behaviour)
{
behaviours.Add(behaviour.GetEnumerator());
}
private void ApplyBehaviours()
{
for (int i = 0; i < behaviours.Count; i++)
{
if (!behaviours[i].MoveNext())
behaviours.RemoveAt(i--);
}
}
public void HandleCollision(Enemy other)
{
var d = Position - other.Position;
Velocity += 10 * d / (d.LengthSquared + 1);
}
public void WasShot()
{
IsExpired = true;
PlayerStatus.AddPoints(PointValue);
PlayerStatus.IncreaseMultiplier();
float hue1 = rand.NextFloat(0, 6);
float hue2 = (hue1 + rand.NextFloat(0, 2)) % 6f;
Color color1 = ColorUtil.HSVToColor(hue1, 0.5f, 1);
Color color2 = ColorUtil.HSVToColor(hue2, 0.5f, 1);
for (int i = 0; i < 120; i++)
{
float speed = 18f * (1f - 1 / rand.NextFloat(1, 10));
var state = new ParticleState()
{
Velocity = rand.NextVector2(speed, speed),
Type = ParticleType.Enemy,
LengthMultiplier = 1
};
Color color = Color.Lerp(color1, color2, rand.NextFloat(0, 1));
GameRoot.ParticleManager.CreateParticle(Art.LineParticle, Position, color, 190, 1.5f, state);
}
//Sound.Explosion.Play(0.5f, rand.NextFloat(-0.2f, 0.2f), 0);
}
#region Behaviours
IEnumerable<int> FollowPlayer(float acceleration)
{
while (true)
{
if (!PlayerShip.Instance.IsDead)
Velocity += (PlayerShip.Instance.Position - Position).ScaleTo(acceleration);
if (Velocity != Vector2.Zero)
Orientation = Velocity.ToAngle();
yield return 0;
}
}
IEnumerable<int> MoveRandomly()
{
float direction = rand.NextFloat(0, MathHelper.TwoPi);
while (true)
{
direction += rand.NextFloat(-0.1f, 0.1f);
direction = MathHelper.WrapAngle(direction);
for (int i = 0; i < 6; i++)
{
Velocity += MathUtil.FromPolar(direction, 0.4f);
Orientation -= 0.05f;
var bounds = GameRoot.ScreenBounds;
bounds.Inflate(-image.Width / 2 - 1, -image.Height / 2 - 1);
// if the enemy is outside the bounds, make it move away from the edge
if (!bounds.Contains(Position))
direction = (GameRoot.ScreenSize / 2 - Position).ToAngle() + rand.NextFloat(-MathHelper.PiOver2, MathHelper.PiOver2);
yield return 0;
}
}
}
#endregion
}
}