-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
120 lines (97 loc) · 2.7 KB
/
Player.java
File metadata and controls
120 lines (97 loc) · 2.7 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
/*
*Author: Cameron Ekblad
*UPI: cekb635
*Date: 27/5/2011
*This is the Player class, which represents the user's avatar in the game. It contains basic information relating to the player, such as movement speed,
*position on screen and size, and also contains methods to move the character around the screen, fire, and (unfortunately) die. It also contains details on how
*the character is meant to be drawn, and various accessor and mutator methods which are used by the A3JPanel class to gain and manipulate information
*relating to the player.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class Player{
public static final char UP = 'w';
public static final char DOWN = 's';
public static final char LEFT = 'a';
public static final char RIGHT = 'd';
public static final int SPEED = 2;
public static final int SIZE = 30;
private Point middle;
private Rectangle playerArea;
private boolean isDead;
private double zombieKills;
private boolean canFire;
private Line2D.Double shot;
public Player(){
middle = new Point(300,300);
playerArea = new Rectangle(middle.x - SIZE/2, middle.y - SIZE/2, SIZE, SIZE);
zombieKills = 0;
canFire = true;
}
public void moveUp(Line2D.Double topBorder){
if(!(playerArea.intersectsLine(topBorder))){
middle.y = middle.y - SPEED;
}
}
public void moveDown(Line2D.Double bottomBorder){
if(!(playerArea.intersectsLine(bottomBorder))){
middle.y = middle.y + SPEED;
}
}
public void moveLeft(Line2D.Double leftBorder){
if(!(playerArea.intersectsLine(leftBorder))){
middle.x = middle.x - SPEED;
}
}
public void moveRight(Line2D.Double rightBorder){
if(!(playerArea.intersectsLine(rightBorder))){
middle.x = middle.x + SPEED;
}
}
public void updatePlayerArea(){
playerArea = new Rectangle(middle.x - SIZE/2, middle.y - SIZE/2, SIZE, SIZE);
}
public void die(){
isDead = true;
}
public void fire(Point p){
shot = new Line2D.Double(middle, p);
}
public void drawShot(Graphics g, Point p){
g.setColor(Color.red);
g.drawLine(middle.x, middle.y, p.x, p.y);
}
public void draw(Graphics g){
g.setColor(new Color(211, 175, 142));
g.fillOval(middle.x - SIZE/2, middle.y - SIZE/2, SIZE, SIZE);
}
public void addZombieKill(){
zombieKills++;
}
public void playerCanFire(){
canFire = true;
}
public void playerCannotFire(){
canFire = false;
}
public Point getMiddle(){
return middle;
}
public Rectangle getPlayerArea(){
return playerArea;
}
public boolean playerIsDead(){
return isDead;
}
public double getZombieKills(){
return zombieKills;
}
public boolean canPlayerFire(){
return canFire;
}
public Line2D.Double getShot(){
return shot;
}
}