Files
american-identity-project/Player.java
2026-04-15 19:50:15 -05:00

109 lines
3.0 KiB
Java

import javax.swing.ImageIcon;
import java.util.*;
public class Player extends Collidable {
static final int JUMP_HEIGHT = 10;
static final int POWER_DURATION = 2000; // 2000 ticks ≈ 30 seconds
static final int I_FRAMES = 67; // ~1 second
static final int SHOOT_COOLDOWN = 33; //~0.5 second
int health;
int yVelo;
int xVelo;
boolean onGround;
int curPower;
int powerTimer;
int airJumps;
int facing; // 1 is right -1 is left
int invincibleTimer;
int shootCooldown;
int numAmendments;
public Player(int x, int y, int w, int h){
super(x,y,w,h,new ImageIcon("Sprites/Player/1.png"));
xVelo = 0;
yVelo = 0;
onGround = false;
curPower = 0;
powerTimer = 0;
airJumps = 0;
invincibleTimer = 0;
shootCooldown = 0;
health = 3;
facing = 1;
numAmendments = 0;
}
public void moveX(int moveX){
this.x += moveX;
this.rect.x = this.x;
}
public void moveY(int moveY){
this.y += moveY;
this.rect.y = this.y;
}
public void setLevel(int level){
this.icon = new ImageIcon("Sprites/Player/" + level + ".png");
}
public void onCollideX(Collidable other){
if (other instanceof Tile){
Tile t = (Tile) other;
int playerCenterX = this.x + this.width / 2;
int tileCenterX = t.x + t.width / 2;
if (playerCenterX > tileCenterX) { //player on right side of tiile
this.x = t.x + t.width;
}
else { //player on left side of tile
this.x = t.x - this.width;
}
this.xVelo = 0;
this.rect.x = this.x;
}
}
public void onCollideY(Collidable other, ArrayList<Collectable> collectables){
if (other instanceof Tile){
Tile t = (Tile) other;
if (this.yVelo >= 0) { // falling down, land on top of tile
this.y = t.y - this.height;
onGround = true;
airJumps = 0;
}
else { // moving up, hit underside of tile
this.y = t.y + t.height;
if (other instanceof PowerBrick){
PowerBrick pb = (PowerBrick) other;
if(!pb.hit){
pb.spawnPower(collectables);
}
}
}
this.yVelo = 0;
this.rect.y = this.y;
}
}
public void takeDamage() {
if (invincibleTimer <= 0) {
health--;
invincibleTimer = I_FRAMES;
}
}
public void reset(){
this.rect.x = this.x;
this.rect.y = this.y;
this.xVelo = this.yVelo = 0;
this.curPower = 0;
this.powerTimer = 0;
this.airJumps = 0;
this.onGround = false;
this.invincibleTimer = 0;
this.shootCooldown = 0;
this.facing = 1;
this.numAmendments = 0;
}
}