-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnemy.java
More file actions
83 lines (82 loc) · 2.88 KB
/
Enemy.java
File metadata and controls
83 lines (82 loc) · 2.88 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
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
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 int difficulty;
public int damage;
public int punchdelay = 20;
private DelayCounter punchcounter = new DelayCounter(punchdelay);
public int bulletdelay = 60;
private DelayCounter shootcounter = new DelayCounter(bulletdelay);
public void act()
{
moveTowardsPlayer();
fall(false);
attack();
// dodgeBullet();
shoot();
System.out.println("Enemy HP: " + Integer.toString(hp));
}
public void moveTowardsPlayer() {
Actor fighter = (Actor)getWorld().getObjects(Fighter.class).get(0);
if (!recoil) {
if (getX() > fighter.getX()) {
setLocation(getX() - velocity, getY());
}
else if (getX() < fighter.getX()) {
setLocation(getX() + velocity, getY());
}
}
else {
recoil_velocity -= recoil_acceleration;
if (getX() > fighter.getX() || getX() == fighter.getX()) {
setLocation((int)(getX() + recoil_velocity), getY());
}
else if (getX() < fighter.getX()) {
setLocation((int)(getX() - recoil_velocity), getY());
}
}
}
public void attack() {
if (!getObjectsInRange(70, Fighter.class).isEmpty()) {
if (punchcounter.counter()) {
System.out.println("check");
Fighter fighter = getObjectsInRange(70, Fighter.class).get(0);
fighter.hp -= damage;
recoil = true;
recoil_velocity = initial_recoil_velocity + recoil_acceleration;
fighter.recoil = true;
fighter.recoil_velocity = initial_recoil_velocity/2 + recoil_acceleration;
fighter.attacked = true;
}
}
}
public void dodgeBullet() {
if (!getObjectsInRange(60, Bullet.class).isEmpty()) {
Bullet bullet = getObjectsInRange(60, Bullet.class).get(0);
if (bullet.fighter) {
int chance = difficulty - 10;
if (Greenfoot.getRandomNumber(100) <= chance) {
bullet.dodged = true;
jump();
}
}
}
}
public void shoot() {
if (shootcounter.counter()) {
Fighter fighter = (Fighter)getWorld().getObjects(Fighter.class).get(0);
boolean left = fighter.getX() < getX();
getWorld().addObject(new Bullet(left, false, damage), getX(), getY());
}
}
}