Add more documentation

master
John 2022-06-20 16:26:54 -07:00
parent 38f6c935fd
commit 1cbfda13e0
3 changed files with 29 additions and 21 deletions

View File

@ -1,6 +1,5 @@
/* Eric Li, ICS4U, Completed 6/19/2022 // Eric Li, Charlie Zhao, ICS4U, Completed 6/19/2022
// the NonPlayer class defines behaviour for enemies and characters that are not controlled by the player
NonPlayer class defines behaviour for enemies and characters that are not controlled by the player */
import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
@ -9,25 +8,24 @@ import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
public class NonPlayer extends GenericSprite implements Serializable { public class NonPlayer extends GenericSprite implements Serializable {
// please note that these are not static, in contrast to the player class, as different enemies will have different heights // please note that these are not static, in contrast to the player class
// as different enemies have different heights
public int npcWidth; public int npcWidth;
public int npcHeight; public int npcHeight;
public int currentXDirection, currentYDirection; public int currentXDirection, currentYDirection;
public boolean isDead; public boolean isDead;
public int realX; public int realX;
public int health; public int health;
public double fadeCounter; public double fadeCounter;
public BufferedImageWrapper[][][] spriteArray; public BufferedImageWrapper[][][] spriteArray;
public NonPlayer(int x, int y, BufferedImageWrapper[][][] sprites, int npcWidth, int npcHeight, int health) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
public NonPlayer(int x, int y, BufferedImageWrapper[][][] sprites, int npcWidth, int npcHeight, int health) {
super(x, y, npcHeight, npcWidth); super(x, y, npcHeight, npcWidth);
// bump = new Sound("sound/bump.wav");
this.health = health; this.health = health;
spriteArray = sprites; spriteArray = sprites;
// TODO: remove
this.npcWidth = npcWidth; this.npcWidth = npcWidth;
WIDTH = npcWidth; WIDTH = npcWidth;
this.npcHeight = npcHeight; this.npcHeight = npcHeight;
@ -37,63 +35,73 @@ public class NonPlayer extends GenericSprite implements Serializable {
fadeCounter = 1; fadeCounter = 1;
} }
// check if the player is colliding with the enemy
public boolean collidePlayer(Player p){ public boolean collidePlayer(Player p){
if(realX+npcWidth>p.x&&realX<p.x+Player.PLAYER_WIDTH&&y-p.y<Player.PLAYER_HEIGHT&&p.y-y<npcHeight){ return realX + npcWidth > p.x && realX < p.x + Player.PLAYER_WIDTH && y - p.y < Player.PLAYER_HEIGHT && p.y - y < npcHeight;
return true;
}
return false;
} }
// update the realX value of the enemy (this is dependent on camera.x and is the x position seen by the player)
public void update(){ public void update(){
realX = x-GameFrame.game.camera.x; realX = x-GameFrame.game.camera.x;
} }
// move the enemy
public void move() throws UnsupportedAudioFileException, LineUnavailableException, IOException { public void move() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
// don't move the enemy if the enemy is dead
if (isDead) { if (isDead) {
xVelocity = 0; xVelocity = 0;
// return;
} }
// have the enemy go the opposite direction if the enemy would be colliding with a tile
if(!canUpdate(xVelocity, 0)){ if(!canUpdate(xVelocity, 0)){
xVelocity*=-1; xVelocity*=-1;
} }
// have the enemy stop falling if they are on the ground
if(!canUpdate(0, yVelocity)){ if(!canUpdate(0, yVelocity)){
if(yVelocity>0){ if(yVelocity>0){
while(canUpdate(0,1)){ while(canUpdate(0,1)){
y+=1; y+=1;
} }
isGrounded = true; isGrounded = true;
} else if(yVelocity<0){ } else if(yVelocity<0){ // have the enemy stop moving up if they hit something above them
while(canUpdate(0,-1)){ while(canUpdate(0,-1)){
y-=1; y-=1;
} }
} }
yVelocity = 0; yVelocity = 0;
} }
// move the object yVelocity and xVelocity pixels away from their current position
if(canUpdate(0, yVelocity)) { if(canUpdate(0, yVelocity)) {
y = y + (int) yVelocity; y = y + (int) yVelocity;
x = x + (int) xVelocity; x = x + (int) xVelocity;
} }
// simulate gravity
if(!isGrounded) { if(!isGrounded) {
yVelocity += 0.3; yVelocity += 0.3;
} }
// cap maximum speed
capSpeed(); capSpeed();
} }
public int draw(Graphics g, int frame) { public int draw(Graphics g, int frame) {
// if the enemy is not dead, animate the enemy sprite by cycling through the frames of the enemy
if (!isDead) { if (!isDead) {
// last frame is reserved for death animation // last frame is reserved for death animation
frame %= spriteArray[0][0].length - 1; frame %= spriteArray[0][0].length - 1;
// save the current x and y directions so the enemy is facing the right way if it dies
currentXDirection = (int)(Math.signum(xVelocity) + 1) / 2; currentXDirection = (int)(Math.signum(xVelocity) + 1) / 2;
currentYDirection = (int)(Math.signum(yVelocity) + 1) / 2; currentYDirection = (int)(Math.signum(yVelocity) + 1) / 2;
// x-GameFrame.game.camera.x is used as the camera doesn't follow NPCs // x-GameFrame.game.camera.x is used as the camera doesn't follow NPCs
// draw the enemy
g.drawImage(spriteArray[currentXDirection][currentYDirection][frame].image, x-GameFrame.game.camera.x, y, null); g.drawImage(spriteArray[currentXDirection][currentYDirection][frame].image, x-GameFrame.game.camera.x, y, null);
return 1; return 1;
} else { } else {
// fade the enemy slowly by overlaying a composite on the enemy
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)fadeCounter)); ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)fadeCounter));
// draw faded enemy
g.drawImage(spriteArray[currentXDirection][currentYDirection][spriteArray[0][0].length-1].image, x-GameFrame.game.camera.x, (int)(y+HEIGHT/1.7), null); g.drawImage(spriteArray[currentXDirection][currentYDirection][spriteArray[0][0].length-1].image, x-GameFrame.game.camera.x, (int)(y+HEIGHT/1.7), null);
// reset composite to not affect other sprites being drawn // reset composite to not affect other sprites being drawn
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1)); ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
// increase the amount of fade every tick to a maximum fade amount of 100% (total transparency)
fadeCounter = Math.max(0, fadeCounter-0.01); fadeCounter = Math.max(0, fadeCounter-0.01);
return 0; return 0;
} }

View File

@ -1,10 +1,11 @@
// Eric Li, Charlie Zhao, ICS4U, Finished 6/18/22
// create particles when the character jumps or on top of lava
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
public class Particle extends GenericSprite implements Serializable { public class Particle extends GenericSprite implements Serializable {
public static final int small = 3;
public static final int big = 10;
public int xVelocity; public int xVelocity;

View File

@ -1,6 +1,5 @@
/* Eric Li, ICS4U, Completed 5/29/2022 // Eric Li, Charlie Zhao, ICS4U, Completed 6/20/2022
// Player class defines behaviours for the player controlled character
Paddle class defines behaviours for the left and right player-controlled paddles */
import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;