-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diese.cs
252 lines (212 loc) · 6.38 KB
/
Diese.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Drawing;
using System.Collections.Generic;
namespace diese
{
public abstract class Actor
{
public double X;
public double Y;
public double Width;
public double Height;
public int Speed;
public Image Image;
public double Angle;
public bool Dead = false;
public int Life {
get { return life; }
set {
life = value;
if (life < 1)
{
Dead = true;
}
}
}
private int life;
public double Degree
{
get { return Angle * 180 / Math.PI; }
set { Angle = value * Math.PI / 180.0; }
}
public virtual void Draw(Graphics gr)
{
var state = gr.Save();
gr.TranslateTransform((float)X, (float)Y);
gr.RotateTransform((float)(Degree));
gr.DrawImage(Image, (int)(-Width / 2.0), (int)(-Height / 2.0), (int)Width, (int)Height);
gr.Restore(state);
}
public bool CollisionWith(Actor other)
{
// Dummy algorith using the distance method.
var min = Math.Min(Width, Height);
var minOther = Math.Min(other.Width, other.Height);
var distance = Math.Sqrt(Math.Pow(other.X - X, 2) + Math.Pow(other.Y - Y, 2));
return distance < (min + minOther) / 2f;
}
public abstract void Tick();
}
public class Diese : Actor
{
private readonly List<Operation> moves;
private readonly IDictionary<string, Image> images;
private readonly MainForm mainForm;
public Diese(MainForm main, IDictionary<string, Image> assets)
: base()
{
Speed = 5;
Angle = 0;
moves = new List<Operation>();
mainForm = main;
images = assets;
}
public void Move(int dx, int dy)
{
moves.Add(new Move(dx, dy));
}
public void Shot(double angle)
{
moves.Add(new Shot()
{
X = (int)X,
Y = (int)Y,
Angle = angle
});
}
public override void Tick()
{
if (moves.Count == 0)
{
return;
}
var move = moves[0];
if (move is Move)
{
var m = (Move)move;
var speed = Math.Min(Speed, m.Length);
m.Shorten(Speed);
X += m.X * speed;
Y += m.Y * speed;
// Remove useless events.
if (m.Length <= 0)
{
moves.RemoveAt(0);
}
}
else if (move is Shot)
{
var b = (Shot)move;
b.Shorten(Speed);
if (!b.HasShot)
{
var image = images["tir"];
mainForm.AddBullet(new Bullet
{
X = X,
Y = Y,
Degree = b.Angle,
Image = image,
Width = image.Width,
Height = image.Height
});
b.HasShot = true;
}
if (b.Length <= 0)
{
moves.RemoveAt(0);
}
}
}
}
public class Bullet : Actor
{
public Bullet()
: base()
{
Speed = 5;
}
public override void Tick()
{
X += Math.Cos(Angle) * Speed;
Y += Math.Sin(Angle) * Speed;
}
public override void Draw(Graphics g)
{
Angle += Math.PI / 2f;
base.Draw(g);
Angle -= Math.PI / 2f;
}
}
public class Asteroid : Actor
{
public double Rotation;
public double AngularRotation;
private static Random seed = new Random();
public Image[] Images;
public Asteroid()
: base()
{
Speed = 2;
Rotation = 0;
Life = 1;
// Il faut seeder sinon toutes les valeurs sont identiques (car basé sur le temps).
var rnd = new Random(seed.Next());
AngularRotation = (rnd.NextDouble() - .5) * Math.Pow(rnd.NextDouble() + 1, 3);
}
public override void Tick()
{
Rotation = (Rotation + AngularRotation) % 360;
X += Math.Cos(Angle) * Speed;
Y += Math.Sin(Angle) * Speed;
}
public override void Draw(Graphics g)
{
Degree += Rotation;
Image = Images[Life - 1];
Width = Image.Width;
Height = Image.Height;
base.Draw(g);
Degree -= Rotation;
}
}
public class Blast : Actor
{
public int Frame;
public int FrameRate;
public Blast()
: base()
{
Frame = 0;
FrameRate = 2;
}
public override void Tick()
{
Frame++;
var maxFrame = FrameRate * (int)(Width / Height);
if (Frame > maxFrame)
{
Dead = true;
Frame = maxFrame;
}
}
public override void Draw(Graphics gr)
{
var state = gr.Save();
gr.TranslateTransform((float)X, (float)Y);
gr.RotateTransform((float)(Degree));
gr.DrawImage(
Image,
(int)(-Height / 2.0), (int)(-Height / 2.0),
new Rectangle(){
X = (int)(Height * (int)(Frame / FrameRate)),
Y = 0,
Width = (int)Height,
Height = (int)Height
},
GraphicsUnit.Pixel
);
gr.Restore(state);
}
}
}