Skip to content

Commit

Permalink
Revisao Geral - 2021/2022
Browse files Browse the repository at this point in the history
  • Loading branch information
elisandro-tnb committed Jan 2, 2022
1 parent 2ff6003 commit 9cf6d1e
Show file tree
Hide file tree
Showing 23 changed files with 471 additions and 494 deletions.
59 changes: 59 additions & 0 deletions src/com/ks2002br/entities/Bullet.java
@@ -0,0 +1,59 @@
package com.ks2002br.entities;

/*
* By Elisandro 12/2021 revisao geral
*/
import java.awt.*;
import java.util.LinkedList;

import com.ks2002br.frameworks.*;

public class Bullet extends GameObject {

private GameController gc;

public Bullet(float x, float y, int velX, long timer, GameController gc, ObjectId id) {
super(x, y, id);
this.spdX = velX;
this.timer = timer;
this.gc = gc;
}

@Override
public void tick(LinkedList<GameObject> obj) {
x += spdX;
isCollision();
}

private void isCollision() {

for (int i = 0; i < gc.obj.size(); i++) {
GameObject objTemp = gc.obj.get(i);

if (objTemp.getId() == ObjectId.BLOCO) {
if (getBounds().intersects(objTemp.getBounds())) {
gc.removeObj(this);
}
}

if (objTemp.getId() == ObjectId.ENEMY) {
if (getBounds().intersects(objTemp.getBounds())) {
gc.removeObj(gc.obj.get(i));
gc.removeObj(this);
}
}
}
}

@Override
public void render(Graphics g) {
g.setColor(new Color(255, 0, 0));
g.fillOval((int) x, (int) y, 16, 16);
}

@Override
public Rectangle getBounds() {
return new Rectangle((int) x, (int) y, 16, 16);
}

}
61 changes: 24 additions & 37 deletions src/com/ks2002br/entities/Enemy.java
@@ -1,74 +1,61 @@
package com.ks2002br.entities;

/*
* By Elisandro
* By Elisandro 12/2021 revisao geral
*/
import java.awt.*;
import java.util.LinkedList;

import com.ks2002br.frameworks.Animation;
import com.ks2002br.frameworks.GameObject;
import com.ks2002br.frameworks.ObjectId;
import com.ks2002br.frameworks.*;
import com.ks2002br.game.Game;
import com.ks2002br.graficos.Texturas;

public class Enemy extends GameObject {

private int tipo;
private Texturas tex = Game.getInstance();

private Animation animIdle;

private int posInit = 0;
private int dir = 1;
private int pixelsTemp = 64;
private int pixelsTemp = 64;

public Enemy(float x, float y, int t, ObjectId id) {
private Texturas tex = Game.getInstance();
private Animation animIdle;

public Enemy(float x, float y, ObjectId id) {
super(x, y, id);
this.tipo = t;

posInit = (int) x;

animIdle = new Animation(10,tex.bat_idle);

animIdle = new Animation(10, tex.bat_idle);
}

public void tick(LinkedList<GameObject> obj) {
x += spdX;
y += spdY;

animIdle.runAnimation();

iaBurra();


iaEnemy();
}

private void iaBurra() {

if(dir == 1) {
if(x <= posInit + pixelsTemp) spdX = 1; // 50 <= 50+64=114
private void iaEnemy() {
if (dir == 1) {
if (x <= posInit + pixelsTemp) spdX = 1;
else dir = -1;
}else {
if(x >= posInit) spdX = -1;
} else {
if (x >= posInit) spdX = -1;
else dir = 1;
}

}

public void render(Graphics g) {
g.setColor(Color.red);
// g.fillRect((int) x, (int) y, 64, 64);
g.drawString("X = "+x+ " dir = "+dir, (int) x,(int) y);
//if(tipo== 0) g.drawImage(tex.enemy[0],(int) x,(int) y,null);
//if(tipo== 1) g.drawImage(tex.enemy[1],(int) x,(int) y, null);

//Animacao idle teste
animIdle.renderAnimation(g,(int) x, (int) y);
animIdle.renderAnimation(g, (int) x, (int) y);

//g.setColor(Color.red);
// g.fillRect((int) x, (int) y, 64, 64);
//g.drawString("X = " + x + " dir = " + dir, (int) x, (int) y);
// if(tipo== 0) g.drawImage(tex.enemy[0],(int) x,(int) y,null);
// if(tipo== 1) g.drawImage(tex.enemy[1],(int) x,(int) y, null);

}

@Override
public Rectangle getBounds() {
return new Rectangle((int) x, (int) y, 32, 64);
return new Rectangle((int) x, (int) y, 32, 32);
}

}
138 changes: 55 additions & 83 deletions src/com/ks2002br/entities/Player.java
@@ -1,6 +1,7 @@
package com.ks2002br.entities;

/*
* by Elisandro
* By Elisandro 12/2021 revisao geral
*/
import java.awt.*;
import java.util.LinkedList;
Expand All @@ -10,136 +11,110 @@

public class Player extends GameObject {

private int width = 32, height = 64; // Largura e altura do player ( obj)
private int colW = width, colH = height; // Largura e altura da caixa de colisao

private int width = 32, height = 64;
private int colW = width, colH = height;
private float gravity = 0.5f;
private final float MAX_SPD = 10;

private int tipo;
private Texturas tex = Game.getInstance();
private boolean move = false;
private long firingTimer, firingDelay;

private Texturas tex = Game.getInstance();
private GameController gc;

private Animation animIdle, animEsq, animDir;
private int dir = 1; //1 = direita -1 = esquerda
private boolean move = false;
private Animation animIdle, animEsq, animDir;

public Player(float x, float y, int tipo, ObjectId id, GameController gc) {
public Player(float x, float y, ObjectId id, GameController gc) {
super(x, y, id);
this.gc = gc;
this.tipo = tipo;


startPlayer();
}

private void startPlayer() {

animIdle = new Animation(10,tex.player_idle);
animEsq = new Animation(5, tex.playerLeft);
animDir = new Animation(5, tex.playerRight);

firingDelay = System.nanoTime();
firingDelay = 400;

animIdle = new Animation(10, tex.player_idle);
animEsq = new Animation(5, tex.playerLeft);
animDir = new Animation(5, tex.playerRight);
}

public void tick(LinkedList<GameObject> obj) {
x += spdX;
y += spdY;

//esq/dir
if(spdX > 0 ) tipo=2;
else if(spdX < 0 ) tipo=3;
//up/down
if(spdY < 0 ) tipo=0;
else if(spdY > 0) tipo=1;

if(falling || jumping) {
spdY += gravity;
if(spdY > MAX_SPD) spdY = MAX_SPD;
y += spdY;

if (falling || jumping) {
spdY += gravity;
if (spdY > MAX_SPD) spdY = MAX_SPD;
}
if (spdX > 0 ) dir = 1;
if (spdX < 0 ) dir = -1;

if (spdX > 0) dir = 1;
if (spdX < 0) dir = -1;

startAnim();
verificarColisao(obj);
isCollision(obj);

if (shooting) {
long elapsed = (System.nanoTime() - firingTimer) / 1000000;
if (elapsed > firingDelay) {
gc.addObj(new Bullet(x + 8, y + 10, dir * 1, System.nanoTime(), gc, ObjectId.BULLET));
firingTimer = System.nanoTime();
}
}
}


private void startAnim() {

animEsq.runAnimation();
animDir.runAnimation();
animIdle.runAnimation();

if (spdX != 0 ) move = true;
else move = false;


if (spdX != 0) move = true;
else move = false;
}

private void verificarColisao(LinkedList<GameObject> obj) {
private void isCollision(LinkedList<GameObject> obj) {
for (int i = 0; i < gc.obj.size(); i++) {
GameObject tempObj = gc.obj.get(i);
if (tempObj.getId() == ObjectId.BLOCO) {

if (getBounds().intersects(tempObj.getBounds())) {
spdY=0;
spdY = 0;
y = tempObj.getY() + 32;
}

else if (getBoundsBaixo().intersects(tempObj.getBounds())) {
else if (getBoundsBaixo().intersects(tempObj.getBounds())) {
spdY = 0;
y = tempObj.getY() - height +2;
y = tempObj.getY() - height + 2;
falling = false;
jumping = false;
}else {
falling=true;
} else {
falling = true;
}

if (getBoundsEsq().intersects(tempObj.getBounds())) {
x = tempObj.getX() + 32;
if (getBoundsEsq().intersects(tempObj.getBounds())) x = tempObj.getX() + 32;
else if (getBoundsDir().intersects(tempObj.getBounds())) x = tempObj.getX() - width;
}

else if (getBoundsDir().intersects(tempObj.getBounds())) {
x = tempObj.getX() - width;
}

}

// colisao cobaia
else if (tempObj.getId() == ObjectId.ENEMY) {
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);

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) {
Graphics2D g2d = (Graphics2D) g;

if(move) {

if(dir == 1) animDir.renderAnimation(g2d, (int) x, (int) y);
else if(dir == -1) animEsq.renderAnimation(g2d, (int) x, (int) y);


}else {
animIdle.renderAnimation(g2d, (int) x, (int) y);

if (move) {
if (dir == 1) animDir.renderAnimation(g2d, (int) x, (int) y);
else if (dir == -1) animEsq.renderAnimation(g2d, (int) x, (int) y);
} else {
animIdle.renderAnimation(g2d, (int) x, (int) y);
}




// REDERIZACAO DAS CAIXAS DE COLISOES
if (gc.isDebug()) {
if (isDebug()) {
g.setColor(Color.RED);
g2d.draw(getBounds());

Expand All @@ -152,12 +127,10 @@ public void render(Graphics g) {
g.setColor(Color.CYAN);
g2d.draw(getBoundsEsq());

// Debug x,y
g.setFont(new Font("arial", Font.BOLD, 16));
g.setColor(new Color(255, 50, 20));
g.drawString("x,y: " + x + " / " + y + " " + this.id, (int) x, (int) y);
}

}

public Rectangle getBounds() {
Expand All @@ -175,5 +148,4 @@ public Rectangle getBoundsDir() {
public Rectangle getBoundsEsq() {
return new Rectangle((int) x, (int) y + 5, 5, colH - 10);
}

}

0 comments on commit 9cf6d1e

Please sign in to comment.