-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
66 lines (52 loc) · 2.05 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
package MoveTest;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
public class Player {
private Rectangle bounding;
private float f_posx;
private float f_posy;
private int worldsize_x;
private int worldsize_y;
private BufferedImage look;
private List<Bullet> bullets;
private float timeSinceLastShot = 0;
private final float SHOTFREQUENZY = 0.1f;
public Player(int x, int y,int worldsize_x, int worldsize_y, List<Bullet> bullets) {
try {
look = ImageIO.read(getClass().getClassLoader().getResourceAsStream("graphics/Fish.png"));
} catch (IOException e) {e.printStackTrace();}
bounding = new Rectangle (x, y, look.getWidth(), look.getHeight());
f_posx = x;
f_posy = y;
this.worldsize_x = worldsize_x;
this.worldsize_y = worldsize_y;
this.bullets = bullets;
}
public void update(float timeSinceLastFrame){
timeSinceLastShot+=timeSinceLastFrame;
if(Keyboard.isKeyDown(KeyEvent.VK_W))f_posy-=300*timeSinceLastFrame;
if(Keyboard.isKeyDown(KeyEvent.VK_S)) f_posy+=300*timeSinceLastFrame;
if(Keyboard.isKeyDown(KeyEvent.VK_D)) f_posx+=300*timeSinceLastFrame;
if(Keyboard.isKeyDown(KeyEvent.VK_A)) f_posx-=300*timeSinceLastFrame;
if(timeSinceLastShot>0.1&&Keyboard.isKeyDown(KeyEvent.VK_SPACE)){
timeSinceLastShot = 0;
bullets.add(new Bullet(f_posx+look.getWidth()/2-Bullet.getLook().getWidth()/2, f_posy+look.getHeight()/2-Bullet.getLook().getHeight()/2,500, 0, bullets));
}
if(f_posx<0)f_posx=0;
if(f_posy<0)f_posy=0;
if(f_posx>worldsize_x-bounding.width)f_posx=worldsize_x-bounding.width;
if(f_posy>worldsize_y-bounding.height)f_posy=worldsize_y-bounding.height;
bounding.x= (int)f_posx;
bounding.y= (int)f_posy;
}
public Rectangle getBounding(){
return bounding;
}
public BufferedImage getLook(){
return look;
}
}