-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bright.java
137 lines (132 loc) · 3.8 KB
/
Bright.java
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
import greenfoot.*;
public class Bright extends AnimatedActor
{
private int health;
private double posx;
private double poxy;
private double velocityX;
private double velocityY;
private int frameCounter = 0;
private int frameWait = 8;
private int cooldown;
public Bright()
{
super("spaceshipmotion", ".png", 6);
velocityX = 0;
velocityY = 0;
health = 3;
}
public void act()
{
((Universe)getWorld()).healthUpdate(health);
if(getX() < 10)
die();
if (cooldown <= 0)
{
if(velocityX <= -1)
{
super.changeImage("spaceshipidle", ".png", 6);
}
else
{
super.changeImage("spaceshipmotion", ".png", 6);
}
}
else
{
cooldown--;
GreenfootImage trans = getImage();
trans.setTransparency(120);
setImage(trans);
}
if(frameCounter >= frameWait)
{
super.act();
frameCounter = 0;
}
else
{
frameCounter++;
}
setLocation(getX() + (int)velocityX, getY() + (int)velocityY);
/* java.util.List l = getWorld().getObjects(Blackhole.class);
for (int i = 0; i < l.size(); i ++)
{
Actor blob = (Actor)l.get(i);
double ballX = (double)blob.getX();
double ballY = (double)blob.getY();
double diffX = (double)getX() - ballX;
double diffY = (double)getY() - ballY;
double distance = (diffX * diffX) + (diffY * diffY);
double aXMult = 10000.0/distance;
double aYMult = 13000.0/distance;
if (diffX < 0)
aXMult *= -1.0;
if (diffY < 0)
aYMult *= -1.0;
if (distance < 45000)
{
super.changeImage("spaceship", ".png", 6);
velocityX -= aXMult;
if (aYMult > 1.0)
aYMult = .95;
if (aYMult < -1.0)
aYMult = -.95;
velocityY -= aYMult;
if (velocityX > 5)
velocityX = 5;
if (velocityX < -5)
velocityX = -5;
if (velocityY > 5)
velocityY = 5;
if (velocityY < -5)
velocityY = -5;
}
}*/
//System.out.printf("Velocity Y %f\n", velocityY);
//System.out.printf("Velocity X %f\n", velocityX);
keycheck();
}
private void keycheck()
{
if(Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d")) {
if (velocityX < 5.5)
{
velocityX += 1.0;
}
}
else velocityX *= .85;
if (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a")){
if (velocityX > -5.5)
{
velocityX -= 1.0;
}
}
else
velocityX *= .85;
if(Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w")) {
if (velocityY > -5.5)
velocityY -= 1.0;
}
else velocityY *= .85;
if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s")){
if (velocityY < 5.5)
velocityY += 1.0;
}
else velocityY *= .85;
}
public void die()
{
if (cooldown <= 0)
{
super.changeImage("spaceship", ".png", 6);
health --;
((Universe)getWorld()).healthUpdate(health);
cooldown = 30;
if (health <= 0)
{
((Universe)getWorld()).gameOver();
}
}
}
}