-
Notifications
You must be signed in to change notification settings - Fork 1
/
Controller.cs
172 lines (148 loc) · 5.98 KB
/
Controller.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
171
172
using System;
using System.Collections.Generic;
using System.Drawing;
namespace Astroshooter
{
class Controller
{
private Ship ship;
private SpaceField spacefield;
private List<SpaceObject> spaceObjects;
private Random random = new Random((int)DateTime.Now.Ticks);
private List<Image> asteroidTextureList;
private Queue<SpaceObject> toSpawn = new Queue<SpaceObject>(100);
private double circularParameter = 0;
private readonly Dictionary<string, CachedSound> soundLibrary = new Dictionary<string, CachedSound>();
private int score = 0;
public Controller(Ship ship, SpaceField spacefield, List<SpaceObject> spaceObjects, List<Image> asteroidImages)
{
this.ship = ship;
this.spacefield = spacefield;
this.spaceObjects = spaceObjects;
asteroidTextureList = asteroidImages;
InitializeSoundLibrary();
}
private void InitializeSoundLibrary()
{
soundLibrary.Add("AsteroidExplode", new CachedSound(@".\audio\explode.wav"));
soundLibrary.Add("ShipShootSound", new CachedSound(@".\audio\impulse.wav"));
}
public void CreateBullet(float angle)
{
if (ship.cooldown <= 0)
{
var pelleteVelocity = new Vec2();
pelleteVelocity.X = -Math.Cos(angle / 180 * Math.PI);
pelleteVelocity.Y = -Math.Sin(angle / 180 * Math.PI);
spaceObjects.Add(new Bullet(ship.GetCurrentCoordinates(), pelleteVelocity));
AudioPlaybackEngine.Instance.PlaySound(soundLibrary["ShipShootSound"]);
ship.SetShootCooldown(300);
}
}
private void BoundaryCollisionHandle(SpaceObject obj)
{
var Location = obj.GetCoordinates();
if (Location.X < -64) obj.SetCurrentCoordinates(spacefield.Width, Location.Y);
if (Location.X > spacefield.Width + 64) obj.SetCurrentCoordinates(0, Location.Y);
if (Location.Y < -64) obj.SetCurrentCoordinates(ship.Location.X, spacefield.Height);
if (Location.Y > spacefield.Height + 64) obj.SetCurrentCoordinates(Location.X, -32);
}
void Collision(SpaceObject spaceobject)
{
for(int i = 0; i < spaceObjects.Count; i++)
{
if(spaceobject != null && spaceObjects[i] != null)
{
if (spaceobject != spaceObjects[i])
CollisionCheck(spaceObjects[i], spaceobject);
CollisionCheck(spaceObjects[i], ship);
}
}
}
public void CollisionCheck(SpaceObject left, SpaceObject right)
{
if (left.IsCollided(right))
{
if(left is Asteroid)
{
var leftaster = left as Asteroid;
leftaster.Collide(right);
}
}
}
private Queue<int> toDeleteQ = new Queue<int>(32);
private List<int> toDelete = new List<int>(512);
void SimulateSpaceObjectsTimeFrame(double dt)
{
BoundaryCollisionHandle(ship);
for(int i = 0; i < spaceObjects.Count; i++)
{
spaceObjects[i].SimulateTimeFrame(dt);
BoundaryCollisionHandle(spaceObjects[i]);
Collision(spaceObjects[i]);
if (spaceObjects[i].IsDead())
toDelete.Add(i);
}
for(int i = 0; i < toDelete.Count; i++)
{
if(spaceObjects[toDelete[i] - i] is Asteroid)
{
if (spacefield.isSoundEnabled)
AudioPlaybackEngine.Instance.PlaySound(soundLibrary["AsteroidExplode"]);
score += 100;
spacefield.UpdateScore(score);
}
spaceObjects[toDelete[i] - i] = spaceObjects[spaceObjects.Count - 1];
spaceObjects.RemoveAt(spaceObjects.Count - 1);
}
toDelete.Clear();
while (toSpawn.Count != 0)
spaceObjects.Add(toSpawn.Dequeue());
}
public void UpdateSimulation(double dt)
{
SimulateSpaceObjectsTimeFrame(dt);
}
private Vec2 GetRandomPositionOutsidePlayzone()
{
var xcord = random.Next() * 1920 % 30000;
var ycord = random.Next() * 1080 % 30000;
return new Vec2(xcord, ycord);
}
private Vec2 GetRandomVelocity()
{
var xvel = random.Next(-10, 10) / 100d;
var yvel = random.Next(-10, 10) / 100d;
return new Vec2(xvel, yvel);
}
public void CreateRandomAsteroid() => CreateAsteroidInRadius(2000);
public void CreateAsteroidInRadius(double spawnRadius)
{
circularParameter %= Math.PI * 2;
if(spaceObjects.Count <= 32 && toSpawn.Count <= 16)
{
circularParameter += Math.PI / 6;
Vec2 pos = new Vec2()
{
X = spawnRadius * Math.Cos(circularParameter) + spacefield.Width / 2,
Y = spawnRadius * Math.Sin(circularParameter) + spacefield.Height / 2
};
Vec2 vel = GetRandomVelocity();
var asteroid = new Asteroid(pos, vel * 3, asteroidTextureList[random.Next(0, asteroidTextureList.Count - 1)]);
asteroid.SetCooldown(300);
toSpawn.Enqueue(asteroid);
}
}
public void Respawn()
{
if (ship.IsDead())
{
ship.SetDeadState(false);
score = 0;
spacefield.UpdateScore(score);
ship.SetCurrentCoordinates(spacefield.Width / 2, spacefield.Height / 2);
spaceObjects.Add(ship);
}
}
}
}