-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hero.java
165 lines (117 loc) · 2.79 KB
/
Hero.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
163
164
165
package GraphicTest;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import GraphicDeom.Controller;
public class Hero {
private Image image;
private float x, y;
private double speedY, speedX;
private double gravity = 30;
public double getSpeedY() {
return speedY;
}
public void setSpeedY(double speedY) {
this.speedY = speedY;
}
public double getSpeedX() {
return speedX;
}
public void setSpeedX(double speedX) {
this.speedX = speedX;
}
public Hero(float x, float y, double speedX, double speedY, Image image) {
this.x = x;
this.y = y;
this.image = image;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public void render(Graphics2D g) {
g.translate(x, y);
int width = image.getWidth(null);
int height = image.getHeight(null);
height = height/2;
width = width/2;
if(gravity < 0){
g.rotate(Math.PI);
g.drawImage(image, -width, -height, width, height, null);
}
else{
g.drawImage(image, 0, 0, width, height, null);
}
g.translate(-x, -y);
}
public void update(Controller controller, double delta, boolean occupiedCoordinates[][]) {
delta = delta/1000;
setSpeedX(0);
setSpeedY(getSpeedY()+gravity*delta);
if(checkOccupied(occupiedCoordinates)){
}
if(y > 538){
speedY = 0;
y = 538;
}
if(y < 0){
speedY = 0;
y = 0;
}
if(x < 0){
x = 0;
}
if(x > 755){
x = 755;
}
rightButton(controller, delta);
leftButton(controller, delta);
downButton(controller, delta);
upButton(controller, delta);
spaceButton(controller, delta);
x += getSpeedX();
y += getSpeedY();
//Skriver ut SpeedY och delta
if(speedY != 0){
System.out.println("Speed Y: " +speedY);
System.out.println("Delta: " +delta);
System.out.println("Y: " +y);
}
}
private boolean checkOccupied(boolean[][] occupiedCoordinates) {
if(occupiedCoordinates[(int)x][(int)y]){
return true;
}
return false;
}
private void spaceButton(Controller controller, double delta) {
if(controller.characters[KeyEvent.VK_SPACE] && !controller.hoppa){
gravity = gravity*-1;
controller.hoppa = false;
}
}
private void upButton(Controller controller, double delta) {
if(y >=538 && controller.characters[KeyEvent.VK_UP]){
setSpeedY(-10);
}
}
private void downButton(Controller controller, double delta) {
if(controller.characters[KeyEvent.VK_DOWN]){
if(getSpeedY() == 0){
setSpeedY(10);
}
}
}
private void leftButton(Controller controller, double delta) {
if(controller.characters[KeyEvent.VK_LEFT]){
setSpeedX(-3);
}
}
private void rightButton(Controller controller, double delta) {
if(controller.characters[KeyEvent.VK_RIGHT]){
setSpeedX(3);
}
}
}