Skip to content

Commit

Permalink
Texturizando o mapa
Browse files Browse the repository at this point in the history
  • Loading branch information
elisandro-tnb committed Sep 7, 2021
1 parent a1f2722 commit ca8437f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 23 deletions.
Binary file added res/bloco_tex.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/mapa-01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 17 additions & 5 deletions src/com/ks2002br/frameworks/Bloco.java
Expand Up @@ -6,21 +6,33 @@
import java.awt.*;
import java.util.LinkedList;

import com.ks2002br.game.Game;
import com.ks2002br.graficos.Texturas;

public class Bloco extends GameObject {

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

public Bloco(float x, float y, ObjectId id) {
public Bloco(float x, float y, int tipo, ObjectId id) {
super(x, y, id);
this.tipo = tipo;
}

public void tick(LinkedList<GameObject> obj) {
}

public void render(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect((int) x, (int) y, 32, 32);
//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);
// 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);


}

@Override
Expand Down
12 changes: 9 additions & 3 deletions src/com/ks2002br/game/Game.java
Expand Up @@ -6,12 +6,10 @@
import java.awt.*;
import java.awt.image.*;
import javax.swing.JFrame;

import com.ks2002br.entities.Player;
import com.ks2002br.frameworks.Cobaia;
import com.ks2002br.frameworks.GameController;
import com.ks2002br.frameworks.ObjectId;
import com.ks2002br.graficos.CarregarImagem;
import com.ks2002br.graficos.Texturas;
import com.ks2002br.input.Teclado;
import com.ks2002br.world.Camera;
import com.ks2002br.world.World;
Expand All @@ -33,6 +31,7 @@ public class Game extends Canvas implements Runnable {
private BufferedImage level = null;
private World world;
private Camera cam;
private static Texturas tex;

// CONTRUTOR DA CLASSE
public Game() {
Expand All @@ -48,6 +47,8 @@ private void startGame() {
// OBJETOS AQUI

CarregarImagem mapa = new CarregarImagem();
tex = new Texturas();

level = mapa.pegarImagem("/mapa-01.png");
world = new World(level, gc);
world.carregarLevel();
Expand Down Expand Up @@ -152,4 +153,9 @@ private void render() {
g2d.dispose();
}


public static Texturas getInstance() {
return tex;
}

}
22 changes: 8 additions & 14 deletions src/com/ks2002br/graficos/FolhaSprites.java
Expand Up @@ -3,27 +3,21 @@
* By Elisandro
*/
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class FolhaSprites {

private BufferedImage img;
private BufferedImage img;

public FolhaSprites(String caminho) {
try {
img = ImageIO.read(getClass().getResource(caminho));
System.out.println("[DEBUG FolhaSprites] LOADING SPRITESHEET... OK!");
} catch (IOException | IllegalArgumentException e) {
System.err.println("[DEBUG FolhaSprites] Não foi localizado o arquivo pedido! Sistema sera encerrado! \n");
e.printStackTrace();
System.exit(1);
}
public FolhaSprites(BufferedImage image) {
this.img = image;
}

public BufferedImage pegarSprite(int x, int y, int w, int h) {
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);
}

}
38 changes: 38 additions & 0 deletions src/com/ks2002br/graficos/Texturas.java
@@ -0,0 +1,38 @@
package com.ks2002br.graficos;

import java.awt.image.BufferedImage;

public class Texturas {

private FolhaSprites blockSheet;

private BufferedImage bloco_tex;

public BufferedImage[] block = new BufferedImage[2]; // posicao 0 grama ,1 terra

public Texturas() {
CarregarImagem loader = new CarregarImagem();
try {
bloco_tex = loader.pegarImagem("/bloco_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);


getTextures();


}

private void getTextures() {

block[0] = blockSheet.pegarSpriteColRow(1, 1, 32,32); //Grama 1
block[1] = blockSheet.pegarSpriteColRow(4, 1, 32,32); // terra 1

}


}
3 changes: 2 additions & 1 deletion src/com/ks2002br/world/World.java
Expand Up @@ -28,7 +28,8 @@ 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, ObjectId.BLOCO));
if(pixel == 0xFFFFFFFF) Game.gc.addObj(new Bloco(xx*32, yy*32, 0,ObjectId.BLOCO));
else if(pixel == 0xFF7F3300) Game.gc.addObj(new Bloco(xx*32, yy*32, 1,ObjectId.BLOCO));
else if(pixel == 0xFFFF0000) Game.gc.addObj(new Player(xx*32,yy*32,ObjectId.PLAYER, gc));
else if(pixel == 0xFFFF6A00 )Game.gc.addObj(new Cobaia(xx*32,yy*32,ObjectId.COBAIA));
}
Expand Down

0 comments on commit ca8437f

Please sign in to comment.