master
bob 2022-06-07 13:12:48 -04:00
parent 424e71eb74
commit e856a93fb9
6 changed files with 119 additions and 23 deletions

View File

@ -40,6 +40,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public static ArrayList<Tile>map = new ArrayList<Tile>();
public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
public static StickyBomb b;
public static Camera camera;
// image imports begin here
@ -85,6 +86,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
player.mousePressed(e);
b.mousePressed(e);
}
});
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
@ -114,6 +116,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
enemy.get(i).draw(g, enemyFrame);
}
playerFrameCounter += player.draw(g, playerFrame);
b.draw(g);
g.drawString(camera.x+" "+player.y,100,100);
}
@ -124,6 +127,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
for (NonPlayer n: enemy) {
n.move();
}
b.move();
}
//handles all collision detection and responds accordingly
@ -165,7 +169,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){
b = new StickyBomb(600, 100, 20, 1,-5);
LevelManager.setLevel(1);
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
long lastTime = System.nanoTime();
double amountOfTicks = 60;

View File

@ -28,6 +28,7 @@ public class GenericSprite extends Rectangle{
HEIGHT = height;
}
//called from GamePanel when any keyboard input is detected
//updates the direction of the ball based on user input
//if the keyboard input isn't any of the options (d, a, w, s), then nothing happens
@ -60,7 +61,25 @@ public class GenericSprite extends Rectangle{
}
}
private boolean collide(Tile tile, double x, double y){
if(!tile.collision){
return false;
}
if(x+WIDTH>tile.x&&x<tile.x+Tile.length&&y-tile.y<Tile.length&&tile.y-y<HEIGHT){
return true;
}
return false;
}
public boolean canUpdate(double x, double y){
boolean canUpdate = true;
for(Tile i: GamePanel.map){
if(collide(i,this.x+x,this.y+y)){
canUpdate = false;
break;
}
}
return canUpdate;
}
//called frequently from the GamePanel class
//draws the current location of the ball to the screen
public void draw(Graphics g){

View File

@ -65,7 +65,7 @@ public class MapReader {
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/box.png")));
map.get(map.size()-1).collision = false;
} else if(file.charAt(i)=='!'){
enemy.add(new NonPlayer(x, y, GamePanel.slimeSpriteArray, 50, 28));
enemy.add(new NonPlayer(x, y, GamePanel.slimeSpriteArray, 50, 28, 100));
} else if(file.charAt(i)=='+') {
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/finish.png")));

View File

@ -18,13 +18,16 @@ public class NonPlayer extends GenericSprite {
public int realX;
public int health;
// private final Sound bump;
public BufferedImage[][][] spriteArray;
public NonPlayer(int x, int y, BufferedImage[][][] sprites, int npcWidth, int npcHeight) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
public NonPlayer(int x, int y, BufferedImage[][][] sprites, int npcWidth, int npcHeight, int health) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
super(x, y, npcHeight, npcWidth);
// bump = new Sound("sound/bump.wav");
this.health = health;
spriteArray = sprites;
// TODO: remove
this.npcWidth = npcWidth;
@ -34,15 +37,7 @@ public class NonPlayer extends GenericSprite {
xVelocity = 3;
}
private boolean collide(Tile tile, double x, double y){
if(!tile.collision){
return false;
}
if(x+npcWidth>tile.x&&x<tile.x+Tile.length&&y-tile.y<Tile.length&&tile.y-y<npcHeight){
return true;
}
return false;
}
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){
@ -50,16 +45,7 @@ public class NonPlayer extends GenericSprite {
}
return false;
}
public boolean canUpdate(double x, double y){
boolean canUpdate = true;
for(Tile i: GamePanel.map){
if(collide(i,this.x+x,this.y+y)){
canUpdate = false;
break;
}
}
return canUpdate;
}
public void update(){
realX = x-GamePanel.camera.x;

View File

@ -78,7 +78,7 @@ public class Player extends GenericSprite {
if(!tile.collision){
return false;
}
if(x+PLAYER_WIDTH>tile.realX&&x<tile.realX+Tile.length&&y-tile.y<Tile.length&&tile.y-y<PLAYER_HEIGHT){
if(x+WIDTH>tile.realX&&x<tile.realX+Tile.length&&y-tile.y<Tile.length&&tile.y-y<HEIGHT){
return true;
}
return false;

85
src/StickyBomb.java Normal file
View File

@ -0,0 +1,85 @@
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
public class StickyBomb extends GenericSprite{
public int length;
public int xVelocity;
public int yVelocity;
public boolean isMove;
public int realX;
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity){
super(x,y,length,length);
this.length = length;
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
isMove = true;
}
public void update(){
realX = x - GamePanel.camera.x;
}
public void move(){
update();
if(isMove) {
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
x += -Math.signum(xVelocity);
//isMove = false;
}
if(!canUpdate(xVelocity, 0)){
isMove = false;
int updateAmount = 0;
if(xVelocity>0){
while(canUpdate(updateAmount, 0)){
updateAmount++;
}
x+=updateAmount-1;
} else if(xVelocity<0){
while(canUpdate(updateAmount, 0)){
updateAmount--;
}
x+=updateAmount+1;
}
xVelocity = 0;
}
if(!canUpdate(0, yVelocity)){
isMove = false;
if(yVelocity>0){
while(canUpdate(0,1)){
y+=1;
}
isGrounded = true;
} else if(yVelocity<0){
while(canUpdate(0,-1)){
y-=1;
}
}
yVelocity = 0;
}
if(canUpdate(xVelocity, yVelocity)) {
y = y + (int) yVelocity;
x = x + (int) xVelocity;
} else {
isMove = false;
}
yVelocity+=3;
}
}
public void mousePressed(MouseEvent e){
int xx = e.getX();
int yy = e.getY();
GamePanel.b = new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35, (xx-GamePanel.player.x-GamePanel.camera.x)/20, (yy-GamePanel.player.y)/10);
}
public void draw(Graphics g){
g.drawRect(x-GamePanel.camera.x,y,length,length);
}
}