-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.java
115 lines (92 loc) · 3.43 KB
/
Ship.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
import greenfoot.*;
import java.util.ArrayList;
public class Ship implements IDrawable
{
private World _world;
private int _targetX = 0;
private int _targetY = 0;
private int _curX = 0;
private int _curY = 0;
private static final int MARGIN = 25;
private GreenfootImage _ship;
private GreenfootImage _shadow;
private GreenfootImage _engine;
private int _engineAnimation = 0;
private long _lastShotFired = 0;
private int _refireRate = 150;
private int _health = 100;
private int _speed = 10;
public ArrayList<Shot> Shots = new ArrayList<Shot>();
private Font myFont;
private int _shotCounter = 0;
public Ship()
{
_engine = new GreenfootImage("ship_engine.png");
myFont = new Font("Hermit", false, false, 24);
}
public void Init(World world) {
_world = world;
}
public void setModel(String model) {
_ship = new GreenfootImage(model+".png");
_shadow = new GreenfootImage(model+"_shadow.png");
}
public void Refresh() {
_curX += ((_targetX-_curX) / 10)>_speed?_speed:(_targetX-_curX) / 10;
_curY += ((_targetY-_curY) / 10)>_speed?_speed:(_targetY-_curY) / 10;
int newX = _curX-(_ship.getWidth()/2);
int newY = _curY-(_ship.getHeight()/2);
int shadowX = (int)Math.round((newX-400)*.75) + 400;
int shadowY = (int)Math.round((newY-300)*.75) + 300;
// shadow
_world.getBackground().drawImage(_shadow, shadowX, shadowY);
// engine
if (_engineAnimation++ > 4) _engineAnimation = 0;
int engineOffsetX = _ship.getWidth()/2-6;
int engineOffsetY = _ship.getHeight()-8;
if (_engineAnimation == 0) _engine.scale(15,50);
if (_engineAnimation == 1) _engine.scale((int)(15*.8),(int)(50*.8));
if (_engineAnimation == 2) {_engine.mirrorHorizontally(); _engine.scale(15,50);}
if (_engineAnimation == 3) {_engine.mirrorHorizontally(); _engine.scale((int)(15*.8),(int)(50*.8));}
_world.getBackground().drawImage(_engine, newX+engineOffsetX, newY+engineOffsetY);
// ship
_world.getBackground().drawImage(_ship, newX, newY);
// shots
ArrayList<Shot> newShots = new ArrayList<Shot>();
for (Shot shot : Shots) {
shot.y -= shot.speed;
if (shot.y < -100) continue;
newShots.add(shot);
// paint the shot
_world.getBackground().setColor(Color.ORANGE);
_world.getBackground().fillRect(shot.x-1, shot.y-1, 3, shot.speed+3);
_world.getBackground().setColor(Color.YELLOW);
_world.getBackground().drawLine(shot.x, shot.y, shot.x, shot.y+shot.speed);
}
Shots = newShots;
}
public void setTarget(int x, int y) {
if (x < MARGIN) x = MARGIN;
if (x > 800-MARGIN) x = 800-MARGIN;
if (y < MARGIN) y = MARGIN;
if (y > 600-MARGIN) y = 600-MARGIN;
_targetX = x;
_targetY = y;
}
public void Fire() {
if (System.currentTimeMillis() - _lastShotFired > _refireRate) {
_lastShotFired = System.currentTimeMillis();
Shot shot = new Shot();
shot.x = _curX;
shot.y = _curY-15;
shot.speed = 10;
_shotCounter++;
Shots.add(shot);
}
}
public class Shot {
public int x = 0;
public int y = 0;
public int speed = 0;
}
}