You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
4.6 KiB
204 lines
4.6 KiB
/** |
|
* Copyright 2022 Jamie Munro, All rights reserved |
|
* CS5041 P1 |
|
* Game 1 |
|
*/ |
|
|
|
/* |
|
* Which engines are firing |
|
*/ |
|
public enum EngineYState { |
|
NONE, FORWARDS, REVERSE |
|
} |
|
|
|
public enum EngineXState { |
|
NONE, LEFT, RIGHT |
|
} |
|
|
|
/** |
|
* Player class |
|
* Extends camera (as camera follows player) and represents a player |
|
*/ |
|
class Player extends Camera { |
|
private final boolean GOD_MODE = false; |
|
|
|
private int scaleFactor; |
|
private PImage sprites[]; |
|
private EngineXState engineXState; |
|
private EngineYState engineYState; |
|
|
|
private float momentum; |
|
|
|
private long hitTime; |
|
|
|
/** |
|
* Constructor |
|
* @param initialX starting x position of player |
|
* @param initialY starting y position of player |
|
* @param initialRotation starting rotation of player |
|
*/ |
|
public Player(int initialX, int initialY, int initialRotation, PImage image[]) { |
|
super(initialX, initialY, initialRotation); |
|
this.scaleFactor = conf.playerScaleFactor; |
|
this.sprites = image; |
|
this.engineXState = EngineXState.NONE; |
|
this.engineYState = EngineYState.NONE; |
|
|
|
this.momentum = 0; |
|
|
|
this.hitTime = millis(); |
|
} |
|
|
|
/** |
|
* get scale factor |
|
* @param multiple of original sprite |
|
*/ |
|
public int getScaleFactor() { |
|
return this.scaleFactor; |
|
} |
|
|
|
/** |
|
* get sheild |
|
*/ |
|
public boolean getSheild() { |
|
if (GOD_MODE) return true; |
|
long currentTime = millis(); |
|
return (currentTime - hitTime <= conf.shieldTime); |
|
} |
|
|
|
/** |
|
* Set position |
|
* @param x new x-position |
|
* @param y new y-position |
|
*/ |
|
public void setPosition(int x, int y) { |
|
this.x = x; |
|
this.y = y; |
|
} |
|
|
|
/** |
|
* set rotation |
|
* @param degrees new rotation in degrees |
|
*/ |
|
public void setRotation(int degrees) { |
|
if ((degrees >= 0) && (degrees <= 360)) { |
|
this.rotation = degrees; |
|
} |
|
} |
|
|
|
/** |
|
* Gets the player sprite |
|
* @returns sprite PImage |
|
*/ |
|
public PImage getSprite() { |
|
int index; |
|
|
|
//just one engine is firing |
|
if ((this.engineYState == EngineYState.FORWARDS) && (this.engineXState == EngineXState.NONE)) { |
|
index = 1; |
|
} |
|
|
|
else if ((this.engineYState == EngineYState.REVERSE) && (this.engineXState == EngineXState.NONE)) { |
|
index = 2; |
|
} |
|
|
|
else if ((this.engineYState == EngineYState.NONE) && (this.engineXState == EngineXState.LEFT)) { |
|
index = 3; |
|
} |
|
|
|
else if ((this.engineYState == EngineYState.NONE) && (this.engineXState == EngineXState.RIGHT)) { |
|
index = 4; |
|
} |
|
|
|
//combinations of engines |
|
else if ((this.engineYState == EngineYState.FORWARDS) && (this.engineXState == EngineXState.LEFT)) { |
|
index = 5; |
|
} |
|
|
|
else if ((this.engineYState == EngineYState.FORWARDS) && (this.engineXState == EngineXState.RIGHT)) { |
|
index = 6; |
|
} |
|
|
|
else if ((this.engineYState == EngineYState.REVERSE) && (this.engineXState == EngineXState.LEFT)) { |
|
index = 7; |
|
} |
|
|
|
else if ((this.engineYState == EngineYState.REVERSE) && (this.engineXState == EngineXState.RIGHT)) { |
|
index = 8; |
|
} |
|
|
|
else { |
|
index = 0; |
|
} |
|
|
|
return sprites[index]; |
|
} |
|
|
|
/** |
|
* getter for momentum |
|
*/ |
|
public float getMomentum() { |
|
return this.momentum; |
|
} |
|
|
|
public void fireMainEngine() { |
|
this.engineYState = EngineYState.FORWARDS; |
|
this.momentum += conf.mainEngineForce; |
|
|
|
if (this.momentum > conf.maxForwardsMomentum) { |
|
this.momentum = conf.maxForwardsMomentum; |
|
} |
|
} |
|
|
|
public void fireReverseEngine() { |
|
this.engineYState = EngineYState.REVERSE; |
|
this.momentum -= conf.reverseEngineForce; |
|
|
|
if (this.momentum < -conf.maxRearMomentum) { |
|
this.momentum = -conf.maxRearMomentum; |
|
} |
|
} |
|
|
|
public void fireLeftEngine() { |
|
this.engineXState = EngineXState.LEFT; |
|
|
|
int newRotation = player.getRotation() + conf.rotationSpeed; |
|
if (newRotation > 360) newRotation -= 360; |
|
player.setRotation(newRotation); |
|
} |
|
|
|
public void fireRightEngine() { |
|
this.engineXState = EngineXState.RIGHT; |
|
|
|
int newRotation = this.getRotation() - conf.rotationSpeed; |
|
if (newRotation < 0) newRotation += 360; |
|
this.setRotation(newRotation); |
|
} |
|
|
|
public void shutdownXEngines() { |
|
this.engineXState = EngineXState.NONE; |
|
} |
|
|
|
public void shutdownYEngines() { |
|
this.engineYState = EngineYState.NONE; |
|
|
|
if (this.momentum < 0) { |
|
this.momentum += conf.friction; |
|
|
|
if (momentum > 0) { |
|
momentum = 0; |
|
} |
|
} |
|
else { |
|
this.momentum -= conf.friction; |
|
|
|
if (momentum < 0) { |
|
momentum = 0; |
|
} |
|
} |
|
} |
|
|
|
public void hit() { |
|
this.hitTime = millis(); |
|
} |
|
}
|
|
|