-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathEnemy.java
More file actions
108 lines (93 loc) · 1.83 KB
/
Enemy.java
File metadata and controls
108 lines (93 loc) · 1.83 KB
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
package com.espol.mathhero2;
import java.awt.*;
public abstract class Enemy
{
protected double r,t,speed;
protected Color color = Color.BLACK;
protected int radius = 5;
private boolean dying1 = false;
private boolean dying2 = false;
private boolean dead = false;
private int alpha = 255;
private int arrowR = Util.PLAYER_RADIUS+Util.ARROW_LENGTH;
public Enemy(double speed)
{
this.speed = speed;
r = Util.MAX_R;
t = Math.random()*Math.PI*2;
}
public int x()
{
return Util.MAX_R+(int)(Math.cos(t)*r+.5);
}
public int y()
{
return Util.MAX_R+(int)(Math.sin(t)*r+.5);
}
private int arrowX1()
{
return Util.MAX_R+(int)(Math.cos(t)*arrowR+.5);
}
private int arrowY1()
{
return Util.MAX_R+(int)(Math.sin(t)*arrowR+.5);
}
private int arrowX2()
{
return Util.MAX_R+(int)(Math.cos(t)*(arrowR-Util.ARROW_LENGTH)+.5);
}
private int arrowY2()
{
return Util.MAX_R+(int)(Math.sin(t)*(arrowR-Util.ARROW_LENGTH)+.5);
}
public void die()
{
dying1 = true;
}
public boolean dead()
{
return dead;
}
public boolean hitting()
{
return r == Util.PLAYER_RADIUS+radius && !dying1;
}
public void update()
{
if(!dying2)
{
r-=speed;
if(r<Util.PLAYER_RADIUS+radius)
r = Util.PLAYER_RADIUS+radius;
}
if(dying2)
{
alpha/=1.1;
if(alpha==0)
dead = true;
}
else if(dying1)
{
arrowR+=Util.ARROW_SPEED;
if(arrowR>=r)
dying2 = true;
}
}
public abstract String getProblem();
public abstract int getSolution();
public void draw(Graphics g)
{
g.setColor(color);
g.drawString(getProblem(), x()-radius, y()-radius);
g.fillOval(x()-radius, y()-radius, radius*2, radius*2);
if(dying1)
{
g.setColor(Color.BLACK);
g.drawLine(arrowX1(),arrowY1(),arrowX2(),arrowY2());
}
if(dying2)
{
color = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
}
}
}