forked from salutationsearth/troy-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFighter.java
More file actions
98 lines (93 loc) · 2.75 KB
/
Fighter.java
File metadata and controls
98 lines (93 loc) · 2.75 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Fighter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Fighter extends Character
{
/**
* Act - do whatever the Fighter wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public double recoil_acceleration = 3.0;
public double initial_recoil_velocity = 30.0;
public double recoil_velocity = 0.0;
public boolean recoil = false;
public Fighter() {
velocity = 20;
}
public void act()
{
checkKeys();
fall();
}
public void moveRight() {
if (!recoil) {
setLocation(getX() + velocity, getY());
}
}
public void moveLeft() {
if (!recoil) {
setLocation(getX() - velocity, getY());
}
}
public void jump() {
if (getY() == ground) {
gravity_velocity = initial_jump_velocity;
setLocation(getX(), (int)(getY() + gravity_velocity));
}
}
public void fall() {
// vertical acceleration
if (getY() < ground) {
gravity_velocity += gravity;
setLocation(getX(), (int)(getY() + gravity_velocity));
}
if (getY() > ground) {
setLocation(getX(), ground);
}
// recoil acceleration
if (recoil_velocity < 0) {
recoil_velocity = 0;
recoil = false;
}
if (recoil) {
Enemy enemy = getWorld().getObjects(Enemy.class).get(0);
recoil_velocity -= recoil_acceleration;
if (enemy.getX() > getX()) {
setLocation((int)(getX() - recoil_velocity), getY());
}
else {
setLocation((int)(getX() + recoil_velocity), getY());
}
}
}
public void punch() {
if (!getObjectsInRange(70, Enemy.class).isEmpty()) {
Enemy enemy = getObjectsInRange(70, Enemy.class).get(0);
enemy.hp -= 5;
recoil = true;
recoil_velocity = initial_recoil_velocity;
}
}
private void checkKeys() {
if (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a")) {
setImage(left_file);
moveLeft();
}
if (Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d")) {
setImage(right_file);
moveRight();
}
if (Greenfoot.isKeyDown("space") || Greenfoot.isKeyDown("w")) {
setImage(jump_file);
jump();
}
if (Greenfoot.mouseClicked(null)) {
// setImage(punch_file);
punch();
}
}
}