1515
1616public class Player extends Mob {
1717
18- private static Name customeName = new Name () ;
19- public static String guestPlayerName = customeName .setName ("Player " );
20- private static double speed = 1 ;
21- private static int [] collisionBoders = {- 2 , 8 , 0 , 7 } ;
22- private InputHandler input ;
23- private Swim swim ;
24- private int colour , shirtCol , faceCol ;
18+ private static final int [] COLLISION_BORDERS = {- 2 , 8 , 0 , 7 } ;
19+ private static final String guestPlayerName = new Name () .setName ("Player " );
20+ private static final double speed = 1 ;
21+ private final InputHandler inputHandler ;
22+ private final int shirtColour ;
23+ private final int faceColour ;
24+ private int colour ;
2525 private int tickCount = 0 ;
26+ private Swim swim ;
2627 private String userName ;
2728 private boolean [] swimType ;
28- private int [] swimColour ;
29- private int fireRate = 0 ;
30-
31- // Need to add a class for constants
32- private final int fontCharSize = 8 ;
29+ private int fireRate ;
3330 // "Cache" the division for the username length, no need for 60 divisions per second here.
3431 private int nameOffset = 0 ;
3532
36- public Player (LevelHandler level , int x , int y , InputHandler input ,
37- String userName , int shirtCol , int faceCol ) {
38- super (level , "Player" , x , y , speed , collisionBoders );
39- this .input = input ;
33+ public Player (LevelHandler level , int x , int y , InputHandler inputHandler , String userName , int shirtColour , int faceColour ) {
34+ super (level , "Player" , x , y , speed , COLLISION_BORDERS );
35+ this .inputHandler = inputHandler ;
4036 this .userName = userName ;
41- this .faceCol = faceCol ;
42- this .shirtCol = shirtCol ;
43- this .colour = Colours .get (-1 , 111 , shirtCol , faceCol );
37+ this .faceColour = faceColour ;
38+ this .shirtColour = shirtColour ;
39+ this .colour = Colours .get (-1 , 111 , shirtColour , faceColour );
4440 fireRate = Small .FIRE_RATE ;
4541 }
4642
47- public static double getSpeed () {
48- return speed ;
49- }
50-
51- public static void setSpeed (double speed ) {
52- Player .speed = speed ;
53- }
54-
5543 public void tick () {
5644 double xa = 0 ;
5745 double ya = 0 ;
5846
59- if (input != null ) {
60- if (input .getUP_KEY ().isPressed () && input .isIgnoreInput () == false ) {
47+ if (inputHandler != null ) {
48+ if (inputHandler .getUP_KEY ().isPressed () && ! inputHandler .isIgnoreInput ()) {
6149 ya -= speed ;
6250 }
63- if (input .getDOWN_KEY ().isPressed () && input .isIgnoreInput () == false ) {
51+ if (inputHandler .getDOWN_KEY ().isPressed () && ! inputHandler .isIgnoreInput ()) {
6452 ya += speed ;
6553 }
66- if (input .getLEFT_KEY ().isPressed () && input .isIgnoreInput () == false ) {
54+ if (inputHandler .getLEFT_KEY ().isPressed () && ! inputHandler .isIgnoreInput ()) {
6755 xa -= speed ;
6856 }
69- if (input .getRIGHT_KEY ().isPressed () && input .isIgnoreInput () == false ) {
57+ if (inputHandler .getRIGHT_KEY ().isPressed () && ! inputHandler .isIgnoreInput ()) {
7058 xa += speed ;
7159 }
7260 }
7361
74- if (fireRate > 0 ) fireRate --;
62+ if (fireRate > 0 ) fireRate --;
7563
76- if (Game .getMouse ().getButton () == 1 || Game .getMouse ().getButton () == 3 ){
77- if (fireRate <= 0 ){
78- if (Game .getMouse ().getButton ()== 1 ){
64+ if (Game .getMouse ().getButton () == 1 || Game .getMouse ().getButton () == 3 ) {
65+ if (fireRate <= 0 ) {
66+ if (Game .getMouse ().getButton () == 1 ) {
7967 fireRate = Small .FIRE_RATE ;
80- }else if (Game .getMouse ().getButton () == 3 ){
68+ } else if (Game .getMouse ().getButton () == 3 ) {
8169 fireRate = Medium .FIRE_RATE ;
8270 }
83- if (!swim .isActive (swimType )){
84- double dx = Game .getMouse ().getX () - 480 / 2 ;
85- double dy = Game .getMouse ().getY () - 320 / 2 ;
71+ if (!swim .isActive (swimType )) {
72+ double dx = Game .getMouse ().getX () - 480 / 2d ;
73+ double dy = Game .getMouse ().getY () - 320 / 2d ;
8674 double dir = Math .atan2 (dy , dx );
8775 shoot (x , y , dir , Game .getMouse ().getButton (), false );
8876 }
8977 }
9078 }
9179
92- for (int i = 0 ; i < projectiles .size (); i ++) {
93- Projectile p = projectiles .get (i );
94- if (p .isRemoved ()){
95- projectiles .remove (i );
80+ for (Projectile p : projectiles ) {
81+ if (p .isRemoved ()) {
82+ p .remove ();
9683 Game .getLevel ().removeProjectileEntities (p );
9784 }
9885 }
9986
10087 if (xa != 0 || ya != 0 ) {
10188 move (xa , ya );
10289 isMoving = true ;
103- Game .getGame ();
104-
10590 } else {
10691 isMoving = false ;
10792 }
10893
109- setSwim ( new Swim (level , (int ) getX (), (int ) getY () ));
94+ swim = new Swim (level , (int ) getX (), (int ) getY ());
11095 swimType = swim .swimming (isSwimming , isMagma , isMuddy );
11196 isSwimming = swimType [0 ];
11297 isMagma = swimType [1 ];
@@ -130,15 +115,15 @@ public void render(Screen screen) {
130115
131116 if (movingDir == 1 ) {
132117 xTile += 2 ;
133- if (!isMoving || swim .isActive (swimType )){
118+ if (!isMoving || swim .isActive (swimType )) {
134119 yTile -= 2 ;
135120 }
136121 } else if (movingDir == 0 && !isMoving || movingDir == 0 && swim .isActive (swimType )) {
137122 yTile -= 2 ;
138123 } else if (movingDir > 1 ) {
139124 xTile += 4 + ((numSteps >> walkingSpeed ) & 1 ) * 2 ;
140125 flipTop = (movingDir - 1 ) % 2 ;
141- if (!isMoving ){
126+ if (!isMoving ) {
142127 xTile = 4 ;
143128 }
144129 }
@@ -151,44 +136,36 @@ public void render(Screen screen) {
151136 Game .setChangeLevel (true );
152137 }
153138
154- if (isSwimming || isMagma || isMuddy ){
155- swimColour = swim .waveCols (isSwimming , isMagma , isMuddy );
139+ if (isSwimming || isMagma || isMuddy ) {
140+ int [] swimColour = swim .waveCols (isSwimming , isMagma , isMuddy );
156141
157- int waterColour = 0 ;
142+ int waterColour ;
158143 yOffset += 4 ;
159144
160- colour = Colours .get (-1 , 111 , -1 , faceCol );
145+ colour = Colours .get (-1 , 111 , -1 , faceColour );
161146
162147 if (tickCount % 60 < 15 ) {
163148 waterColour = Colours .get (-1 , -1 , swimColour [0 ], -1 );
164- } else if (15 <= tickCount % 60 && tickCount % 60 < 30 ) {
149+ } else if (tickCount % 60 < 30 ) {
165150 yOffset --;
166151 waterColour = Colours .get (-1 , swimColour [1 ], swimColour [2 ], -1 );
167- } else if (30 <= tickCount % 60 && tickCount % 60 < 45 ) {
152+ } else if (tickCount % 60 < 45 ) {
168153 waterColour = Colours .get (-1 , swimColour [2 ], -1 , swimColour [1 ]);
169154 } else {
170155 yOffset --;
171156 waterColour = Colours .get (-1 , -1 , swimColour [1 ], swimColour [2 ]);
172157 }
173158
174- screen .render (xOffset , yOffset + 3 , 31 + 31 * 32 , waterColour ,
175- 0x00 , 1 );
176- screen .render (xOffset + 8 , yOffset + 3 , 31 + 31 * 32 , waterColour ,
177- 0x01 , 1 );
159+ screen .render (xOffset , yOffset + 3 , 31 + 31 * 32 , waterColour , 0x00 , 1 );
160+ screen .render (xOffset + 8 , yOffset + 3 , 31 + 31 * 32 , waterColour , 0x01 , 1 );
178161 }
179162
180- screen .render ((xOffset + (modifier * flipTop )), yOffset ,
181- (xTile + yTile * 32 ), colour , flipTop , scale );
182- screen .render ((xOffset + modifier - (modifier * flipTop )), yOffset ,
183- ((xTile + 1 ) + yTile * 32 ), colour , flipTop , scale );
163+ screen .render ((xOffset + (modifier * flipTop )), yOffset , (xTile + yTile * 32 ), colour , flipTop , scale );
164+ screen .render ((xOffset + modifier - (modifier * flipTop )), yOffset , ((xTile + 1 ) + yTile * 32 ), colour , flipTop , scale );
184165 if (!isSwimming && !isMagma && !isMuddy ) {
185- screen .render ((xOffset + (modifier * flipBottom )),
186- (yOffset + modifier ), (xTile + (yTile + 1 ) * 32 ), colour ,
187- flipBottom , scale );
188- screen .render ((xOffset + modifier - (modifier * flipBottom )),
189- (yOffset + modifier ), ((xTile + 1 ) + (yTile + 1 ) * 32 ),
190- colour , flipBottom , scale );
191- colour = Colours .get (-1 , 111 , shirtCol , faceCol );
166+ screen .render ((xOffset + (modifier * flipBottom )), (yOffset + modifier ), (xTile + (yTile + 1 ) * 32 ), colour , flipBottom , scale );
167+ screen .render ((xOffset + modifier - (modifier * flipBottom )), (yOffset + modifier ), ((xTile + 1 ) + (yTile + 1 ) * 32 ), colour , flipBottom , scale );
168+ colour = Colours .get (-1 , 111 , shirtColour , faceColour );
192169 }
193170
194171 if (userName != null ) {
@@ -198,11 +175,7 @@ public void render(Screen screen) {
198175 * -posmicanomaly
199176 */
200177
201- Font .render (userName ,
202- screen ,
203- (int )x - nameOffset ,
204- yOffset - 10 ,
205- Colours .get (-1 , -1 , -1 , 111 ), 1 );
178+ Font .render (userName , screen , (int ) x - nameOffset , yOffset - 10 , Colours .get (-1 , -1 , -1 , 111 ), 1 );
206179
207180 }
208181 }
@@ -219,6 +192,8 @@ public void setUsername(String name) {
219192 }
220193
221194 public String getSanitisedUsername () {
195+ // Need to add a class for constants
196+ int fontCharSize = 8 ;
222197 if (this .getUsername () == null || this .userName .isEmpty ()) {
223198 setUsername (guestPlayerName );
224199
@@ -227,20 +202,11 @@ public String getSanitisedUsername() {
227202 nameOffset = (userName .length () / 2 ) * fontCharSize - offsetUnit ;
228203
229204 return guestPlayerName ;
230- } else
231- if (nameOffset == 0 ){
232- int offsetUnit = ((userName .length () & 1 ) == 0 ? fontCharSize / 2 : 0 );
233- nameOffset = (userName .length () / 2 ) * fontCharSize - offsetUnit ;
234- }
235- return this .getUsername ();
236- }
237-
238- public Swim getSwim () {
239- return swim ;
240- }
241-
242- public void setSwim (Swim swim ) {
243- this .swim = swim ;
205+ } else if (nameOffset == 0 ) {
206+ int offsetUnit = ((userName .length () & 1 ) == 0 ? fontCharSize / 2 : 0 );
207+ nameOffset = (userName .length () / 2 ) * fontCharSize - offsetUnit ;
208+ }
209+ return this .getUsername ();
244210 }
245211
246212}
0 commit comments