forked from salutationsearth/troy-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.java
More file actions
53 lines (52 loc) · 1.44 KB
/
Enemy.java
File metadata and controls
53 lines (52 loc) · 1.44 KB
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Enemy here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Enemy extends Character
{
/**
* Act - do whatever the Enemy wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Enemy() {
velocity = 2;
hp = 1000; // placeholder
}
public void act()
{
moveTowardsPlayer();
fall();
}
public void fall() {
if (getY() < ground) {
gravity_velocity += gravity;
setLocation(getX(), (int)(getY() + gravity_velocity));
}
if (getY() > ground) {
setLocation(getX(), ground);
}
}
public void moveTowardsPlayer() {
Actor fighter = (Actor)getWorld().getObjects(Fighter.class).get(0);
if (getX() > fighter.getX()) {
setLocation(getX() - velocity, getY());
}
else if (getX() < fighter.getX()) {
setLocation(getX() + velocity, getY());
}
}
public void jump() {
if (getY() == ground) {
gravity_velocity = initial_jump_velocity;
setLocation(getX(), (int)(getY() + gravity_velocity));
}
}
public void jumpRandomly() {
if (Greenfoot.getRandomNumber(jump_random) == 133) {
jump();
}
}
}