-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobs.java
executable file
·162 lines (143 loc) · 3.83 KB
/
Mobs.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import greenfoot.GreenfootImage;
import greenfoot.World;
import java.util.List;
/**
* Mobs Class
*
* @author Adrian, Jason
* @version June 13 2024
*/
public abstract class Mobs extends SuperSmoothMover {
protected World w;
protected int attackAct = 0;
protected int direction = 1;
protected int hp;
protected int speed = 2;
protected int dmg;
/**
* Constructor
*/
public Mobs() {
enableStaticRotation();
}
protected void bounceWall(GreenfootImage image) {
if (getOneObjectAtOffset(direction * getImage().getWidth() + 2, 0, Tile.class) != null) {
direction *= -1;
image.mirrorHorizontally();
}
}
public void addedToWorld(World w) {
this.w = w;
}
public void act() {
stepped();
attackAct++;
}
protected void gravity() {
if (!isTouching(Brick.class)) {
setLocation(getX(), getY() + 2);
}
}
protected void boundary() {
if (w == null) {
return;
}
if (getX() < 0) {
setLocation(0, getY());
}
if (getX() > w.getWidth()) {
setLocation(w.getWidth(), getY());
}
if (getY() < 0) {
setLocation(getX(), 0);
}
if (getY() > w.getHeight()) {
setLocation(getX(), w.getHeight());
}
}
/**
* Checks for collision with the Brick class
*/
public void collision() {
if (getOneObjectAtOffset(getImage().getWidth() / 2, 0, Brick.class) != null) {
setLocation(getX() - speed, getY());
}
if (getOneObjectAtOffset(-(getImage().getWidth() / 2), 0, Brick.class) != null) {
setLocation(getX() + speed, getY());
}
}
protected void idle() {
if (getWorld() == null) {
return;
}
turnTowards(getX() + direction, getY());
if (getOneObjectAtOffset(direction * getImage().getWidth() + 1, 0, Brick.class) != null) {
direction *= -1;
} else {
move(speed);
}
}
/**
* Get distance to a given Player
* Inspired by Gevater_Tod4177 on greenfoot.org
* <a href="https://www.greenfoot.org/topics/4911">...</a>
*
* @param p Player
*/
public double getDistance(Player p) {
if (p != null) {
return Math.hypot(p.getX() - getX(), p.getY() - getY());
}
return 0;
}
/**
* Get the Player
* Inspired by Gevater_Tod4177 on greenfoot.org
* <a href="https://www.greenfoot.org/topics/4911">...</a>
*/
public Player getPlayer(int range) {
if (getWorld() == null) {
return null;
}
List<Player> pNear = getObjectsInRange(range, Player.class);
if (!pNear.isEmpty()) {
return pNear.get(0);
}
return null;
}
/**
* If stepped on by Player class, remove this mob
*/
public void stepped() {
if (getWorld() == null) {
return;
}
if (getOneObjectAtOffset(getX(), -(getImage().getHeight() / 2), Player.class) != null) {
getWorld().removeObject(this);
}
}
/**
* Checks if Mob attacks Player and changes HP by dmg if Player gets hit
*
* @param dmg The amount to change Player HP by
*/
public void attack(int dmg) {
Player p = (Player) getOneIntersectingObject(Player.class);
if (p != null && attackAct > 60) {
attackAct = 0;
p.changeHP(-dmg);
}
}
public void changeHP(int deltaHP) {
hp += deltaHP;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void setSpeed(double multiplier) {
speed = (int) (speed * multiplier);
}
}