-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
182 lines (155 loc) · 4.88 KB
/
Player.java
File metadata and controls
182 lines (155 loc) · 4.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import greenfoot.*; // import every greefoot function, object and more
public class Player extends Actor
{
private int health = 800;
private int moveSteps = 4;
// position datas
private int groundLevel;
private int yCord;
private int xCord = 200;
// jump flags
private boolean jumpedUp = false;
private boolean jumpFinished = true;
private boolean canDoubleJump = true;
private boolean spacePreviouslyDown = false;
// jump movement datas
private int initialJumpSpeed = 16;
private int doubleJumpSpeed = 14;
private double ySpeed = 0;
private double gravity = 0.7;
private double maxFallSpeed = 10;
private int steppingIndex = 1;
public Player() {
GreenfootImage image = new GreenfootImage("./images/knight.png");
this.setImage(image);
this.groundLevel = MapManager.getHeight() - image.getHeight()/2 - MapManager.getGroundHeight();
this.yCord = this.groundLevel;
this.showHealth();
}
public int getXCord() {
return xCord;
}
public int getYCord() {
return yCord;
}
public void act() {
this.stepping();
this.moving();
this.jumping();
this.damage();
this.heal();
}
/**
* Change image of the player to create an nice setpping effect
*/
private void stepping() {
if (this.steppingIndex < 9)
{
this.steppingIndex = this.steppingIndex + 1;
} else {
this.steppingIndex = 0;
}
this.setImage("./images/knight_" + this.steppingIndex + ".png");
}
/**
* Handle all methods for the jumping feature
*/
private void jumping() {
this.handleJumpInput();
this.applyGravity();
this.applyMovement();
this.checkGround();
}
private void handleJumpInput() {
boolean spaceNowDown = Greenfoot.isKeyDown("space");
// activate jump motion
if (spaceNowDown && !this.spacePreviouslyDown) {
if (this.jumpFinished) { // first jump
this.ySpeed = -this.initialJumpSpeed;
this.jumpFinished = false;
this.canDoubleJump = true;
} else if (this.canDoubleJump) { // second jump
this.ySpeed = -this.doubleJumpSpeed;
this.canDoubleJump = false;
}
}
this.spacePreviouslyDown = spaceNowDown;
}
/**
* Add gravity to the jump
*/
private void applyGravity() {
this.ySpeed += this.gravity;
if (this.ySpeed > this.maxFallSpeed) {
this.ySpeed = this.maxFallSpeed;
}
}
/**
* The acctully jump
*/
private void applyMovement() {
this.yCord += (int) this.ySpeed;
this.setLocation(this.xCord, this.yCord);
}
/**
* Reset datas when player is on ground
*/
private void checkGround() {
if (this.yCord >= this.groundLevel) {
this.yCord = this.groundLevel;
setLocation(this.xCord, this.yCord);
this.ySpeed = 0;
this.jumpFinished = true;
this.canDoubleJump = true;
}
}
private void moving()
{
// move left
if (Greenfoot.isKeyDown("left")) {
this.xCord = this.xCord - this.moveSteps;
this.setLocation(this.xCord, this.yCord);
}
// move right
if (Greenfoot.isKeyDown("right")) {
this.xCord = this.xCord + this.moveSteps;
this.setLocation(this.xCord, this.yCord);
}
}
public void damage() {
if (this.isTouching(Enemy.class)) {
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
this.health -= enemy.getDamage();
// stop game when player has no health
if (this.health <= 0) {
GameManager.stop();
return;
}
// place medi kit
if (this.health == 100) {
this.placeMediKit();
}
this.showHealth();
}
}
private void showHealth() {
MapManager.showText("♥️" + this.health, 80, 30);
}
private void placeMediKit() {
if (this.yCord == this.groundLevel) {
MediKit mediKit = new MediKit();
MapManager.currentMap.addObject(mediKit, Greenfoot.getRandomNumber(MapManager.getWidth()) + 10, this.yCord - 200);
}
}
/**
* Add health
*/
private void heal() {
if (this.isTouching(MediKit.class)) {
MediKit mediKit = (MediKit) getOneIntersectingObject(MediKit.class);
this.health += mediKit.getHealLevel();
this.showHealth();
MapManager.currentMap.removeObject(mediKit);
}
}
}