-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvancedFarmer.java
112 lines (95 loc) · 2.51 KB
/
AdvancedFarmer.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
package SaueSpillet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import javax.swing.ImageIcon;
public class AdvancedFarmer extends GameObject {
private int width = 68;
private int height = 133;
private Random r;
private static Image farmerPic;
private int health;
private GameObject stoneHit;
private boolean flimmer = false;
private long start, end;
public static ArrayList<ID> hitList = new ArrayList<ID>(Arrays.asList(ID.Stone));
public AdvancedFarmer(float x, float y, ID id, Handler handler, int hit, int health) {
super(x, y, id, handler, hit);
r = new Random();
velX = (float) (-0.7 - r.nextFloat()/1.8);
velY = 0;
this.health = health;
loadpic();
handler.addObject(new Trail((int) x - 50, (int) y - 20, ID.InitBoom, handler, 0.025));
start = System.currentTimeMillis();
}
public Rectangle getBounds() {
return new Rectangle((int) x, (int) y, width, height);
}
public void tick() {
r = new Random();
x += velX;
// y += velY;
if (r.nextInt(5) == 1) {
y += 5 - r.nextInt(10);
}
if (y <= 0 || y >= Game.HEIGHT - 131)
velY *= -1;
if (x < 0) {
Hud.health -= 4;
handler.removeObject(this);
}
// if (x <= 0 || x >= Game.WIDTH)
// velX *= -1;
// x = Game.clamp(x, 0, Game.WIDTH);
y = Game.clamp(y, 0, Game.HEIGHT - 132);
collision();
if (hit == 1) {
hit = 0;
health -= 10;
}
if (health < 1) {
handler.removeObject(this);
Hud.increaseKills();
}
}
public void loadpic() {
farmerPic = new ImageIcon(
"C:\\Users\\Sjur\\tdt4100-2018-master\\git\\tdt4100-2018\\minegenkode\\GamepackRes\\farmer2.png")
.getImage();
}
private void collision() {
for (GameObject obj : handler.object) {
for (ID idi : hitList) {
if (obj.getId() == idi) {
if (getBounds() != null && obj.getBounds() != null) {
if (getBounds().intersects(obj.getBounds()) && obj != stoneHit) {
health -= Hud.stoneDamage;
obj.increaseHit();
stoneHit = obj;
Hud.score += 5;
if (health > 0) {
start = System.currentTimeMillis();
flimmer = true;
}
}
}
}
}
}
}
public void render(Graphics g) {
end = System.currentTimeMillis();
if (!((end - start) / 10 < 5)) {
if ((end - start) / 10 > 10 && flimmer) {
start = System.currentTimeMillis();
flimmer = false;
}
//g.drawRect((int) x, (int) y, width, height);
g.drawImage(farmerPic, (int) x, (int) y, null);
}
}
}