-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
155 lines (142 loc) · 4.88 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
// import classes
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.Timer;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.geom.Ellipse2D;
// class Player represents a soccer player
public class Player extends JComponent {
// instance fields
private int xPos; // horizontal position
private int yPos; // vertical position
private int xVec; // magnitude of horizontal vector
private int yVec; // magnitude of vertical vector
private Timer[] timers;
private Color headColor; // color of head
private Color jerseyColor; // color of jersey
private String type; // type of player
// default constructor
public Player() {
this.construct(0, 0, null, null, "player", 0, 0);
}
// customizable constructors
public Player(int xP, int yP) {
this.construct(xP, yP, null, null, "player", 0, 0);
}
public Player(int xP, int yP, Color jc) {
this.construct(0, 0, jc, null, "player", 0, 0);
}
public Player(int xP, int yP, Color hc, Color jc, String t) {
this.construct(0, 0, hc, jc, t, 0, 0);
}
public Player(int xP, int yP, int xV, int yV) {
this.construct(xP, yP, null, null, "player", xV, yV);
}
// private mutator convenience method for setting instance fields
private void construct(int xP, int yP, Color hc, Color jc, String t, int xV, int yV) {
// choose random head and jersey colors if not provided
if (hc == null)
hc = new Color((int) (Math.random() * 75 + 175), (int) (Math.random() * 65 + 145), (int) (Math.random() * 55 + 135));
if (jc == null)
jc = new Color((int) (Math.random() * 210 + 45), (int) (Math.random() * 210 + 30), (int) (Math.random() * 210 + 40));
this.xPos = xP;
this.yPos = yP;
this.xVec = xV;
this.yVec = yV;
this.type = t;
this.timers = new Timer[2];
this.headColor = hc;
this.jerseyColor = jc;
this.setBounds(0, 0, 500, 500);
this.setBackground(null);
}
// overridden paintComponent method for JComponent
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double head = new Ellipse2D.Double(this.xPos, this.yPos, 18, 18);
g2.setColor(this.jerseyColor);
g2.fillRect(this.xPos + 4, this.yPos + 15, 11, 25);
g2.setColor(this.headColor);
g2.fill(head);
}
// (chainable) mutator for method for generating new random direction and speed
public Player randVector() {
// generate new random vector magnitudes
this.xVec = (int) (Math.random() * 7 - 3);
this.yVec = (int) (Math.random() * 7 - 3);
// chain
return this;
}
// (chainable) mutator for method for setting new direction and speed
public Player setVector(int[] v) {
// set new vector magnitudes
this.xVec = v[0];
this.yVec = v[1];
// chain
return this;
}
// (chainable) overloaded method for moving JComponent
public Player move() {
// call normal move player method
this.movePlayer();
// chain
return this;
}
// (chainable) mutator for method for setting new position
public Player setPosition(int[] p) {
// set new coordinates
this.xPos = p[0];
this.yPos = p[1];
// chain
return this;
}
// (chainable) mutator method for moving player
public Player movePlayer() {
// boundary colliders
if (this.xPos >= 480) {
this.xVec *= -1;
this.xPos = 479;
} else if (this.xPos <= 0) {
this.xVec *= -1;
this.xPos = 1;
}
if (this.yPos >= 460) {
this.yVec *= -1;
this.yPos = 459;
} else if (this.yPos <= 45) {
this.yVec *= -1;
this.yPos = 46;
}
// move coordinates by vector
this.xPos += this.xVec;
this.yPos += this.yVec;
// chain
return this;
}
// (chainable) mutator method to keep track of player's timers
public Player setTimers(Timer t1, Timer t2) {
this.timers[0] = t1;
this.timers[1] = t2;
// chain
return this;
}
// accessor method to keep track of player's timers
public Timer[] getTimers() {
return this.timers;
}
// accessor method for rectangle representing the bounds of the player
public Rectangle getCollider() {
return new Rectangle(this.xPos, this.yPos, 18, 32);
}
// accessor method for array containing player vector
public int[] getVector() {
return new int[] {this.xVec, this.yVec};
}
// accessor method for array containing player position
public int[] getPosition() {
return new int[] {this.xPos, this.yPos};
}
}