diff --git a/src/com/ks2002br/entities/Bullet.java b/src/com/ks2002br/entities/Bullet.java new file mode 100644 index 0000000..5c1e14b --- /dev/null +++ b/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 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); + } + +} diff --git a/src/com/ks2002br/entities/Enemy.java b/src/com/ks2002br/entities/Enemy.java index 2803a89..53cc859 100644 --- a/src/com/ks2002br/entities/Enemy.java +++ b/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 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); } } diff --git a/src/com/ks2002br/entities/Player.java b/src/com/ks2002br/entities/Player.java index c199f26..2d65cb7 100644 --- a/src/com/ks2002br/entities/Player.java +++ b/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; @@ -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 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 obj) { + private void isCollision(LinkedList 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()); @@ -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() { @@ -175,5 +148,4 @@ public Rectangle getBoundsDir() { public Rectangle getBoundsEsq() { return new Rectangle((int) x, (int) y + 5, 5, colH - 10); } - } diff --git a/src/com/ks2002br/entities/TestAnim.java b/src/com/ks2002br/entities/TestAnim.java index 99c7bf2..1d293c4 100644 --- a/src/com/ks2002br/entities/TestAnim.java +++ b/src/com/ks2002br/entities/TestAnim.java @@ -1,5 +1,8 @@ package com.ks2002br.entities; +/* + * By Elisandro 12/2021 revisao geral + */ import java.awt.Graphics; import java.awt.Rectangle; import java.util.LinkedList; @@ -9,28 +12,25 @@ import com.ks2002br.frameworks.ObjectId; import com.ks2002br.graficos.Texturas; -public class TestAnim extends GameObject{ - +public class TestAnim extends GameObject { + private Texturas tex = new Texturas(); - - private Animation animIdle; // FRAMES IDLE - private Animation animCima, animBaixo; //FRAMES UP/DOWN - private Animation animEsq, animDir; //FRAMES LEFT/RIGHT + private Animation animIdle; + private Animation animCima, animBaixo; + private Animation animEsq, animDir; public TestAnim(float x, float y, ObjectId id) { - super(x, y, id); - - animIdle = new Animation(10,tex.player_idle); - animCima = new Animation(15, tex.playerUp); - animBaixo = new Animation(15, tex.playerDown); - animEsq = new Animation(15, tex.playerLeft); - animDir = new Animation(10, tex.playerRight); - + super(x, y, id); + animIdle = new Animation(10, tex.player_idle); + animCima = new Animation(15, tex.playerUp); + animBaixo = new Animation(15, tex.playerDown); + animEsq = new Animation(15, tex.playerLeft); + animDir = new Animation(10, tex.playerRight); } @Override public void tick(LinkedList obj) { - animCima.runAnimation(); //EXECUTANDO ANIMACAO + animCima.runAnimation(); // EXECUTANDO ANIMACAO animBaixo.runAnimation(); animEsq.runAnimation(); animDir.runAnimation(); @@ -40,15 +40,14 @@ public void tick(LinkedList obj) { @Override public void render(Graphics g) { animIdle.renderAnimation(g, (int) x, (int) y, 128, 256); - animCima.renderAnimation(g, 160, 60,128,256); // REDERIZANDO ANIMACAO - animBaixo.renderAnimation(g, 260, 60,128,256); - animEsq.renderAnimation(g, 360, 60,128,256); - animDir.renderAnimation(g, 460, 60,128,256); + animCima.renderAnimation(g, 160, 60, 128, 256); // REDERIZANDO ANIMACAO + animBaixo.renderAnimation(g, 260, 60, 128, 256); + animEsq.renderAnimation(g, 360, 60, 128, 256); + animDir.renderAnimation(g, 460, 60, 128, 256); } @Override public Rectangle getBounds() { return null; } - } diff --git a/src/com/ks2002br/entities/itens/Ammo.java b/src/com/ks2002br/entities/itens/Ammo.java index 01439c7..4f1ee2a 100644 --- a/src/com/ks2002br/entities/itens/Ammo.java +++ b/src/com/ks2002br/entities/itens/Ammo.java @@ -1,5 +1,8 @@ package com.ks2002br.entities.itens; +/* + * By Elisandro 12/2021 revisao geral + */ import java.awt.Graphics; import java.awt.Rectangle; import java.util.LinkedList; @@ -10,29 +13,25 @@ import com.ks2002br.graficos.Texturas; public class Ammo extends GameObject { - + private Texturas tex = Game.getInstance(); public Ammo(float x, float y, int type, ObjectId id) { - super(x, y, type, id); + super(x, y, type, id); } @Override - public void tick(LinkedList obj) { + public void tick(LinkedList obj) { } @Override - public void render(Graphics g) { - - if(type == 1) g.drawImage(tex.item[5], (int) x, (int) y, null); - if(type == 2) g.drawImage(tex.item[6], (int) x, (int) y, null); - + public void render(Graphics g) { + if (type == 1) g.drawImage(tex.item[5], (int) x, (int) y, null); + if (type == 2) g.drawImage(tex.item[6], (int) x, (int) y, null); } @Override public Rectangle getBounds() { - return new Rectangle((int) x, (int) y, 32, 32); } - } diff --git a/src/com/ks2002br/entities/itens/Gun.java b/src/com/ks2002br/entities/itens/Gun.java index 9d23f4b..5d387a4 100644 --- a/src/com/ks2002br/entities/itens/Gun.java +++ b/src/com/ks2002br/entities/itens/Gun.java @@ -1,11 +1,9 @@ package com.ks2002br.entities.itens; -import java.awt.Graphics; -import java.awt.Rectangle; +import java.awt.*; import java.util.LinkedList; -import com.ks2002br.frameworks.GameObject; -import com.ks2002br.frameworks.ObjectId; +import com.ks2002br.frameworks.*; import com.ks2002br.game.Game; import com.ks2002br.graficos.Texturas; @@ -23,15 +21,11 @@ public void tick(LinkedList obj) { @Override public void render(Graphics g) { - - if(type == 1) g.drawImage(tex.item[4], (int) x, (int) y,null); - + g.drawImage(tex.item[4], (int) x, (int) y,null); } @Override public Rectangle getBounds() { - return new Rectangle((int) x, (int) y, 32, 32); } - } diff --git a/src/com/ks2002br/entities/itens/Key.java b/src/com/ks2002br/entities/itens/Key.java index 2bf1658..1b67da3 100644 --- a/src/com/ks2002br/entities/itens/Key.java +++ b/src/com/ks2002br/entities/itens/Key.java @@ -1,37 +1,34 @@ package com.ks2002br.entities.itens; -import java.awt.Graphics; -import java.awt.Rectangle; +/* + * By Elisandro 12/2021 revisao geral + */ +import java.awt.*; import java.util.LinkedList; -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 Key extends GameObject { - + private Texturas tex = Game.getInstance(); - public Key(float x, float y, ObjectId id) { - super(x, y, id); + public Key(float x, float y, ObjectId id) { + super(x, y, id); } @Override - public void tick(LinkedList obj) { + public void tick(LinkedList obj) { } @Override - public void render(Graphics g) { - - g.drawImage(tex.item[7], (int) x, (int) y,null); - + public void render(Graphics g) { + g.drawImage(tex.item[7], (int) x, (int) y, null); } @Override public Rectangle getBounds() { - return new Rectangle((int) x, (int) y, 32, 32); } - } diff --git a/src/com/ks2002br/entities/itens/KeyCard.java b/src/com/ks2002br/entities/itens/KeyCard.java index d115a33..a01e226 100644 --- a/src/com/ks2002br/entities/itens/KeyCard.java +++ b/src/com/ks2002br/entities/itens/KeyCard.java @@ -1,38 +1,35 @@ package com.ks2002br.entities.itens; -import java.awt.Graphics; -import java.awt.Rectangle; +/* + * By Elisandro 12/2021 revisao geral + */ +import java.awt.*; import java.util.LinkedList; -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 KeyCard extends GameObject { - + private Texturas tex = Game.getInstance(); public KeyCard(float x, float y, int type, ObjectId id) { - super(x, y, type, id); + super(x, y, type, id); } @Override - public void tick(LinkedList obj) { + public void tick(LinkedList obj) { } @Override - public void render(Graphics g) { - - if(type == 1) g.drawImage(tex.item[8], (int) x, (int) y, null); - if(type == 2) g.drawImage(tex.item[9], (int) x, (int) y, null); - + public void render(Graphics g) { + if (type == 1) g.drawImage(tex.item[8], (int) x, (int) y, null); + if (type == 2) g.drawImage(tex.item[9], (int) x, (int) y, null); } @Override public Rectangle getBounds() { - return new Rectangle((int) x, (int) y, 32, 32); } - } diff --git a/src/com/ks2002br/entities/itens/Medkit.java b/src/com/ks2002br/entities/itens/Medkit.java index 598eaca..f0f7f68 100644 --- a/src/com/ks2002br/entities/itens/Medkit.java +++ b/src/com/ks2002br/entities/itens/Medkit.java @@ -1,36 +1,35 @@ package com.ks2002br.entities.itens; -import java.awt.Graphics; -import java.awt.Rectangle; +/* + * By Elisandro 12/2021 revisao geral + */ +import java.awt.*; import java.util.LinkedList; -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 Medkit extends GameObject { - + private Texturas tex = Game.getInstance(); public Medkit(float x, float y, int type, ObjectId id) { - super(x, y, type, id); + super(x, y, type, id); } @Override - public void tick(LinkedList obj) { + public void tick(LinkedList obj) { } @Override - public void render(Graphics g) { - if(type == 1) g.drawImage(tex.item[0], (int) x, (int) y,null); - if(type == 2) g.drawImage(tex.item[1], (int) x, (int) y,null); + public void render(Graphics g) { + if (type == 1) g.drawImage(tex.item[0], (int) x, (int) y, null); + if (type == 2) g.drawImage(tex.item[1], (int) x, (int) y, null); } @Override public Rectangle getBounds() { - return new Rectangle((int) x, (int) y, 32, 32); } - } diff --git a/src/com/ks2002br/entities/itens/Potion.java b/src/com/ks2002br/entities/itens/Potion.java index 0a45058..2430907 100644 --- a/src/com/ks2002br/entities/itens/Potion.java +++ b/src/com/ks2002br/entities/itens/Potion.java @@ -1,36 +1,35 @@ package com.ks2002br.entities.itens; -import java.awt.Graphics; -import java.awt.Rectangle; +/* + * By Elisandro 12/2021 revisao geral + */ +import java.awt.*; import java.util.LinkedList; -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 Potion extends GameObject { - + private Texturas tex = Game.getInstance(); public Potion(float x, float y, int type, ObjectId id) { - super(x, y, type, id); + super(x, y, type, id); } @Override - public void tick(LinkedList obj) { + public void tick(LinkedList obj) { } @Override - public void render(Graphics g) { - if(type == 1) g.drawImage(tex.item[2], (int) x, (int) y,null); - if(type == 2) g.drawImage(tex.item[3], (int) x, (int) y,null); + public void render(Graphics g) { + if (type == 1) g.drawImage(tex.item[2], (int) x, (int) y, null); + if (type == 2) g.drawImage(tex.item[3], (int) x, (int) y, null); } @Override public Rectangle getBounds() { - return new Rectangle((int) x, (int) y, 32, 32); } - } diff --git a/src/com/ks2002br/frameworks/Animation.java b/src/com/ks2002br/frameworks/Animation.java index 57f8556..c103241 100644 --- a/src/com/ks2002br/frameworks/Animation.java +++ b/src/com/ks2002br/frameworks/Animation.java @@ -1,5 +1,8 @@ package com.ks2002br.frameworks; +/* + * By Elisandro 12/2021 revisao geral + */ import java.awt.Graphics; import java.awt.image.BufferedImage; @@ -7,22 +10,22 @@ public class Animation { private int speed, frames; private int index = 0, count = 0; - - private BufferedImage img[] , curImg; - + + private BufferedImage img[], curImg; + public Animation(int spd, BufferedImage... args) { this.speed = spd; this.img = new BufferedImage[args.length]; - + for (int i = 0; i < args.length; i++) { img[i] = args[i]; } frames = args.length; } - + public void runAnimation() { index++; - if(index > speed) { + if (index > speed) { index = 0; nextFrame(); } @@ -30,19 +33,20 @@ public void runAnimation() { private void nextFrame() { for (int i = 0; i < frames; i++) { - if(count == i ) curImg = img[i]; + if (count == i) + curImg = img[i]; } count++; - if(count > frames) count = 0; + if (count > frames) + count = 0; } - + public void renderAnimation(Graphics g, int x, int y) { g.drawImage(curImg, x, y, null); } - + public void renderAnimation(Graphics g, int x, int y, int scaleX, int scaleY) { g.drawImage(curImg, x, y, scaleX, scaleY, null); } - - + } diff --git a/src/com/ks2002br/frameworks/Bloco.java b/src/com/ks2002br/frameworks/Bloco.java index 801d1c7..f4ee9af 100644 --- a/src/com/ks2002br/frameworks/Bloco.java +++ b/src/com/ks2002br/frameworks/Bloco.java @@ -1,7 +1,7 @@ package com.ks2002br.frameworks; /* - * By Elisandro + * By Elisandro 12/2021 revisao geral */ import java.awt.*; import java.util.LinkedList; @@ -10,36 +10,25 @@ import com.ks2002br.graficos.Texturas; public class Bloco extends GameObject { - - private int tipo; + private Texturas tex = Game.getInstance(); - public Bloco(float x, float y, int tipo, ObjectId id) { + public Bloco(float x, float y, int type, ObjectId id) { super(x, y, id); - this.tipo = tipo; + this.type = type; } - public void tick(LinkedList obj) { - } + public void tick(LinkedList obj) { } public void render(Graphics g) { - //g.setColor(Color.GREEN); - //g.fillRect((int) x, (int) y, 32, 32); - // Caixas de colisao do bloco - // g.setColor(Color.RED); - // g.drawRect((int) x, (int) y, 32, 32); - - if(tipo== 0) g.drawImage(tex.block[0],(int) x,(int) y, null); - if(tipo== 1) g.drawImage(tex.block[1],(int) x,(int) y, null); - if(tipo== 2) g.drawImage(tex.block[2],(int) x,(int) y, null); - if(tipo== 3) g.drawImage(tex.block[3],(int) x,(int) y, null); - - + if(type== 0) g.drawImage(tex.block[0],(int) x,(int) y, null); + if(type== 1) g.drawImage(tex.block[1],(int) x,(int) y, null); + if(type== 2) g.drawImage(tex.block[2],(int) x,(int) y, null); + if(type== 3) g.drawImage(tex.block[3],(int) x,(int) y, null); } @Override public Rectangle getBounds() { return new Rectangle((int) x, (int) y, 32, 32); } - } diff --git a/src/com/ks2002br/frameworks/GameController.java b/src/com/ks2002br/frameworks/GameController.java index c35b0b7..caff42a 100644 --- a/src/com/ks2002br/frameworks/GameController.java +++ b/src/com/ks2002br/frameworks/GameController.java @@ -1,28 +1,33 @@ package com.ks2002br.frameworks; /* - * By Elisandro + * By Elisandro 12/2021 revisao geral */ import java.awt.Graphics; import java.util.LinkedList; -import com.ks2002br.game.Game; - public class GameController { -/* - * By Elisandro - */ - private boolean debug = false; public LinkedList obj = new LinkedList<>(); - private GameObject tempObj; public void update() { + int contador = 0; + for (int i = 0; i < obj.size(); i++) { tempObj = obj.get(i); tempObj.tick(obj); } + + for (int i = 0; i < obj.size(); i++) { + tempObj = obj.get(i); + if (tempObj.getId() == ObjectId.BULLET) { + contador++; + } + } + + //System.out.println("[DEBUG GC - Bullets in Game = " + contador); + } public void draw(Graphics g) { @@ -40,35 +45,12 @@ public void removeObj(GameObject obj) { this.obj.remove(obj); } - /* - public void criarMundo() { - - for (int xx = 0; xx < Game.LARGURA * 2 + 32; xx += 32) { - - addObj(new Bloco(xx, 0, ObjectId.BLOCO)); - addObj(new Bloco(xx, Game.ALTURA * 2 - 32, ObjectId.BLOCO)); - addObj(new Bloco(0, xx + 32, ObjectId.BLOCO)); - addObj(new Bloco(Game.ALTURA * 2 - 32, xx, ObjectId.BLOCO)); - - for (int i = 0; i < 10; i++) { - addObj(new Bloco(160 + i * 32, 350, ObjectId.BLOCO)); - } - - for (int i = 0; i < 5; i++) { - addObj(new Bloco(96 + i * 32, 190, ObjectId.BLOCO)); - } - - } - - } - */ //Gets e Sets - public boolean isDebug() { - return debug; - } - - public void setDebug(boolean debug) { - this.debug = debug; - } - +// public boolean isDebug() { +// return debug; +// } +// +// public void setDebug(boolean debug) { +// this.debug = debug; +// } } diff --git a/src/com/ks2002br/frameworks/GameObject.java b/src/com/ks2002br/frameworks/GameObject.java index e89b181..e2c200a 100644 --- a/src/com/ks2002br/frameworks/GameObject.java +++ b/src/com/ks2002br/frameworks/GameObject.java @@ -1,10 +1,9 @@ package com.ks2002br.frameworks; /* - * By Elisandro + * By Elisandro 12/2021 revisao geral */ -import java.awt.Graphics; -import java.awt.Rectangle; +import java.awt.*; import java.util.LinkedList; public abstract class GameObject { @@ -12,19 +11,23 @@ public abstract class GameObject { protected ObjectId id; protected float x, y; protected float spdX, spdY; - - protected boolean falling = true; //CAINDO - protected boolean jumping = false; // PULO + + protected boolean falling = true; + protected boolean jumping = false; + protected boolean shooting = false; + protected boolean debug = false; protected int type; + protected int dir = 1; + protected long timer; - // construtor + // CONSTRUTOR 1 public GameObject(float x, float y, ObjectId id) { this.x = x; this.y = y; this.id = id; } - + // CONSTRUTOR 2 public GameObject(float x, float y, int type, ObjectId id) { this.x = x; this.y = y; @@ -33,19 +36,13 @@ public GameObject(float x, float y, int type, ObjectId id) { } public abstract void tick(LinkedList obj); - public abstract void render(Graphics g); - public abstract Rectangle getBounds(); public ObjectId getId() { return id; } -// public void setId(ObjectId id) { -// this.id = id; -// } - public float getX() { return x; } @@ -101,7 +98,24 @@ public int getType() { public void setType(int type) { this.type = type; } + + public boolean isShooting() { + return shooting; + } + + public void setShooting(boolean shooting) { + this.shooting = shooting; + } + + public long isTimer() { + return timer; + } + public boolean isDebug() { + return debug; + } - + public void setDebug(boolean debug) { + this.debug = debug; + } } diff --git a/src/com/ks2002br/frameworks/ObjectId.java b/src/com/ks2002br/frameworks/ObjectId.java index b019282..a2e5ea5 100644 --- a/src/com/ks2002br/frameworks/ObjectId.java +++ b/src/com/ks2002br/frameworks/ObjectId.java @@ -1,10 +1,11 @@ package com.ks2002br.frameworks; /* - * By Elisandro + * By Elisandro 12/2021 revisao geral */ public enum ObjectId { PLAYER, ENEMY, GUN, AMMO, SOLIDO, BLOCO, - MEDKIT,POTION,KEY,KEY_CARD, BOX_AMMO; + MEDKIT,POTION,KEY,KEY_CARD, BOX_AMMO, + BULLET; } diff --git a/src/com/ks2002br/game/Game.java b/src/com/ks2002br/game/Game.java index 4fb59ae..b58f7e4 100644 --- a/src/com/ks2002br/game/Game.java +++ b/src/com/ks2002br/game/Game.java @@ -1,21 +1,16 @@ package com.ks2002br.game; -/** - * by Elisandro +/* + * By Elisandro 12/2021 revisao geral */ import java.awt.*; import java.awt.image.*; import javax.swing.JFrame; -import com.ks2002br.entities.TestAnim; -import com.ks2002br.frameworks.Animation; -import com.ks2002br.frameworks.GameController; -import com.ks2002br.frameworks.ObjectId; -import com.ks2002br.graficos.CarregarImagem; -import com.ks2002br.graficos.Texturas; +import com.ks2002br.frameworks.*; +import com.ks2002br.graficos.*; import com.ks2002br.input.Teclado; -import com.ks2002br.world.Camera; -import com.ks2002br.world.World; +import com.ks2002br.world.*; public class Game extends Canvas implements Runnable { @@ -35,10 +30,8 @@ public class Game extends Canvas implements Runnable { private World world; private Camera cam; private static Texturas tex; - - - //private TestAnim testAnim; - + + // private TestAnim testAnim; // CONTRUTOR DA CLASSE public Game() { @@ -52,16 +45,15 @@ private void startGame() { gc = new GameController(); addKeyListener(new Teclado(gc)); // OBJETOS AQUI - CarregarImagem mapa = new CarregarImagem(); tex = new Texturas(); - - //testAnim = new TestAnim(40, 60, null); - + + // testAnim = new TestAnim(40, 60, null); + level = mapa.pegarImagem("/mapa-01.png"); world = new World(level, gc); world.carregarLevel(); - cam = new Camera(0,0); + cam = new Camera(0, 0); } private void initFrame() { @@ -125,16 +117,26 @@ public synchronized void start() { private void tick() { gc.update(); - - for (int i = 0; i = 4000) { + gc.removeObj(gc.obj.get(i)); + } + } } - - - // testAnim.tick(null); - + // testAnim.tick(null); } private void render() { @@ -146,31 +148,27 @@ private void render() { Graphics g = image.getGraphics(); // RENDER DO GAME - PINTANDO A TELA DE FUNCO - g.setColor(new Color(80,158,216)); + g.setColor(new Color(80, 158, 216)); g.fillRect(0, 0, LARGURA, ALTURA); g = bs.getDrawGraphics(); g.drawImage(image, 0, 0, LARGURA * ESCALA, ALTURA * ESCALA, null); // A PARTIR DAQUI TUDO SERA REDERIZADO EM CIMA DA COR DA TELA DE FUNDO - Graphics2D g2d = (Graphics2D) g; - - g2d.translate(cam.getCamX(), cam.getCamY()); // CAMERA INICIO + + g2d.translate(cam.getCamX(), cam.getCamY()); // CAMERA INICIO gc.draw(g2d); - - g2d.translate(-cam.getCamX(),-cam.getCamY()); // CAMERA FIM - - - // testAnim.render(g2d); + + g2d.translate(-cam.getCamX(), -cam.getCamY()); // CAMERA FIM + + // testAnim.render(g2d); // FINAL DO OBJETOS A SEREM DESENHADOS bs.show(); // MOSTRAR TUDO QUE O PINTOR DESENHOU g2d.dispose(); } - public static Texturas getInstance() { return tex; } - } diff --git a/src/com/ks2002br/graficos/CarregarImagem.java b/src/com/ks2002br/graficos/CarregarImagem.java index d5ac4cf..474fdcc 100644 --- a/src/com/ks2002br/graficos/CarregarImagem.java +++ b/src/com/ks2002br/graficos/CarregarImagem.java @@ -1,28 +1,28 @@ package com.ks2002br.graficos; + /* - * By Elisandro + * By Elisandro 12/2021 revisao geral */ - import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; public class CarregarImagem { - + private BufferedImage img; - + public BufferedImage pegarImagem(String path) { try { img = ImageIO.read(getClass().getResource(path)); - System.out.println("[DEBUG CarregarImagem] CARREGANDO IMAGEM: "+path); + System.out.println("[DEBUG CarregarImagem] CARREGANDO IMAGEM: " + path); return img; - } catch (IOException | IllegalArgumentException e) { - System.err.println("[DEBUG CarregarImagem] Não foi localizado o arquivo pedido! Sistema sera encerrado! \n"); + } catch (IOException | IllegalArgumentException e) { + System.err + .println("[DEBUG CarregarImagem] Não foi localizado o arquivo pedido! Sistema sera encerrado! \n"); e.printStackTrace(); System.exit(1); - } - return null; + } + return null; } - } diff --git a/src/com/ks2002br/graficos/FolhaSprites.java b/src/com/ks2002br/graficos/FolhaSprites.java index 742d898..393f344 100644 --- a/src/com/ks2002br/graficos/FolhaSprites.java +++ b/src/com/ks2002br/graficos/FolhaSprites.java @@ -1,12 +1,13 @@ package com.ks2002br.graficos; + /* - * By Elisandro + * By Elisandro 12/2021 revisao geral */ import java.awt.image.BufferedImage; public class FolhaSprites { - private BufferedImage img; + private BufferedImage img; public FolhaSprites(BufferedImage image) { this.img = image; @@ -15,9 +16,8 @@ public FolhaSprites(BufferedImage image) { public BufferedImage pegarSpritePosXPosY(int x, int y, int w, int h) { return img.getSubimage(x, y, w, h); } - + public BufferedImage pegarSpriteColRow(int col, int row, int w, int h) { - return img.getSubimage((col * w) - w,(row * h) -h, w, h); + return img.getSubimage((col * w) - w, (row * h) - h, w, h); } - } diff --git a/src/com/ks2002br/graficos/Texturas.java b/src/com/ks2002br/graficos/Texturas.java index aa1b779..298e4df 100644 --- a/src/com/ks2002br/graficos/Texturas.java +++ b/src/com/ks2002br/graficos/Texturas.java @@ -1,106 +1,98 @@ package com.ks2002br.graficos; +/* + * By Elisandro 12/2021 revisao geral + */ import java.awt.image.BufferedImage; public class Texturas { - private FolhaSprites blockSheet, enemySheet, playerSheet; //Instancia/referencia + private FolhaSprites blockSheet, enemySheet, playerSheet; private FolhaSprites batSheet, itemSheet; - - private BufferedImage bloco_tex, enemy_tex, player_tex; + + private BufferedImage bloco_tex, enemy_tex, player_tex; private BufferedImage bat_tex, item_tex; - - public BufferedImage[] block = new BufferedImage[4]; //SPRITES DOS BLOCOS - public BufferedImage[] enemy = new BufferedImage[2]; //SPRITES DOS BLOCOS - public BufferedImage[] player_idle = new BufferedImage[2]; //SPRITES DO PLAYER PARADO - - public BufferedImage[] playerUp = new BufferedImage[5]; // SPRITES DO PLAYER SUBINDO - public BufferedImage[] playerDown = new BufferedImage[5]; // SPRITES DO PLAYER DESCENDO - public BufferedImage[] playerLeft = new BufferedImage[8]; // SPRITES DO PLAYER ESQUERDA - public BufferedImage[] playerRight = new BufferedImage[8]; // SPRITES DO PLAYER DIREITA - - public BufferedImage[] bat_idle = new BufferedImage[4]; - public BufferedImage[] item = new BufferedImage[10]; - + + public BufferedImage[] block = new BufferedImage[4]; + public BufferedImage[] enemy = new BufferedImage[2]; + public BufferedImage[] player_idle = new BufferedImage[2]; + + public BufferedImage[] playerUp = new BufferedImage[5]; + public BufferedImage[] playerDown = new BufferedImage[5]; + public BufferedImage[] playerLeft = new BufferedImage[8]; + public BufferedImage[] playerRight = new BufferedImage[8]; + + public BufferedImage[] bat_idle = new BufferedImage[4]; + public BufferedImage[] item = new BufferedImage[10]; + public Texturas() { CarregarImagem loader = new CarregarImagem(); try { - - bloco_tex = loader.pegarImagem("/bloco_tex.png "); - enemy_tex = loader.pegarImagem("/enemy_tex.png "); - player_tex = loader.pegarImagem("/player_tex.png "); - bat_tex = loader.pegarImagem("/bat_tex.png "); - item_tex = loader.pegarImagem("/itens_tex.png "); - + + bloco_tex = loader.pegarImagem("/bloco_tex.png "); + enemy_tex = loader.pegarImagem("/enemy_tex.png "); + player_tex = loader.pegarImagem("/player_tex.png "); + bat_tex = loader.pegarImagem("/bat_tex.png "); + item_tex = loader.pegarImagem("/itens_tex.png "); + System.out.println("[DEBUG TEXTURA] TEXTURAS CARREGADAS COM SUCESSO!"); } catch (Exception e) { System.err.println("[DEBUG TEXTURA] DEU RUIM AO CARREGAR AS TEXTURAS...!"); } - - blockSheet = new FolhaSprites(bloco_tex); + + blockSheet = new FolhaSprites(bloco_tex); enemySheet = new FolhaSprites(enemy_tex); - playerSheet = new FolhaSprites(player_tex); - batSheet = new FolhaSprites(bat_tex); - itemSheet = new FolhaSprites(item_tex); - - getTextures(); - + playerSheet = new FolhaSprites(player_tex); + batSheet = new FolhaSprites(bat_tex); + itemSheet = new FolhaSprites(item_tex); + + getTextures(); + } private void getTextures() { - //TERRAS GRAMAS - block[0] = blockSheet.pegarSpriteColRow(1, 1, 32,32); //GRAMA - block[1] = blockSheet.pegarSpriteColRow(6, 1, 32,32); // TERRA ESQ - block[2] = blockSheet.pegarSpriteColRow(5, 1, 32,32); // TERRA CENTRAL - block[3] = blockSheet.pegarSpriteColRow(4, 1, 32,32); // TERRA DIR - //ENEMY - enemy[0] = enemySheet.pegarSpriteColRow(1, 1, 32,64); //Lado 1 - enemy[1] = enemySheet.pegarSpriteColRow(2, 1, 32,64); // Lado 2 - - //PLAYER - + // TERRAS GRAMAS + block[0] = blockSheet.pegarSpriteColRow(1, 1, 32, 32); // GRAMA + block[1] = blockSheet.pegarSpriteColRow(6, 1, 32, 32); // TERRA ESQ + block[2] = blockSheet.pegarSpriteColRow(5, 1, 32, 32); // TERRA CENTRAL + block[3] = blockSheet.pegarSpriteColRow(4, 1, 32, 32); // TERRA DIR + // ENEMY + enemy[0] = enemySheet.pegarSpriteColRow(1, 1, 32, 64); // Lado 1 + enemy[1] = enemySheet.pegarSpriteColRow(2, 1, 32, 64); // Lado 2 + // PLAYER for (int i = 0; i < playerUp.length; i++) { - playerUp[i] = playerSheet.pegarSpriteColRow(i+1, 1, 32, 64); + playerUp[i] = playerSheet.pegarSpriteColRow(i + 1, 1, 32, 64); } - // PLAYER DOWN - for (int i = 0; i = Game.LARGURA && player.getX() <= (World.w*32) -Game.LARGURA) { - camX = -player.getX() + Game.LARGURA; + if (player.getX() >= Game.LARGURA && player.getX() <= (World.w * 32) - Game.LARGURA) { + camX = -player.getX() + Game.LARGURA; } -// camY++; } public float getCamX() { @@ -36,7 +36,4 @@ public float getCamY() { public void setCamY(float camY) { this.camY = camY; } - - - } diff --git a/src/com/ks2002br/world/World.java b/src/com/ks2002br/world/World.java index 5bf9923..71695db 100644 --- a/src/com/ks2002br/world/World.java +++ b/src/com/ks2002br/world/World.java @@ -1,5 +1,8 @@ package com.ks2002br.world; +/* + * By Elisandro 12/2021 revisao geral + */ import java.awt.image.BufferedImage; import com.ks2002br.entities.Enemy; @@ -16,60 +19,61 @@ import com.ks2002br.game.Game; public class World { - + private BufferedImage level; private GameController gc; - public static int w,h; - + public static int w, h; + public World(BufferedImage level, GameController gc) { this.level = level; this.gc = gc; - w=level.getWidth(); - h=level.getHeight(); - - System.out.println("[DEBUG WORLD] MAPA x,y = "+w*32+ " , "+h*32); + w = level.getWidth(); + h = level.getHeight(); + + //System.out.println("[DEBUG WORLD] MAPA x,y = " + w * 32 + " , " + h * 32); } public void carregarLevel() { for (int xx = 0; xx < h; xx++) { for (int yy = 0; yy < w; yy++) { int pixel = level.getRGB(xx, yy); - if(pixel == 0xFFFFFFFF) Game.gc.addObj(new Bloco(xx*32, yy*32, 0,ObjectId.BLOCO)); //GRAMA - else if(pixel == 0xFF7F3300) Game.gc.addObj(new Bloco(xx*32, yy*32, 1,ObjectId.BLOCO)); // TERRA ESQ - else if(pixel == 0xFFFFD800) Game.gc.addObj(new Bloco(xx*32, yy*32, 2,ObjectId.BLOCO)); //TERRA CENTRAL - else if(pixel == 0xFF7C3C11) Game.gc.addObj(new Bloco(xx*32, yy*32, 3,ObjectId.BLOCO)); //TERRA DIR - - else if(pixel == 0xFFFF0000) Game.gc.addObj(new Player(xx*32,yy*32,3,ObjectId.PLAYER, gc)); - - else if(pixel == 0xFFFF6A00 )Game.gc.addObj(new Enemy(xx*32,yy*32,0,ObjectId.ENEMY)); //INIMIGO - else if(pixel == 0xFF0026FF)Game.gc.addObj(new Enemy(xx*32,yy*32,1,ObjectId.ENEMY)); - - -//Criando os itens - - - else if(pixel == 0xFF0028FF)Game.gc.addObj(new Medkit(xx*32,yy*32,1,ObjectId.MEDKIT)); - else if(pixel == 0xFF002CFF)Game.gc.addObj(new Medkit(xx*32,yy*32,2,ObjectId.MEDKIT)); - - else if(pixel == 0xFF0032FF)Game.gc.addObj(new Potion(xx*32,yy*32,1,ObjectId.POTION)); - else if(pixel == 0xFF003AFF)Game.gc.addObj(new Potion(xx*32,yy*32,2,ObjectId.POTION)); - - else if(pixel == 0xFF0040FF)Game.gc.addObj(new Gun(xx*32,yy*32,1,ObjectId.GUN)); - else if(pixel == 0xFF0046FF)Game.gc.addObj(new Ammo(xx*32,yy*32,2,ObjectId.BOX_AMMO)); - else if(pixel == 0xFF0050FF)Game.gc.addObj(new Ammo(xx*32,yy*32,1,ObjectId.AMMO)); - - else if(pixel == 0xFF005AFF)Game.gc.addObj(new Key(xx*32,yy*32,ObjectId.KEY)); - else if(pixel == 0xFF0062FF)Game.gc.addObj(new KeyCard(xx*32,yy*32,1,ObjectId.KEY_CARD)); - else if(pixel == 0xFF006EFF)Game.gc.addObj(new KeyCard(xx*32,yy*32,2,ObjectId.KEY_CARD)); - + if (pixel == 0xFFFFFFFF) + Game.gc.addObj(new Bloco(xx * 32, yy * 32, 0, ObjectId.BLOCO)); // GRAMA + else if (pixel == 0xFF7F3300) + Game.gc.addObj(new Bloco(xx * 32, yy * 32, 1, ObjectId.BLOCO)); // TERRA ESQ + else if (pixel == 0xFFFFD800) + Game.gc.addObj(new Bloco(xx * 32, yy * 32, 2, ObjectId.BLOCO)); // TERRA CENTRAL + else if (pixel == 0xFF7C3C11) + Game.gc.addObj(new Bloco(xx * 32, yy * 32, 3, ObjectId.BLOCO)); // TERRA DIR + + else if (pixel == 0xFFFF0000) + Game.gc.addObj(new Player(xx * 32, yy * 32, ObjectId.PLAYER, gc)); + else if (pixel == 0xFFFF6A00) + Game.gc.addObj(new Enemy(xx * 32, yy * 32, ObjectId.ENEMY)); + else if (pixel == 0xFF0026FF) + Game.gc.addObj(new Enemy(xx * 32, yy * 32, ObjectId.ENEMY)); //EM DECISAO + + else if (pixel == 0xFF0028FF) + Game.gc.addObj(new Medkit(xx * 32, yy * 32, 1, ObjectId.MEDKIT)); + else if (pixel == 0xFF002CFF) + Game.gc.addObj(new Medkit(xx * 32, yy * 32, 2, ObjectId.MEDKIT)); + else if (pixel == 0xFF0032FF) + Game.gc.addObj(new Potion(xx * 32, yy * 32, 1, ObjectId.POTION)); + else if (pixel == 0xFF003AFF) + Game.gc.addObj(new Potion(xx * 32, yy * 32, 2, ObjectId.POTION)); + else if (pixel == 0xFF0040FF) + Game.gc.addObj(new Gun(xx * 32, yy * 32, 1, ObjectId.GUN)); + else if (pixel == 0xFF0046FF) + Game.gc.addObj(new Ammo(xx * 32, yy * 32, 2, ObjectId.BOX_AMMO)); + else if (pixel == 0xFF0050FF) + Game.gc.addObj(new Ammo(xx * 32, yy * 32, 1, ObjectId.AMMO)); + else if (pixel == 0xFF005AFF) + Game.gc.addObj(new Key(xx * 32, yy * 32, ObjectId.KEY)); + else if (pixel == 0xFF0062FF) + Game.gc.addObj(new KeyCard(xx * 32, yy * 32, 1, ObjectId.KEY_CARD)); + else if (pixel == 0xFF006EFF) + Game.gc.addObj(new KeyCard(xx * 32, yy * 32, 2, ObjectId.KEY_CARD)); } - } - - System.out.println("[DEBUG WORLD] MAPA CARREGADO OK!"); - } - - - }