This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.java
219 lines (195 loc) · 6.08 KB
/
player.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Color;
public class player extends creature{
private final int WOODEN =0, METAL = 1, BONE = 2, FISH = 3;
//animation
private Animation animDown,animUp,animLeft,animRight;
private Animation animADown,animAUp,animALeft,animARight;
private int direction;
//inventory
private Inventory inventory;
//attack timer
private long lastAttackTimer, attackCooldown = 200, attackTimer = attackCooldown;
public player(Handler handler, float x, float y){
super(handler,x,y,DEFAULT_WIDTH, DEFAULT_HEIGHT);
animDown = new Animation(250, Assets.pDown);
animUp = new Animation(250, Assets.pUp);
animLeft = new Animation(250, Assets.pLeft);
animRight = new Animation(250, Assets.pRight);
animADown = new Animation(250, Assets.aDown);
animAUp = new Animation(250, Assets.aUp);
animALeft = new Animation(250, Assets.aLeft);
animARight = new Animation(250, Assets.aRight);
direction = 0;
bounds.x = 15;
bounds.y = 30;
bounds.width = 24;
bounds.height = 32;
inventory=new Inventory(handler);
}
public void die(){
System.out.println("you lose");
}
public void update(){
animDown.update();
animUp.update();
animLeft.update();
animRight.update();
animADown.update();
animAUp.update();
animALeft.update();
animARight.update();
move();
getInput();
handler.getCamera().centerOnEntity(this);
checkAttacks();
inventory.update();
}
private void checkAttacks(){
attackTimer += System.currentTimeMillis() - lastAttackTimer;
lastAttackTimer = System.currentTimeMillis();
if(attackTimer < attackCooldown)
return;
Rectangle cb= getCollisionBounds(0,0);
Rectangle ar = new Rectangle();
int arSize = 20;
ar.width = arSize;
ar.height = arSize;
if(handler.getKeyManager().aup){
ar.x = cb.x + cb.width / 2 - arSize / 2;
ar.y = cb.y - arSize;
}
else if(handler.getKeyManager().adown){
ar.x = cb.x + cb.width / 2 - arSize / 2;
ar.y = cb.y + cb.height;
}
else if(handler.getKeyManager().aleft){
ar.x = cb.x - arSize;
ar.y = cb.y + cb.height/2 - arSize / 20;
}
else if(handler.getKeyManager().aright){
ar.x = cb.x + cb.width;
ar.y = cb.y + cb.height/2 - arSize / 20;
}
else{
return;
}
attackTimer = 0;
for(entity e: handler.getLevel().getEntityManager().getEntities()){
if(e.equals(this))
continue;
if(e.getCollisionBounds(0,0).intersects(ar)){
if(e.isDoor() == 1){
if(inventory.getCount(WOODEN) > 0){
e.hurt(10);
inventory.setCount(WOODEN, (inventory.getCount(WOODEN) - 1));
return;
}
handler.setNotification("Wooden key not found");
return;
}
else if(e.isDoor() == 2){
if(inventory.getCount(METAL) > 0){
e.hurt(10);
inventory.setCount(METAL, (inventory.getCount(METAL) - 1));
return;
}
if(inventory.getCount(5) > 0){
e.hurt(10);
inventory.setCount(5, (inventory.getCount(5) - 1));
handler.setNotification("Perform action on water [press and hold A key]", 10);
return;
}
handler.setNotification("Metal key not found");
return;
}
else if(e.isDoor() == 3){
if(inventory.getCount(BONE) > 0){
e.hurt(10);
inventory.setCount(BONE, (inventory.getCount(BONE) - 1));
return;
}
handler.setNotification("Bone key not found");
return;
}
else if(e.isDoor() == 4){
if(inventory.getCount(FISH) > 0){
e.hurt(10);
inventory.setCount(FISH, (inventory.getCount(FISH) - 1));
return;
}
handler.setNotification("Fish key not found");
return;
} else if(e.isDoor() == 0){
e.hurt(1);
return;
} else{
System.out.println("Unable to do anything");
return;
}
}
}
}
private void getInput(){
xMove =0;
yMove =0;
if(handler.getKeyManager().up)
yMove = -speed;
if(handler.getKeyManager().down)
yMove = speed;
if(handler.getKeyManager().left)
xMove = -speed;
if(handler.getKeyManager().right)
xMove = speed;
}
public void render(Graphics g){
g.drawImage(getCurrentAnimationFrame(),(int)(x - handler.getCamera().getxOffset()),(int)(y - handler.getCamera().getyOffset()),width,height,null);
//g.setColor(Color.red);
//g.fillRect((int) (x+ bounds.x - handler.getCamera().getxOffset()),(int) (y+ bounds.y - handler.getCamera().getyOffset()),bounds.width,bounds.height);
inventory.render(g);
}
private BufferedImage getCurrentAnimationFrame(){
if(handler.getKeyManager().isAttackingUp()){
return animAUp.getCurrentFrame();
}else if(handler.getKeyManager().isAttackingDown()){
return animADown.getCurrentFrame();
}else if(handler.getKeyManager().isAttackingLeft()){
return animALeft.getCurrentFrame();
}else if(handler.getKeyManager().isAttackingRight()){
return animARight.getCurrentFrame();
}
if(xMove < 0){
direction = 1;
return animLeft.getCurrentFrame();
}else if(xMove>0){
direction = 2;
return animRight.getCurrentFrame();
}else if(yMove<0){
direction = 3;
return animUp.getCurrentFrame();
}else if(yMove>0){
direction = 0;
return animDown.getCurrentFrame();
}else{
switch(direction){
case 1:
return Assets.pLeft[2];
case 2:
return Assets.pRight[2];
case 3:
return Assets.pUp[2];
default:
return Assets.pDown[2];
}
}
}
//getters and setters
public void setInventory(Inventory inventory){
this.inventory = inventory;
}
public Inventory getInventory(){
return inventory;
}
}