Skip to content
Permalink
9d56b929ea
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
431 lines (344 sloc) 8.13 KB
package Entity;
import TileMap.*;
import Audio.AudioPlayer;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
public class Knuckles extends MapObject {
// player stuff
private int health;
private int maxHealth;
private int fire;
private int maxFire;
private boolean dead;
private boolean flinching;
private long flinchTimer;
// blast
private boolean firing;
private int fireCost;
private int fireBallDamage;
private ArrayList<Blast> blasts;
// punch
private boolean punching;
private int punchDamage;
private int punchRange;
// gliding
private boolean gliding;
// animations
private ArrayList<BufferedImage[]> sprites;
private final int[] numFrames = {
7, 8, 1, 3, 1, 2,
};
// animation actions
private static final int IDLE = 0;
private static final int WALKING = 1;
private static final int JUMPING = 2;
private static final int FALLING = 3;
private static final int GLIDING = 4;
private static final int BLAST = 5;
private static final int PUNCHING = 6;
private HashMap<String, AudioPlayer> sfx;
public Knuckles(TileMap tm) {
super(tm);
width = 96;
height = 96;
cwidth = 80;
cheight = 80;
moveSpeed = 4.0;
maxSpeed = 7.6;
stopSpeed = 0.4;
fallSpeed = .15;
maxFallSpeed = 6.0;
jumpStart = -7.8;
stopJumpSpeed = 0.3;
facingRight = true;
health = maxHealth = 5;
fire = maxFire = 2500;
fireCost = 200;
fireBallDamage = 5;
blasts = new ArrayList<Blast>();
punchDamage = 8;
punchRange = 40;
// load sprites
try {
BufferedImage spritesheet = ImageIO.read(
getClass().getResourceAsStream(
"/Sprites/Player/knucks.gif"
)
);
sprites = new ArrayList<BufferedImage[]>();
for(int i = 0; i < 9; i++) {
BufferedImage[] bi =
new BufferedImage[numFrames[i]];
for(int j = 0; j < numFrames[i]; j++) {
if(i != PUNCHING) {
bi[j] = spritesheet.getSubimage(
j * width,
i * height,
width,
height
);
}
else {
bi[j] = spritesheet.getSubimage(
j * width * 2,
i * height,
width * 2,
height
);
}
}
sprites.add(bi);
}
}
catch(Exception e) {
e.printStackTrace();
}
animation = new Animation();
currentAction = IDLE;
animation.setFrames(sprites.get(IDLE));
animation.setDelay(400);
sfx = new HashMap<String, AudioPlayer>();
sfx.put("jump", new AudioPlayer("/SFX/jump.mp3"));
sfx.put("scratch", new AudioPlayer("/SFX/punch.mp3"));
}
public int getHealth() { return health; }
public int getMaxHealth() { return maxHealth; }
public int getFire() { return fire; }
public int getMaxFire() { return maxFire; }
public void setFiring() {
firing = true;
}
public void setPunching() {
punching = true;
}
public void setGliding(boolean b) {
gliding = b;
}
public void checkAttack(ArrayList<Enemy> enemies) {
// loop through enemies
for(int i = 0; i < enemies.size(); i++) {
Enemy e = enemies.get(i);
// scratch attack
if(punching) {
if(facingRight) {
if(
e.getx() > x &&
e.getx() < x + punchRange &&
e.gety() > y - height / 2 &&
e.gety() < y + height / 2
) {
e.hit(punchDamage);
}
}
else {
if(
e.getx() < x &&
e.getx() > x - punchRange &&
e.gety() > y - height / 2 &&
e.gety() < y + height / 2
) {
e.hit(punchDamage);
}
}
}
// blasts
for(int j = 0; j < blasts.size(); j++) {
if(blasts.get(j).intersects(e)) {
e.hit(fireBallDamage);
blasts.get(j).setHit();
break;
}
}
// check enemy collision
if(intersects(e)) {
hit(e.getDamage());
}
}
}
public void hit(int damage) {
if(flinching) return;
health -= damage;
if(health < 0) health = 0;
if(health == 0) dead = true;
flinching = true;
flinchTimer = System.nanoTime();
}
private void getNextPosition() {
// movement
if(left) {
dx -= moveSpeed;
if(dx < -maxSpeed) {
dx = -maxSpeed;
}
}
else if(right) {
dx += moveSpeed;
if(dx > maxSpeed) {
dx = maxSpeed;
}
}
else {
if(dx > 0) {
dx -= stopSpeed;
if(dx < 0) {
dx = 0;
}
}
else if(dx < 0) {
dx += stopSpeed;
if(dx > 0) {
dx = 0;
}
}
}
// cannot move while attacking, except in air
if(
(currentAction == PUNCHING || currentAction == BLAST) &&
!(jumping || falling)) {
dx = 0;
}
// jumping
if(jumping && !falling) {
sfx.get("jump").play();
dy = jumpStart;
falling = true;
}
// falling
if(falling) {
if(dy > 0 && gliding) dy += fallSpeed * 0.1;
else dy += fallSpeed;
if(dy > 0) jumping = false;
if(dy < 0 && !jumping) dy += stopJumpSpeed;
if(dy > maxFallSpeed) dy = maxFallSpeed;
}
}
public boolean isDead(){
if (this.getHealth() == 0){
return true;
}
return false;
}
public void update() {
// update position
getNextPosition();
checkTileMapCollision();
setPosition(xtemp, ytemp);
// check attack has stopped
if(currentAction == PUNCHING) {
if(animation.hasPlayedOnce()) punching = false;
}
if(currentAction == BLAST) {
if(animation.hasPlayedOnce()) firing = false;
}
// blast attack
fire += 1;
if(fire > maxFire) fire = maxFire;
if(firing && currentAction != BLAST) {
if(fire > fireCost) {
fire -= fireCost;
Blast fb = new Blast(tileMap, facingRight);
fb.setPosition(x, y);
blasts.add(fb);
}
}
// update blasts
for(int i = 0; i < blasts.size(); i++) {
blasts.get(i).update();
if(blasts.get(i).shouldRemove()) {
blasts.remove(i);
i--;
}
}
// check done flinching
if(flinching) {
long elapsed =
(System.nanoTime() - flinchTimer) / 1000000;
if(elapsed > 1000) {
flinching = false;
}
}
// set animation
if(punching) {
if(currentAction != PUNCHING) {
sfx.get("scratch").play();
currentAction = PUNCHING;
animation.setFrames(sprites.get(PUNCHING));
animation.setDelay(50);
width = 60;
}
}
else if(firing) {
if(currentAction != BLAST) {
currentAction = BLAST;
animation.setFrames(sprites.get(BLAST));
animation.setDelay(100);
width = 30;
}
}
else if(dy > 0) {
if(gliding) {
if(currentAction != GLIDING) {
currentAction = GLIDING;
animation.setFrames(sprites.get(GLIDING));
animation.setDelay(100);
width = 30;
}
}
else if(currentAction != FALLING) {
currentAction = FALLING;
animation.setFrames(sprites.get(FALLING));
animation.setDelay(100);
width = 30;
}
}
else if(dy < 0) {
if(currentAction != JUMPING) {
currentAction = JUMPING;
animation.setFrames(sprites.get(JUMPING));
animation.setDelay(-1);
width = 30;
}
}
else if(left || right) {
if(currentAction != WALKING) {
currentAction = WALKING;
animation.setFrames(sprites.get(WALKING));
animation.setDelay(40);
width = 30;
}
}
else {
if(currentAction != IDLE) {
currentAction = IDLE;
animation.setFrames(sprites.get(IDLE));
animation.setDelay(400);
width = 30;
}
}
animation.update();
// set direction
if(currentAction != PUNCHING && currentAction != BLAST) {
if(right) facingRight = true;
if(left) facingRight = false;
}
}
public void draw(Graphics2D g) {
setMapPosition();
// draw blasts
for(int i = 0; i < blasts.size(); i++) {
blasts.get(i).draw(g);
}
// draw player
if(flinching) {
long elapsed =
(System.nanoTime() - flinchTimer) / 1000000;
if(elapsed / 100 % 2 == 0) {
return;
}
}
super.draw(g);
}
}