-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
113 lines (102 loc) · 3.76 KB
/
Player.java
File metadata and controls
113 lines (102 loc) · 3.76 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
// this class contains the code for the player class
// this contains the method to draw the player
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
public class Player
{
// these are the player's instance variables
private int x;
private int y;
private int size;
private Color pColor;
private String actionState;
private Image idleDown;
private Image walkDown;
private Image idleUp;
private Image walkUp;
private Image idleLeft;
private Image walkLeft;
private Image idleRight;
private Image walkRight;
// this is the constructor method of the player
public Player(int x, int y)
{
this.x = x;
this.y = y;
pColor = new Color(255, 255, 255);
this.size=80; //width and height will be the same
actionState = "idle";
idleDown = Toolkit.getDefaultToolkit().getImage("playerIdleDown.gif");
idleUp = Toolkit.getDefaultToolkit().getImage("playerIdleUp.gif");
idleLeft = Toolkit.getDefaultToolkit().getImage("playerIdleLeft.gif");
idleRight = Toolkit.getDefaultToolkit().getImage("playerIdleRight.gif");
walkDown = Toolkit.getDefaultToolkit().getImage("playerWalkDown.gif");
walkUp = Toolkit.getDefaultToolkit().getImage("playerWalkUp.gif");
walkLeft = Toolkit.getDefaultToolkit().getImage("playerWalkLeft.gif");
walkRight = Toolkit.getDefaultToolkit().getImage("playerWalkRight.gif");
}
// this draws the player depending on state
public void drawIt(Graphics g, String direction)
{
// draw the body of the snowman
g.setColor(pColor);
// draw the eyes
if (direction.equals("straight")) {
g.drawImage(idleDown, x, y, 64, 64, null);
} else if (direction.equals("right")) {
if (actionState.equals("idle")) {
g.drawImage(idleRight, x, y, 64, 64, null);
} else if (actionState.equals("walk")) {
g.drawImage(walkRight, x, y, 64, 64, null);
}
} else if (direction.equals("left")) {
if (actionState.equals("idle")) {
g.drawImage(idleLeft, x, y, 64, 64, null);
} else if (actionState.equals("walk")) {
g.drawImage(walkLeft, x, y, 64, 64, null);
}
} else if (direction.equals("up")) {
if (actionState.equals("idle")) {
g.drawImage(idleUp, x, y, 64, 64, null);
} else if (actionState.equals("walk")) {
g.drawImage(walkUp, x, y, 64, 64, null);
}
} else if (direction.equals("down")) {
if (actionState.equals("idle")) {
g.drawImage(idleDown, x, y, 64, 64, null);
} else if (actionState.equals("walk")) {
g.drawImage(walkDown, x, y, 64, 64, null);
}
}
}
// these methods check the collision of npcs and items
public void checkNPCCollision(NPC npc, int xDiff, int yDiff) {
if ((x+50 >= npc.getX()+20+xDiff && x <= npc.getX()+xDiff+40) && (y+50 >= npc.getY()+yDiff && y <= npc.getY()+yDiff+50)) {
npc.changeRange(true);
} else {
npc.changeRange(false);
}
}
public void checkItemCollision(Item item, int xDiff, int yDiff) {
if ((x+50 >= item.getX()+20+xDiff && x <= item.getX()+xDiff+40) && (y+50 >= item.getY()+yDiff && y <= item.getY()+yDiff+50)) {
item.changeRange(true);
} else {
item.changeRange(false);
}
}
// these are methods that return and change x and y
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public String getState() {
return actionState;
}
public void changeState(String state) {
actionState = state;
}
}