Raytracing funktioniert

master
Jonathan Hager 6 years ago
parent 991c60944f
commit db811c2de6

@ -143,6 +143,7 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
if(tileX == 0 || tileX == roomX || tileY == 0 || tileY == roomY){ if(tileX == 0 || tileX == roomX || tileY == 0 || tileY == roomY){
updateRoom(); updateRoom();
} }
// Render methode zum rendern der einzelnen Sprites wird aufgerufen // Render methode zum rendern der einzelnen Sprites wird aufgerufen

@ -0,0 +1,70 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.dungeoncrawler.view;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
/**
*
* @author jonathan
*/
public class AnimatedObject {
private Sprite sprite;
private TextureRegion[][] texture;
private int frame;
private int row;
public AnimatedObject(Texture tx, int tileWidth, int tileHeight){
texture = TextureRegion.split(tx, tileWidth, tileHeight);
frame = (int) (Math.random()*texture[0].length);
row = (int) (Math.random()*texture.length);
sprite = new Sprite(texture[row][frame]);
}
public void updateTexture(){
if(frame >= texture[0].length - 1){
frame = 0;
}
else{
frame++;
}
sprite.setRegion(texture[row][frame]);
}
/**
* @return the sprite
*/
public Sprite getSprite() {
return sprite;
}
/**
* @param sprite the sprite to set
*/
public void setSprite(Sprite sprite) {
this.sprite = sprite;
}
/**
* @return the texture
*/
public TextureRegion[][] getTexture() {
return texture;
}
/**
* @param texture the texture to set
*/
public void setTexture(TextureRegion[][] texture) {
this.texture = texture;
}
}

@ -12,9 +12,11 @@ import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer; import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.utils.Timer;
import com.dungeoncrawler.model.Dungeon; import com.dungeoncrawler.model.Dungeon;
import com.dungeoncrawler.model.Entity; import com.dungeoncrawler.model.Entity;
import com.dungeoncrawler.model.entities.*; import com.dungeoncrawler.model.entities.*;
import java.util.ArrayList;
public class GameScreen { public class GameScreen {
//CONTROLS //CONTROLS
@ -44,6 +46,9 @@ public class GameScreen {
TiledMapRenderer tmr; TiledMapRenderer tmr;
TiledMap tm; TiledMap tm;
OrthographicCamera camera; OrthographicCamera camera;
ArrayList<AnimatedObject> objects;
Timer animations;
// Sound // Sound
public Music music; public Music music;
@ -94,6 +99,18 @@ public class GameScreen {
music = Gdx.audio.newMusic(Gdx.files.internal("music/gamemusic.mp3")); music = Gdx.audio.newMusic(Gdx.files.internal("music/gamemusic.mp3"));
music.setVolume(volume); music.setVolume(volume);
music.play(); music.play();
animations = new Timer();
animations.scheduleTask(new Timer.Task() {
@Override
public void run() {
if(objects != null){
for(AnimatedObject object : objects){
object.updateTexture();
}
}
}
},0, 0.1f);
} }
@ -107,6 +124,7 @@ public class GameScreen {
player.setY(p.getyPos()); player.setY(p.getyPos());
tm = getM().getMaps()[level][roomPosX][roomPosY].getMap(); tm = getM().getMaps()[level][roomPosX][roomPosY].getMap();
objects = getM().getMaps()[level][roomPosX][roomPosY].getObjects();
if(tm == null){ if(tm == null){
System.out.println("Dein scheiß geht net"); System.out.println("Dein scheiß geht net");
@ -161,6 +179,10 @@ public class GameScreen {
//BATCH //BATCH
batch.begin(); batch.begin();
for(AnimatedObject object : objects){
object.getSprite().draw(batch);
}
player.draw(batch); player.draw(batch);
//controls.draw(batch); //controls.draw(batch);
//DRAW'T JEDES ENTITY - prueft vorher ob vorhanden //DRAW'T JEDES ENTITY - prueft vorher ob vorhanden
@ -227,6 +249,7 @@ public class GameScreen {
arrowSprites[i].draw(batch); arrowSprites[i].draw(batch);
} }
} }
batch.end(); batch.end();
} }

@ -5,9 +5,8 @@
*/ */
package com.dungeoncrawler.view; package com.dungeoncrawler.view;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap; import com.badlogic.gdx.maps.tiled.TiledMap;
import java.util.ArrayList;
/** /**
* *
@ -15,13 +14,11 @@ import com.badlogic.gdx.maps.tiled.TiledMap;
*/ */
public class MapContainer { public class MapContainer {
private TiledMap map; private TiledMap map;
private Sprite[] animatedObjects; private ArrayList<AnimatedObject> objects;
private TextureRegion[][][] textures;
public MapContainer(TiledMap map, int objectAmount){ public MapContainer(TiledMap map){
this.map = map; this.map = map;
this.animatedObjects = new Sprite[objectAmount]; this.objects = new ArrayList<>();
this.textures = new TextureRegion[objectAmount][][];
} }
/** /**
@ -39,40 +36,17 @@ public class MapContainer {
} }
/** /**
* @return the animatedObjects * @return the objects
*/ */
public Sprite[] getAnimatedObjects() { public ArrayList<AnimatedObject> getObjects() {
return animatedObjects; return objects;
} }
/** /**
* @param animatedObjects the animatedObjects to set * @param objects the objects to set
*/ */
public void setAnimatedObjects(Sprite[] animatedObjects) { public void setObjects(ArrayList<AnimatedObject> objects) {
this.animatedObjects = animatedObjects; this.objects = objects;
} }
public void setAnimatedObjects(Sprite animatedObject, int i) {
this.animatedObjects[i] = animatedObject;
}
/**
* @return the textures
*/
public TextureRegion[][][] getTextures() {
return textures;
}
/**
* @param textures the textures to set
*/
public void setTextures(TextureRegion[][][] textures) {
this.textures = textures;
}
public void setTextures(TextureRegion[][] textures, int i) {
this.textures[i] = textures;
}
} }

@ -26,13 +26,12 @@ public class MapGenerator {
Texture tiles; Texture tiles;
TextureRegion[][] splitTiles; TextureRegion[][] splitTiles;
Texture torchT; Texture torchT;
TextureRegion[][] torch;
public MapGenerator(Texture tiles){ public MapGenerator(Texture tiles){
this.tiles = tiles; this.tiles = tiles;
splitTiles = TextureRegion.split(this.tiles, 48, 48); splitTiles = TextureRegion.split(this.tiles, 48, 48);
torchT = new Texture("sprites/torch.png"); torchT = new Texture("sprites/torch.png");
torch = TextureRegion.split(torchT, 48, 48); //torch = TextureRegion.split(torchT, 48, 48);
} }
public Map generateMap(Dungeon d){ public Map generateMap(Dungeon d){
@ -121,9 +120,13 @@ public class MapGenerator {
// X: Gesamtlänge, Y: Exakte Mitte der Gesamtlänge // X: Gesamtlänge, Y: Exakte Mitte der Gesamtlänge
staticLayer.getCell(tempX + 1, (tempY / 2) + 0).setTile(new StaticTiledMapTile(splitTiles[4][4])); //rechts-1 staticLayer.getCell(tempX + 1, (tempY / 2) + 0).setTile(new StaticTiledMapTile(splitTiles[4][4])); //rechts-1
staticLayer.getCell(tempX + 1, (tempY / 2) + 1).setTile(new StaticTiledMapTile(splitTiles[0][0])); //rechts1 staticLayer.getCell(tempX + 1, (tempY / 2) + 1).setTile(new StaticTiledMapTile(splitTiles[0][0])); //rechts1
staticLayer.getCell(tempX + 1, (tempY / 2) + 2).setTile(new StaticTiledMapTile(splitTiles[3][1])); //rechts2 staticLayer.getCell(tempX + 1, (tempY / 2) + 2).setTile(new StaticTiledMapTile(splitTiles[1][1])); //rechts2
staticLayer.getCell(tempX + 1, (tempY / 2) + 3).setTile(new StaticTiledMapTile(splitTiles[2][1])); //rechts3 staticLayer.getCell(tempX + 1, (tempY / 2) + 3).setTile(new StaticTiledMapTile(splitTiles[0][1])); //rechts3
staticLayer.getCell(tempX + 1, (tempY / 2) + 4).setTile(new StaticTiledMapTile(splitTiles[2][5])); //rechts4 staticLayer.getCell(tempX + 1, (tempY / 2) + 4).setTile(new StaticTiledMapTile(splitTiles[2][5])); //rechts4
AnimatedObject tempObject = new AnimatedObject(torchT, 48, 48);
tempObject.getSprite().setPosition((tempX + 1) * 48, ((tempY / 2) + 2) * 48);
temp.getObjects().add(tempObject);
} }
collisionLayer.getObjects().remove(right); collisionLayer.getObjects().remove(right);
@ -167,9 +170,13 @@ public class MapGenerator {
// X: 0, Y: Exakte Mitte der Gesamtlänge // X: 0, Y: Exakte Mitte der Gesamtlänge
staticLayer.getCell(0, (tempY / 2) + 0).setTile(new StaticTiledMapTile(splitTiles[4][5])); //links-1 staticLayer.getCell(0, (tempY / 2) + 0).setTile(new StaticTiledMapTile(splitTiles[4][5])); //links-1
staticLayer.getCell(0, (tempY / 2) + 1).setTile(new StaticTiledMapTile(splitTiles[0][0])); //links1 staticLayer.getCell(0, (tempY / 2) + 1).setTile(new StaticTiledMapTile(splitTiles[0][0])); //links1
staticLayer.getCell(0, (tempY / 2) + 2).setTile(new StaticTiledMapTile(splitTiles[3][1])); //links2 staticLayer.getCell(0, (tempY / 2) + 2).setTile(new StaticTiledMapTile(splitTiles[1][1])); //links2
staticLayer.getCell(0, (tempY / 2) + 3).setTile(new StaticTiledMapTile(splitTiles[2][1])); //links3 staticLayer.getCell(0, (tempY / 2) + 3).setTile(new StaticTiledMapTile(splitTiles[0][1])); //links3
staticLayer.getCell(0, (tempY / 2) + 4).setTile(new StaticTiledMapTile(splitTiles[3][5])); //links4 staticLayer.getCell(0, (tempY / 2) + 4).setTile(new StaticTiledMapTile(splitTiles[3][5])); //links4
AnimatedObject tempObject = new AnimatedObject(torchT, 48, 48);
tempObject.getSprite().setPosition(0, ((tempY / 2) + 2) * 48);
temp.getObjects().add(tempObject);
} }
collisionLayer.getObjects().remove(left); collisionLayer.getObjects().remove(left);
@ -192,6 +199,8 @@ public class MapGenerator {
} }
private MapContainer generateRoom(Room r, int roomDimensionX, int roomDimensionY, int lvl){ private MapContainer generateRoom(Room r, int roomDimensionX, int roomDimensionY, int lvl){
TiledMap tempRoom = new TiledMap();
MapContainer temp = new MapContainer(tempRoom);
int bodenX; int bodenX;
int bodenY; int bodenY;
@ -205,9 +214,6 @@ public class MapGenerator {
bodenY = lvl - 4; bodenY = lvl - 4;
} }
MapContainer temp;
TiledMap tempRoom = new TiledMap();
// roomDimension bezieht sich auf die Größe des Raumes, da aber noch die Wände fehlen, // roomDimension bezieht sich auf die Größe des Raumes, da aber noch die Wände fehlen,
// muss auf die Größe jeweils 2 addiert werden. // muss auf die Größe jeweils 2 addiert werden.
int mapDimensionX = roomDimensionX + 2; int mapDimensionX = roomDimensionX + 2;
@ -258,20 +264,17 @@ public class MapGenerator {
// oben1 // oben1
else if(y == mapDimensionY - 1){ else if(y == mapDimensionY - 1){
if(x == roomDimensionX / 2 - 1 || x == roomDimensionX / 2 + 3){ if(x == roomDimensionX / 2 - 1 || x == roomDimensionX / 2 + 3){
cell.setTile(new StaticTiledMapTile(splitTiles[3][1]));
} AnimatedObject tempObject = new AnimatedObject(torchT, 48, 48);
else{ tempObject.getSprite().setPosition(x * 48, y * 48);
cell.setTile(new StaticTiledMapTile(splitTiles[1][1])); temp.getObjects().add(tempObject);
} }
cell.setTile(new StaticTiledMapTile(splitTiles[1][1]));
} }
// oben2 // oben2
else if(y == mapDimensionY){ else if(y == mapDimensionY){
if(x == roomDimensionX / 2 - 1 || x == roomDimensionX / 2 + 3){ cell.setTile(new StaticTiledMapTile(splitTiles[0][1]));
cell.setTile(new StaticTiledMapTile(splitTiles[2][1]));
}
else{
cell.setTile(new StaticTiledMapTile(splitTiles[0][1]));
}
} }
// oben3 // oben3
else if(y == mapDimensionY + 1){ else if(y == mapDimensionY + 1){
@ -308,7 +311,6 @@ public class MapGenerator {
layers.add(dynamicLayer); layers.add(dynamicLayer);
layers.add(staticLayer); layers.add(staticLayer);
temp = new MapContainer(tempRoom, 0);
return temp; return temp;
} }

Loading…
Cancel
Save