-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyShip.cs
117 lines (99 loc) · 3.24 KB
/
EnemyShip.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
using System;
using System.Drawing;
using AsteroidsGdiApp.Core;
namespace AsteroidsGdiApp.GameObjects
{
public class EnemyShip : IGameObject
{
Point _location = new Point(-40, -40);
Sprite _shipSprite;
private DateTime _startTime;
private float _deltaX = -3;
private float _deltaY;
readonly Random _random = new Random();
public EnemyShip()
{
_location = new Point(-40, -40);
}
public Point Location
{
get
{
return _location;
}
}
public void Activate()
{
_shipSprite = new Sprite();
_shipSprite.Polygon = new[]
{
new Point(0, 0),
new Point(30, 0),
new Point(30, 20),
new Point(0, 20),
new Point(0, 0)};
_startTime = DateTime.Now;
_location = new Point(Constants.CanvasWidth, 60);
}
public bool IsActive
{
get
{
return _location.X > 0;
}
}
public void Update()
{
if (DateTime.Now.Subtract(_startTime).TotalMilliseconds > 750)
{
int randomValue = _random.Next(3);
if (randomValue % 2 == 0)
{
_deltaY = 3;
}
else if(randomValue % 3 == 0)
{
_deltaY = -3;
}
_startTime = DateTime.Now;
}
_location.X += (int)_deltaX;
_location.Y += (int)_deltaY;
if (_location.Y < 0)
_deltaY = 3;
}
public void Draw(Graphics graphics)
{
if (!IsActive)
return;
CreateShip();
DrawShip(graphics);
}
private void DrawShip(Graphics graphics)
{
graphics.DrawPolygon(Pens.White, _shipSprite.Polygon);
graphics.DrawLine(Pens.White, _location.X - 20, _location.Y, _location.X + 20, _location.Y);
}
private void CreateShip()
{
_shipSprite.Polygon = new[]
{
new Point(_location.X - 5, _location.Y-10),
new Point(_location.X+5, _location.Y-10),
new Point(_location.X+15, _location.Y),
new Point(_location.X+5, _location.Y+10),
new Point(_location.X-5, _location.Y+10),
new Point(_location.X-15, _location.Y),
new Point(_location.X-5, _location.Y-10)
};
}
internal bool IsPointWithin(Point point)
{
return _shipSprite.IsPointWithin(point);
}
internal void Inactivate()
{
_location.X = -100;
}
}
}