From d12c326ee7e5d6768931aa8865a2ccb38e68573d Mon Sep 17 00:00:00 2001 From: Elisandro Rodrigues Date: Sun, 8 Aug 2021 15:58:29 -0300 Subject: [PATCH] Caixas de colisoes P1 a P4 --- src/com/ks2002br/entities/Player.java | 98 +++++++++++++++++-- src/com/ks2002br/frameworks/Bloco.java | 11 ++- src/com/ks2002br/frameworks/Cobaia.java | 10 +- .../ks2002br/frameworks/GameController.java | 2 +- src/com/ks2002br/frameworks/GameObject.java | 9 +- src/com/ks2002br/game/Game.java | 6 +- 6 files changed, 120 insertions(+), 16 deletions(-) diff --git a/src/com/ks2002br/entities/Player.java b/src/com/ks2002br/entities/Player.java index 4cf126c..daaa4dd 100644 --- a/src/com/ks2002br/entities/Player.java +++ b/src/com/ks2002br/entities/Player.java @@ -2,25 +2,107 @@ import java.awt.Color; import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.util.LinkedList; +import com.ks2002br.frameworks.GameController; import com.ks2002br.frameworks.GameObject; import com.ks2002br.frameworks.ObjectId; public class Player extends GameObject { - public Player(float x, float y, ObjectId id) { - super(x, y, id); + private int width = 64, height = 128; // Largura e altura do player ( obj) + private int colW = width, colH = height; // Largura e altura da caixa de colisao + + private GameController gc; + + public Player(float x, float y, ObjectId id, GameController gc) { + super(x, y, id); + this.gc = gc; } - public void tick() { - x+=spdX; - y+=spdY; + public void tick(LinkedList obj) { + x += spdX; + y += spdY; + + verificarColisao(obj); + } + private void verificarColisao(LinkedList obj) { + for (int i = 0; i < gc.obj.size(); i++) { + GameObject tempObj = gc.obj.get(i); + if (tempObj.getId() == ObjectId.BLOCO) { + //topo + if (getBounds().intersects(tempObj.getBounds())) { + System.out.println("Caixa TOP BATEU NUM BLOCO QUALQUER"); + } + //pes + else if (getBoundsBaixo().intersects(tempObj.getBounds())) { + System.out.println("Caixa BAIXO BATEU NUM BLOCO QUALQUER"); + } + //esq + else if (getBoundsEsq().intersects(tempObj.getBounds())) { + System.out.println("Caixa ESQ BATEU NUM BLOCO QUALQUER"); + } + //dir + else if (getBoundsDir().intersects(tempObj.getBounds())) { + System.out.println("Caixa DIR BATEU NUM BLOCO QUALQUER"); + } + + } + + //colisao cobaia + else if(tempObj.getId() == ObjectId.COBAIA) { + if (getBounds().intersects(tempObj.getBounds())) System.out.println("CABECADA NO COBAIA"); + + else if (getBoundsBaixo().intersects(tempObj.getBounds())) gc.removeObj(gc.obj.get(i)); + else if (getBoundsEsq() .intersects(tempObj.getBounds())) gc.obj.get(i).setSpdX(-5); + else if (getBoundsDir() .intersects(tempObj.getBounds())) gc.obj.get(i).setSpdX( 5); + + } + + } + + } public void render(Graphics g) { g.setColor(Color.white); - g.fillRect((int)x, (int)y,64, 64); - } - + g.fillRect((int) x, (int) y, width, height); + + Graphics2D g2d = (Graphics2D) g; + + // REDERIZACAO DAS CAIXAS DE COLISOES + // TOPO + g.setColor(Color.RED); + g2d.draw(getBounds()); + + g.setColor(Color.GREEN); + g2d.draw(getBoundsBaixo()); + + g.setColor(Color.MAGENTA); + g2d.draw(getBoundsDir()); + + g.setColor(Color.CYAN); + g2d.draw(getBoundsEsq()); + + } + + public Rectangle getBounds() { + return new Rectangle((int) x + colW / 2 - (colW / 2) / 2, (int) y, colW / 2, colH / 2 - 1); + } + + public Rectangle getBoundsBaixo() { + return new Rectangle((int) x + colW / 2 - (colW / 2) / 2, (int) y + colH / 2, colW / 2, colH / 2 - 1); + } + + public Rectangle getBoundsDir() { + return new Rectangle((int) x + colW - 6, (int) y + 5, 5, colH - 10); + } + + public Rectangle getBoundsEsq() { + return new Rectangle((int) x, (int) y + 5, 5, colH - 10); + } + } diff --git a/src/com/ks2002br/frameworks/Bloco.java b/src/com/ks2002br/frameworks/Bloco.java index 1bc5182..645751e 100644 --- a/src/com/ks2002br/frameworks/Bloco.java +++ b/src/com/ks2002br/frameworks/Bloco.java @@ -2,6 +2,8 @@ import java.awt.Color; import java.awt.Graphics; +import java.awt.Rectangle; +import java.util.LinkedList; public class Bloco extends GameObject{ @@ -9,17 +11,22 @@ public Bloco(float x, float y, ObjectId id) { super(x, y, id); } - public void tick() { + public void tick(LinkedList obj) { } public void render(Graphics g) { g.setColor(Color.GREEN); g.fillRect((int)x,(int)y, 32, 32); //divisas do bloco - g.setColor(Color.BLACK); + g.setColor(Color.BLACK); //Sera a marca da caixa se colisao g.drawRect((int)x,(int)y, 32, 32); } + + @Override + public Rectangle getBounds() { + return new Rectangle((int) x , (int) y,32,32); + } diff --git a/src/com/ks2002br/frameworks/Cobaia.java b/src/com/ks2002br/frameworks/Cobaia.java index 93a041b..4ae36c1 100644 --- a/src/com/ks2002br/frameworks/Cobaia.java +++ b/src/com/ks2002br/frameworks/Cobaia.java @@ -2,6 +2,8 @@ import java.awt.Color; import java.awt.Graphics; +import java.awt.Rectangle; +import java.util.LinkedList; public class Cobaia extends GameObject { @@ -9,7 +11,7 @@ public Cobaia(float x, float y, ObjectId id) { super(x, y, id); } - public void tick() { + public void tick(LinkedList obj) { x+=spdX; y+=spdY; } @@ -18,6 +20,12 @@ public void tick() { public void render(Graphics g) { g.setColor(Color.blue); g.fillRect((int)x, (int)y,64, 64); + } + + @Override + public Rectangle getBounds() { + + return new Rectangle((int) x , (int) y,64,64); } } diff --git a/src/com/ks2002br/frameworks/GameController.java b/src/com/ks2002br/frameworks/GameController.java index 73144d5..87c2e22 100644 --- a/src/com/ks2002br/frameworks/GameController.java +++ b/src/com/ks2002br/frameworks/GameController.java @@ -14,7 +14,7 @@ public class GameController { public void update() { for (int i = 0; i < obj.size(); i++) { tempObj = obj.get(i); - tempObj.tick(); + tempObj.tick(obj); } } diff --git a/src/com/ks2002br/frameworks/GameObject.java b/src/com/ks2002br/frameworks/GameObject.java index 7ab646b..de7e371 100644 --- a/src/com/ks2002br/frameworks/GameObject.java +++ b/src/com/ks2002br/frameworks/GameObject.java @@ -1,6 +1,10 @@ package com.ks2002br.frameworks; - +/* + * By Elisandro + */ import java.awt.Graphics; +import java.awt.Rectangle; +import java.util.LinkedList; public abstract class GameObject { @@ -15,8 +19,9 @@ public GameObject(float x, float y, ObjectId id) { this.id = id; } - public abstract void tick(); + public abstract void tick(LinkedList obj); public abstract void render(Graphics g); + public abstract Rectangle getBounds(); public ObjectId getId() { return id; diff --git a/src/com/ks2002br/game/Game.java b/src/com/ks2002br/game/Game.java index 9cf2a14..4e6dd3a 100644 --- a/src/com/ks2002br/game/Game.java +++ b/src/com/ks2002br/game/Game.java @@ -54,8 +54,10 @@ private void startGame() { //OBJETOS AQUI gc.criarMundo(); - gc.addObj(new Cobaia(220, 250,ObjectId.COBAIA)); - gc.addObj(new Player(120, 450,ObjectId.PLAYER)); + gc.addObj(new Cobaia(220, 350,ObjectId.COBAIA)); + gc.addObj(new Cobaia(320, 150,ObjectId.COBAIA)); + gc.addObj(new Cobaia( 80, 80,ObjectId.COBAIA)); + gc.addObj(new Player(120, 450,ObjectId.PLAYER,gc)); }