Skip to content

Commit

Permalink
Caixas de colisoes P1 a P4
Browse files Browse the repository at this point in the history
  • Loading branch information
elisandro-tnb committed Aug 8, 2021
1 parent c6df8e1 commit d12c326
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 16 deletions.
98 changes: 90 additions & 8 deletions src/com/ks2002br/entities/Player.java
Expand Up @@ -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<GameObject> obj) {
x += spdX;
y += spdY;

verificarColisao(obj);

}

private void verificarColisao(LinkedList<GameObject> 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);
}

}
11 changes: 9 additions & 2 deletions src/com/ks2002br/frameworks/Bloco.java
Expand Up @@ -2,24 +2,31 @@

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.LinkedList;

public class Bloco extends GameObject{

public Bloco(float x, float y, ObjectId id) {
super(x, y, id);
}

public void tick() {
public void tick(LinkedList<GameObject> 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);
}



Expand Down
10 changes: 9 additions & 1 deletion src/com/ks2002br/frameworks/Cobaia.java
Expand Up @@ -2,14 +2,16 @@

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.LinkedList;

public class Cobaia extends GameObject {

public Cobaia(float x, float y, ObjectId id) {
super(x, y, id);
}

public void tick() {
public void tick(LinkedList<GameObject> obj) {
x+=spdX;
y+=spdY;
}
Expand All @@ -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);
}

}
2 changes: 1 addition & 1 deletion src/com/ks2002br/frameworks/GameController.java
Expand Up @@ -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);
}
}

Expand Down
9 changes: 7 additions & 2 deletions 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 {

Expand All @@ -15,8 +19,9 @@ public GameObject(float x, float y, ObjectId id) {
this.id = id;
}

public abstract void tick();
public abstract void tick(LinkedList<GameObject> obj);
public abstract void render(Graphics g);
public abstract Rectangle getBounds();

public ObjectId getId() {
return id;
Expand Down
6 changes: 4 additions & 2 deletions src/com/ks2002br/game/Game.java
Expand Up @@ -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));

}

Expand Down

0 comments on commit d12c326

Please sign in to comment.