Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Class World
- public class World {
- //L0 = Hintergrund
- //L1 = Vordergrund
- public int[][][] map = new int[80][46][2];
- public ArrayList<Rectangle> getRectangles() {
- return rectangles;
- }
- private ArrayList<Rectangle> rectangles;
- public ArrayList<Rectangle> getHealpacks() {
- return healpacks;
- }
- private ArrayList<Rectangle> healpacks;
- public World(){
- rectangles = new ArrayList<>();
- healpacks = new ArrayList<>();
- }
- public void generate(){
- for (int x = 0; x < getWidth(); x++){
- for (int y = 0; y < getHeight(); y++){
- for (int l = 0; l < getLayer(); l++){
- if (l == 0) {
- if (y < getHeight()) {
- map[x][y][0] = Blocks.getIdByBlock(Blocks.STONE);
- if (new Random().nextInt(11)+1 == 5){
- map[x][y][0] = Blocks.getIdByBlock(Blocks.STONE_COAL);
- }
- }
- } else if (l == 1){
- if (new Random().nextInt(100)+1 == 5){
- map[x][y][1] = Blocks.getIdByBlock(Blocks.ROCK);
- for (int i = 0; i < 28; i++){
- int dx = x+new Random().nextInt(6)-3;
- int dy = y+new Random().nextInt(6)-3;
- if (dx > 0 && dx < 80 && dy > 0 && dy < 46){
- map[dx][dy][1] = Blocks.getIdByBlock(Blocks.ROCK);
- }
- }
- }
- if (new Random().nextInt(400)+1 == 5){
- map[x][y][1] = Blocks.getIdByBlock(Blocks.LIFE);
- }
- if (x == 0) {
- map[x][y][1] = Blocks.getIdByBlock(Blocks.DIRT_RIGHT);
- }
- if (x == 79){
- map[x][y][1] = Blocks.getIdByBlock(Blocks.DIRT_LEFT);
- }
- if (y == 0) {
- map[x][y][1] = Blocks.getIdByBlock(Blocks.DIRT_TOP);
- }
- if (y == 45){
- map[x][y][1] = Blocks.getIdByBlock(Blocks.DIRT_BOTTOM);
- }
- }
- }
- }
- }
- for (int x = 0; x < getWidth(); x++){
- for (int y = 0; y < getHeight(); y++){
- if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(1).texture){
- rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
- }
- if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(2).texture){
- rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
- }
- if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(3).texture){
- rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
- }
- if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(4).texture){
- rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
- }
- if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(6).texture){
- rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
- } else if (Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(8).texture){
- healpacks.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
- }
- }
- }
- }
- public void render(Batch batch){
- Texture texture = null;
- for (int x = 0; x < getWidth(); x++){
- for (int y = 0; y < getHeight(); y++){
- for (int l = 0; l < getLayer(); l++){
- texture = Blocks.getBlockById(map[x][y][l]).texture;
- if (texture != null) {
- Sprite sprite = new Sprite(texture);
- batch.draw(sprite, x * Block.TILE_SIZE, y * Block.TILE_SIZE);
- }
- }
- }
- }
- }
- public void drawrect(ShapeRenderer shapeRenderer){
- for (int i = 0; i < rectangles.size(); i++){
- shapeRenderer.rect(rectangles.get(i).getX(), rectangles.get(i).getY(), Block.TILE_SIZE, Block.TILE_SIZE);
- }
- for (int i = 0; i < healpacks.size(); i++){
- shapeRenderer.rect(healpacks.get(i).getX(), healpacks.get(i).getY(), Block.TILE_SIZE, Block.TILE_SIZE);
- }
- }
- public void removeBlock(int x, int y){
- map[x][y][1] = 0;
- }
- public Block getBlock(int x, int y, int layer){
- return Blocks.getBlockById(map[x][y][layer]);
- }
- public int getWidth(){
- return map.length;
- }
- public int getPXWidth(){
- return map.length * Block.TILE_SIZE;
- }
- public int getPXHeight(){
- return map[0].length * Block.TILE_SIZE;
- }
- public int getHeight(){
- return map[0].length;
- }
- public int getLayer(){
- return map[0][0].length;
- }
- }
- //Class PlayState
- package com.nichtsroffler;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.utils.viewport.FillViewport;
- import com.badlogic.gdx.utils.viewport.Viewport;
- import java.security.Key;
- import java.util.ArrayList;
- public class PlayState implements Screen {
- private int debugzoom = 3;
- private Main main;
- private Hud hud;
- private BulletManger manager;
- private EntityManger entityManger;
- protected OrthographicCamera camera;
- protected Viewport viewport;
- protected World world;
- protected Player player1;
- protected MyInputProzessor inputProcessor;
- ShapeRenderer shapeRenderer;
- public PlayState(Main main){
- this.main = main;
- camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- camera.setToOrtho(false, Gdx.graphics.getWidth()/debugzoom, Gdx.graphics.getHeight()/debugzoom);
- viewport = new FillViewport(Gdx.graphics.getWidth()/debugzoom, Gdx.graphics.getHeight()/debugzoom, camera);
- world = new World();
- world.generate();
- player1 = new Player();
- inputProcessor = new MyInputProzessor();
- Gdx.input.setInputProcessor(inputProcessor);
- hud = new Hud(main.batch);
- shapeRenderer = new ShapeRenderer();
- manager = new BulletManger();
- entityManger = new EntityManger();
- }
- @Override
- public void show() {
- }
- @Override
- public void render(float delta) {
- Gdx.gl.glClearColor(1, 1, 1, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- main.batch.setProjectionMatrix(camera.combined);
- player1.update();
- manager.update();
- entityManger.update(player1.getX(), player1.getY());
- handleInput();
- if (player1.getX() > camera.viewportWidth/2 && player1.getX() < Gdx.graphics.getWidth()-camera.viewportWidth/2) {
- camera.position.x = player1.getX();
- }
- if (player1.getY() > camera.viewportHeight/2 && player1.getY()+camera.viewportHeight/3 < Gdx.graphics.getHeight()-camera.viewportHeight/10-16) {
- camera.position.y = player1.getY();
- }
- main.batch.begin();
- world.render(main.batch);
- player1.render(main.batch);
- manager.render(main.batch);
- entityManger.render(main.batch);
- main.batch.end();
- camera.update();
- main.batch.setProjectionMatrix(hud.stage.getCamera().combined);
- hud.stage.act();
- hud.stage.draw();
- if (main.DEBUG) {
- System.out.println(Gdx.graphics.getFramesPerSecond());
- shapeRenderer.setProjectionMatrix(camera.combined);
- shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
- player1.rendershape(shapeRenderer);
- world.drawrect(shapeRenderer);
- entityManger.rendershape(shapeRenderer);
- manager.rendershape(shapeRenderer);
- shapeRenderer.end();
- }
- }
- @Override
- public void resize(int width, int height) {
- viewport.update(width, height);
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void hide() {
- }
- @Override
- public void dispose() {
- }
- private void handleInput() {
- for (int i = 0; i < world.getRectangles().size(); i++) {
- if (world.getRectangles().get(i).overlaps(player1.getRect_up())) {
- }
- }
- player1.setUp(inputProcessor.up);
- if (inputProcessor.up) {
- manager.setUp(true);
- }
- player1.setDown(inputProcessor.down);
- if (inputProcessor.down) {
- manager.setDown(player1.down);
- }
- player1.setRight(inputProcessor.right);
- if (inputProcessor.right) {
- manager.setRight(player1.right);
- }
- player1.setLeft(inputProcessor.left);
- if (inputProcessor.left) {
- manager.setLeft(player1.left);
- }
- for (int i = 0; i < world.getHealpacks().size(); i++){
- if (world.getHealpacks().get(i).overlaps(player1.getRect_up())){
- world.removeBlock((int)world.getHealpacks().get(i).getX()/16, (int)world.getHealpacks().get(i).getY()/16);
- hud.addlife();
- world.getHealpacks().remove(i);
- }
- }
- if (Gdx.input.isKeyPressed(Input.Keys.SPACE)){
- manager.shoot(player1.getX(), player1.getY());
- }
- /*if (world.getBlock(player1.getX()/16+1,player1.getY()/16, 1).texture == Blocks.getBlockById(6).texture && inputProcessor.right){
- for (int i = 0; i < world.getRectangles().size(); i++){
- if (world.getRectangles().get(i).overlaps(player1.getRect())){
- world.getRectangles().remove(i);
- world.removeBlock(player1.getX()/16+1,player1.getY()/16);
- return;
- }
- }
- }
- }*/
- }
- }
- //Class Player
- public class Player implements GameObjekt {
- private TextureRegion[][] region;
- private int x, y;
- private Rectangle rect_up;
- private Rectangle rect_down;
- private Rectangle rect_left;
- private Rectangle rect_right;
- boolean up = false;
- boolean down = false;
- boolean left = false;
- boolean right = false;
- private Sprite sprite;
- public Player() {
- x = Gdx.graphics.getWidth()/2;
- y = Gdx.graphics.getHeight()/2;
- region = TextureRegion.split( new Texture("player/spr_character.png"),16,16);
- sprite = new Sprite(region[0][0]);
- rect_up = new Rectangle(x, y+sprite.getHeight(), sprite.getWidth(), 3);
- rect_down = new Rectangle(x, y-3, sprite.getWidth(), 3);
- rect_right = new Rectangle(x+sprite.getWidth(), y, 3, sprite.getHeight());
- rect_left = new Rectangle(x-3, y, 3, sprite.getHeight());
- }
- @Override
- public void render(SpriteBatch batch) {
- batch.end();
- if (up && right) {
- sprite.setRegion(region[0][3]);
- } else if (up && left){
- sprite.setRegion(region[0][5]);
- } else if (down && right){
- sprite.setRegion(region[0][1]);
- } else if (down && left){
- sprite.setRegion(region[0][7]);
- } else if (up){
- sprite.setRegion(region[0][4]);
- } else if (down){
- sprite.setRegion(region[0][0]);
- } else if (right){
- sprite.setRegion(region[0][2]);
- } else if (left){
- sprite.setRegion(region[0][6]);
- }
- batch.begin();
- batch.draw(sprite, x, y, 16, 16);
- }
- @Override
- public void update() {
- rect_up.x = x;
- rect_up.y = y+sprite.getHeight();
- rect_down.x = x;
- rect_down.y = y-3;
- rect_right.x = x+sprite.getWidth();
- rect_right.y = y;
- rect_left.x = x-3;
- rect_left.y = y;
- if (up) {
- y = y + 1;
- }
- if (down) {
- y = y - 1;
- }
- if (left) {
- x = x - 1;
- }if (right) {
- x = x + 1;
- }
- }
- public void rendershape(ShapeRenderer shapeRenderer){
- shapeRenderer.rect(rect_up.getX(), rect_up.getY(), rect_up.width, rect_up.height);
- shapeRenderer.rect(rect_down.getX(), rect_down.getY(), rect_down.width, rect_down.height);
- shapeRenderer.rect(rect_right.getX(), rect_right.getY(), rect_right.width, rect_right.height);
- shapeRenderer.rect(rect_left.getX(), rect_left.getY(), rect_left.width, rect_left.height);
- }
- public Rectangle getRect_down() {
- return rect_down;
- }
- public Rectangle getRect_left() {
- return rect_left;
- }
- public Rectangle getRect_right() {
- return rect_right;
- }
- public Rectangle getRect_up(){
- return rect_up;
- }
- public void setX(int x) {
- this.x = x;
- }
- public void setY(int y) {
- this.y = y;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public void setUp(boolean up) {
- this.up = up;
- }
- public void setDown(boolean down) {
- this.down = down;
- }
- public void setLeft(boolean left) {
- this.left = left;
- }
- public void setRight(boolean right) {
- this.right = right;
- }
- }
- //Class GameObjekt
- public interface GameObjekt {
- public abstract void render(SpriteBatch batch);
- public abstract void update();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement