-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cs
203 lines (184 loc) · 5.63 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My90Tank.Properties;
using System.Drawing;
namespace My90Tank
{
public class Enemy:Member
{
private const int BORNTIME = 24;
public static Image[] bornImages = new Image[] { Resources.born1, Resources.born2, Resources.born3, Resources.born4 };
private static TankType[] tankType = new TankType[] { new TankType(6, 2, 2), new TankType(12, 1, 2), new TankType(4,6, 2) };
private static Image[] imgEnemy1 = new Image[] //加载一号敌人图片
{
Resources.enemy1U,
Resources.enemy1D,
Resources.enemy1L,
Resources.enemy1R
};
private static Image[] imgEnemy2 = new Image[] //加载二号敌人图片
{
Resources.enemy2U,
Resources.enemy2D,
Resources.enemy2L,
Resources.enemy2R
};
private static Image[] imgEnemy3 = new Image[] //加载三号敌人图片
{
Resources.enemy3U,
Resources.enemy3D,
Resources.enemy3L,
Resources.enemy3R
};
private EnemyType type; //定义敌人的类型
public EnemyType Type //设置属性
{
get { return type; }
set { type = value; }
}
public static int GetLife(EnemyType type)
{
return tankType[(int)type].life;
}
public static int GetSpeed(EnemyType type)
{
return tankType[(int)type].speed;
}
public static int GetPower(EnemyType type)
{
return tankType[(int)type].power;
}
public Enemy(EnemyType type, int x, int y, DIRECTION direct)
: base(x, y, GetLife(type), GetSpeed(type), GetPower(type), direct)
{
this.type = type;
this.Width = 60;
this.Height = 60;
}
public override void Move()
{
if (IsBlocked)
{
ChangeDirection();
}
else
{
base.ChangePosition();
}
}
public void ChangeDirection()
{
Random rand = new Random((int)DateTime.Now.Ticks);
int t = rand.Next(4);
this.direct = (DIRECTION)t;
}
public override void Draw(Graphics g)
{
//如果是出生状态,则需要出生时的闪烁图像序列;
if (bornTime < BORNTIME)
{
switch (bornTime % 8)
{
case 0:
case 1:
g.DrawImage(bornImages[0], this.X, this.Y);
break;
case 2:
case 3:
g.DrawImage(bornImages[1], this.X, this.Y);
break;
case 4:
case 5:
g.DrawImage(bornImages[2], this.X, this.Y);
break;
case 6:
case 7:
g.DrawImage(bornImages[3], this.X, this.Y);
break;
}
bornTime++;
return;
}
Image[] images = imgEnemy2;
//if (type == EnemyType.FAST)
//{
// images = imgEnemy2;
//}
if (type == EnemyType.HEAVY)
{
images = imgEnemy3;
}
else if (type == EnemyType.NORMAL)
{
images = imgEnemy1;
}
Move();
Fire();
switch (direct)
{
case DIRECTION.UP:
g.DrawImage(images[0], this.X, this.Y);
break;
case DIRECTION.DOWN:
g.DrawImage(images[1], this.X, this.Y);
break;
case DIRECTION.LEFT:
g.DrawImage(images[2], this.X, this.Y);
break;
case DIRECTION.RIGHT:
g.DrawImage(images[3], this.X, this.Y);
break;
default:
break;
}
}
public void Fire()
{
Random r = new Random((int)DateTime.Now.Ticks);
int a = r.Next(50);
if (a < 48)
return;
int xx = this.X, yy = this.Y;
switch (this.direct)
{
case DIRECTION.UP:
yy -= this.speed;
xx += this.Width / 2;
break;
case DIRECTION.DOWN:
yy += this.speed + this.Height;
xx += this.Width / 2;
break;
case DIRECTION.LEFT:
xx -= this.speed;
yy += this.Width / 2;
break;
case DIRECTION.RIGHT:
xx += this.speed + this.Width;
yy += this.Width / 2;
break;
default:
break;
}
Scene.Instance.AddElement(new Missile(xx, yy, 1, 25, this.power, this.direct));
}
}
public struct TankType
{
public int speed;
public int life;
public int power;
public TankType(int speed, int life, int power)
{
this.speed = speed;
this.life = life;
this.power = power;
}
}
public enum EnemyType
{
NORMAL=1,FAST=2,HEAVY=3
}
}