-
Notifications
You must be signed in to change notification settings - Fork 2
/
Player.java
139 lines (121 loc) · 3.74 KB
/
Player.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
138
139
import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import javax.imageio.*;
public class Player {
private boolean left, right, up, down, firing, recovering;
private BufferedImage imageUp, image, imageUpTrans;
private Color color1, color2;
private int requiredPower[] = {1, 2, 3, 4, 5, 6, 7, 8};
private int x, y, r, dx, dy, speed, lives, score, powerLevel, power;
private long firingTimer, firingDelay, recoveryTimer;
public Player () {
x = SpacePanel.width / 2;
y = SpacePanel.height - 100;
r = 25;
dx = 0;
dy = 0;
speed = 5;
lives = 3;
color1 = Color.WHITE;
color2 = Color.RED;
firing = false;
firingTimer = System.nanoTime();
firingDelay = 200;
recovering = false;
recoveryTimer = System.nanoTime();
score = 0;
if (imageUp == null) imageUp = new Generals().loadImg("/img/hero/hero-up.png");
if (imageUpTrans == null) imageUpTrans = new Generals().loadImg("/img/hero/hero-up-transp.png");
image = imageUp;
}
// Render methods
public void update () {
if (left) dx = -speed;
if (right) dx = speed;
if (up) dy = -speed;
if (down) dy = speed;
x += dx;
y += dy;
// setting the frame limit
if (x < r) x = r;
if (y < r) y = r;
if (x > SpacePanel.width - r) x = SpacePanel.width - r;
if (y > SpacePanel.height - r) y = SpacePanel.height - r;
dx = 0;
dy = 0;
if (firing) {
long elapsed = (System.nanoTime() - firingTimer) / 1000000;
if (elapsed > firingDelay) {
firingTimer = System.nanoTime();
if (powerLevel < 2)
SpacePanel.bullets.add(new Bullet(270, (x + 10), y));
else
if (powerLevel < 3) {
SpacePanel.bullets.add(new Bullet(270, (x + 10) + 5, y));
SpacePanel.bullets.add(new Bullet(270, (x + 10) - 5, y));
} else
if (powerLevel < 4) {
SpacePanel.bullets.add(new Bullet(265, (x + 10) - 5, y));
SpacePanel.bullets.add(new Bullet(270, (x + 10), y));
SpacePanel.bullets.add(new Bullet(277, (x + 10) + 5, y));
} else {
SpacePanel.bullets.add(new Bullet(264, (x + 10) - 3, y));
SpacePanel.bullets.add(new Bullet(267, (x + 10) - 3, y));
SpacePanel.bullets.add(new Bullet(270, (x + 10), y));
SpacePanel.bullets.add(new Bullet(273, (x + 10) + 3, y));
SpacePanel.bullets.add(new Bullet(276, (x + 10) + 3, y));
}
}
}
// Time, i am invencible (2 s)
if (recovering) {
long elapsed = (System.nanoTime() - recoveryTimer) / 1000000;
if (elapsed > 2000) {
recovering = false;
recoveryTimer = 0;
}
}
}
public void draw (Graphics2D g) {
if (recovering)
image = imageUpTrans;
else
image = imageUp;
g.drawImage(image, x - r, y - r, null);
}
// Setters
public void setLife (int life) {lives = life;}
public void setLeft (boolean direction) {left = direction;}
public void setRigth (boolean direction) {right = direction;}
public void setUp (boolean direction) {up = direction;}
public void setDown (boolean direction) {down = direction;}
public void setFiring (boolean fire) {firing = fire;}
public void setScore (int sc) {score += sc;}
// Getters
public boolean isDead () {return lives <= 0;}
public boolean isRecovering () {return recovering;}
public int getLives () {return lives;}
public int getPower () {return power;}
public int getPowerLevel () {return powerLevel;}
public int getR () {return r;}
public int getRequiredPower () {return requiredPower[powerLevel];}
public int getScore () {return score;}
public int getX () {return x;}
public int getY () {return y;}
// General methods
public void loseLife () {
lives--;
recovering = true;
recoveryTimer = System.nanoTime();
}
public void increasePower (int pwr) {
if (power < 6) {
power += pwr;
if (power >= requiredPower[powerLevel]) {
power -= requiredPower[powerLevel];
powerLevel++;
}
}
}
}