Advertisement
Nairo05

Untitled

Jan 6th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.49 KB | None | 0 0
  1. //Class World
  2. public class World {
  3.  
  4.     //L0 = Hintergrund
  5.     //L1 = Vordergrund
  6.  
  7.  
  8.     public int[][][] map = new int[80][46][2];
  9.  
  10.     public ArrayList<Rectangle> getRectangles() {
  11.         return rectangles;
  12.     }
  13.  
  14.     private ArrayList<Rectangle> rectangles;
  15.  
  16.     public ArrayList<Rectangle> getHealpacks() {
  17.         return healpacks;
  18.     }
  19.  
  20.     private ArrayList<Rectangle> healpacks;
  21.  
  22.     public World(){
  23.         rectangles = new ArrayList<>();
  24.         healpacks = new ArrayList<>();
  25.     }
  26.  
  27.     public void generate(){
  28.         for (int x = 0; x < getWidth(); x++){
  29.             for (int y = 0; y < getHeight(); y++){
  30.                 for (int l = 0; l < getLayer(); l++){
  31.                     if (l == 0) {
  32.                         if (y < getHeight()) {
  33.                             map[x][y][0] = Blocks.getIdByBlock(Blocks.STONE);
  34.                             if (new Random().nextInt(11)+1 == 5){
  35.                                 map[x][y][0] = Blocks.getIdByBlock(Blocks.STONE_COAL);
  36.                             }
  37.                         }
  38.                     } else if (l == 1){
  39.                         if (new Random().nextInt(100)+1 == 5){
  40.                             map[x][y][1] = Blocks.getIdByBlock(Blocks.ROCK);
  41.                             for (int i = 0; i < 28; i++){
  42.                                 int dx = x+new Random().nextInt(6)-3;
  43.                                 int dy = y+new Random().nextInt(6)-3;
  44.                                 if (dx > 0 && dx < 80 && dy > 0 && dy < 46){
  45.                                     map[dx][dy][1] = Blocks.getIdByBlock(Blocks.ROCK);
  46.                                 }
  47.                             }
  48.                         }
  49.                         if (new Random().nextInt(400)+1 == 5){
  50.                             map[x][y][1] = Blocks.getIdByBlock(Blocks.LIFE);
  51.                         }
  52.                         if (x == 0) {
  53.                             map[x][y][1] = Blocks.getIdByBlock(Blocks.DIRT_RIGHT);
  54.                         }
  55.                         if (x == 79){
  56.                             map[x][y][1] = Blocks.getIdByBlock(Blocks.DIRT_LEFT);
  57.                         }
  58.                         if (y == 0) {
  59.                             map[x][y][1] = Blocks.getIdByBlock(Blocks.DIRT_TOP);
  60.                         }
  61.                         if (y == 45){
  62.                             map[x][y][1] = Blocks.getIdByBlock(Blocks.DIRT_BOTTOM);
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         for (int x = 0; x < getWidth(); x++){
  69.             for (int y = 0; y < getHeight(); y++){
  70.                 if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(1).texture){
  71.                     rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
  72.                 }
  73.                 if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(2).texture){
  74.                     rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
  75.                 }
  76.                 if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(3).texture){
  77.                     rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
  78.                 }
  79.                 if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(4).texture){
  80.                     rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
  81.                 }
  82.                 if(Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(6).texture){
  83.                     rectangles.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
  84.                 } else if (Blocks.getBlockById(map[x][y][1]).texture == Blocks.getBlockById(8).texture){
  85.                     healpacks.add(new Rectangle(x*Block.TILE_SIZE, y*Block.TILE_SIZE, Block.TILE_SIZE, Block.TILE_SIZE));
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     public void render(Batch batch){
  92.         Texture texture = null;
  93.  
  94.         for (int x = 0; x < getWidth(); x++){
  95.             for (int y = 0; y < getHeight(); y++){
  96.                 for (int l = 0; l < getLayer(); l++){
  97.                     texture = Blocks.getBlockById(map[x][y][l]).texture;
  98.                     if (texture != null) {
  99.                         Sprite sprite = new Sprite(texture);
  100.                         batch.draw(sprite, x * Block.TILE_SIZE, y * Block.TILE_SIZE);
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     public void drawrect(ShapeRenderer shapeRenderer){
  108.         for (int i = 0; i < rectangles.size(); i++){
  109.             shapeRenderer.rect(rectangles.get(i).getX(), rectangles.get(i).getY(), Block.TILE_SIZE, Block.TILE_SIZE);
  110.         }
  111.         for (int i = 0; i < healpacks.size(); i++){
  112.             shapeRenderer.rect(healpacks.get(i).getX(), healpacks.get(i).getY(), Block.TILE_SIZE, Block.TILE_SIZE);
  113.         }
  114.     }
  115.  
  116.     public void removeBlock(int x, int y){
  117.         map[x][y][1] = 0;
  118.     }
  119.  
  120.     public Block getBlock(int x, int y, int layer){
  121.         return Blocks.getBlockById(map[x][y][layer]);
  122.     }
  123.  
  124.     public int getWidth(){
  125.         return map.length;
  126.     }
  127.  
  128.     public int getPXWidth(){
  129.         return map.length * Block.TILE_SIZE;
  130.     }
  131.  
  132.     public int getPXHeight(){
  133.         return map[0].length * Block.TILE_SIZE;
  134.     }
  135.  
  136.     public int getHeight(){
  137.         return map[0].length;
  138.     }
  139.  
  140.     public int getLayer(){
  141.         return map[0][0].length;
  142.     }
  143. }
  144.  
  145.  
  146. //Class PlayState
  147. package com.nichtsroffler;
  148.  
  149. import com.badlogic.gdx.Gdx;
  150. import com.badlogic.gdx.Input;
  151. import com.badlogic.gdx.Screen;
  152. import com.badlogic.gdx.graphics.GL20;
  153. import com.badlogic.gdx.graphics.OrthographicCamera;
  154. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  155. import com.badlogic.gdx.utils.viewport.FillViewport;
  156. import com.badlogic.gdx.utils.viewport.Viewport;
  157.  
  158. import java.security.Key;
  159. import java.util.ArrayList;
  160.  
  161. public class PlayState implements Screen {
  162.  
  163.     private int debugzoom = 3;
  164.     private Main main;
  165.  
  166.     private Hud hud;
  167.  
  168.     private BulletManger manager;
  169.     private EntityManger entityManger;
  170.  
  171.     protected OrthographicCamera camera;
  172.     protected Viewport viewport;
  173.  
  174.     protected World world;
  175.     protected Player player1;
  176.  
  177.     protected MyInputProzessor inputProcessor;
  178.  
  179.     ShapeRenderer shapeRenderer;
  180.  
  181.  
  182.     public PlayState(Main main){
  183.  
  184.         this.main = main;
  185.  
  186.         camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  187.         camera.setToOrtho(false, Gdx.graphics.getWidth()/debugzoom, Gdx.graphics.getHeight()/debugzoom);
  188.  
  189.         viewport = new FillViewport(Gdx.graphics.getWidth()/debugzoom, Gdx.graphics.getHeight()/debugzoom, camera);
  190.  
  191.         world = new World();
  192.         world.generate();
  193.  
  194.         player1 = new Player();
  195.  
  196.         inputProcessor = new MyInputProzessor();
  197.         Gdx.input.setInputProcessor(inputProcessor);
  198.  
  199.         hud = new Hud(main.batch);
  200.  
  201.         shapeRenderer = new ShapeRenderer();
  202.  
  203.         manager = new BulletManger();
  204.         entityManger = new EntityManger();
  205.     }
  206.  
  207.     @Override
  208.     public void show() {
  209.  
  210.     }
  211.  
  212.     @Override
  213.     public void render(float delta) {
  214.         Gdx.gl.glClearColor(1, 1, 1, 1);
  215.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  216.         main.batch.setProjectionMatrix(camera.combined);
  217.  
  218.         player1.update();
  219.         manager.update();
  220.         entityManger.update(player1.getX(), player1.getY());
  221.         handleInput();
  222.  
  223.         if (player1.getX() > camera.viewportWidth/2 && player1.getX() < Gdx.graphics.getWidth()-camera.viewportWidth/2) {
  224.             camera.position.x = player1.getX();
  225.         }
  226.         if (player1.getY() > camera.viewportHeight/2 && player1.getY()+camera.viewportHeight/3 < Gdx.graphics.getHeight()-camera.viewportHeight/10-16) {
  227.             camera.position.y = player1.getY();
  228.         }
  229.  
  230.         main.batch.begin();
  231.         world.render(main.batch);
  232.         player1.render(main.batch);
  233.         manager.render(main.batch);
  234.         entityManger.render(main.batch);
  235.         main.batch.end();
  236.  
  237.         camera.update();
  238.  
  239.         main.batch.setProjectionMatrix(hud.stage.getCamera().combined);
  240.         hud.stage.act();
  241.         hud.stage.draw();
  242.  
  243.         if (main.DEBUG) {
  244.             System.out.println(Gdx.graphics.getFramesPerSecond());
  245.             shapeRenderer.setProjectionMatrix(camera.combined);
  246.             shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
  247.             player1.rendershape(shapeRenderer);
  248.             world.drawrect(shapeRenderer);
  249.             entityManger.rendershape(shapeRenderer);
  250.             manager.rendershape(shapeRenderer);
  251.             shapeRenderer.end();
  252.         }
  253.     }
  254.  
  255.     @Override
  256.     public void resize(int width, int height) {
  257.         viewport.update(width, height);
  258.     }
  259.  
  260.     @Override
  261.     public void pause() {
  262.  
  263.     }
  264.  
  265.     @Override
  266.     public void resume() {
  267.  
  268.     }
  269.  
  270.     @Override
  271.     public void hide() {
  272.  
  273.     }
  274.  
  275.     @Override
  276.     public void dispose() {
  277.  
  278.     }
  279.  
  280.     private void handleInput() {
  281.         for (int i = 0; i < world.getRectangles().size(); i++) {
  282.             if (world.getRectangles().get(i).overlaps(player1.getRect_up())) {
  283.  
  284.             }
  285.         }
  286.  
  287.  
  288.         player1.setUp(inputProcessor.up);
  289.         if (inputProcessor.up) {
  290.             manager.setUp(true);
  291.         }
  292.         player1.setDown(inputProcessor.down);
  293.         if (inputProcessor.down) {
  294.             manager.setDown(player1.down);
  295.         }
  296.         player1.setRight(inputProcessor.right);
  297.         if (inputProcessor.right) {
  298.             manager.setRight(player1.right);
  299.         }
  300.         player1.setLeft(inputProcessor.left);
  301.         if (inputProcessor.left) {
  302.             manager.setLeft(player1.left);
  303.         }
  304.  
  305.  
  306.         for (int i = 0; i < world.getHealpacks().size(); i++){
  307.             if (world.getHealpacks().get(i).overlaps(player1.getRect_up())){
  308.                 world.removeBlock((int)world.getHealpacks().get(i).getX()/16, (int)world.getHealpacks().get(i).getY()/16);
  309.                 hud.addlife();
  310.                 world.getHealpacks().remove(i);
  311.             }
  312.         }
  313.         if (Gdx.input.isKeyPressed(Input.Keys.SPACE)){
  314.             manager.shoot(player1.getX(), player1.getY());
  315.         }
  316.             /*if (world.getBlock(player1.getX()/16+1,player1.getY()/16, 1).texture == Blocks.getBlockById(6).texture && inputProcessor.right){
  317.                 for (int i = 0; i < world.getRectangles().size(); i++){
  318.                     if (world.getRectangles().get(i).overlaps(player1.getRect())){
  319.                         world.getRectangles().remove(i);
  320.                         world.removeBlock(player1.getX()/16+1,player1.getY()/16);
  321.                         return;
  322.                     }
  323.                 }
  324.             }
  325.         }*/
  326.     }
  327. }
  328.  
  329. //Class Player
  330. public class Player implements GameObjekt {
  331.  
  332.     private TextureRegion[][] region;
  333.     private int x, y;
  334.  
  335.     private Rectangle rect_up;
  336.     private Rectangle rect_down;
  337.     private Rectangle rect_left;
  338.     private Rectangle rect_right;
  339.  
  340.     boolean up = false;
  341.     boolean down = false;
  342.     boolean left = false;
  343.     boolean right = false;
  344.  
  345.     private Sprite sprite;
  346.  
  347.     public Player() {
  348.         x = Gdx.graphics.getWidth()/2;
  349.         y = Gdx.graphics.getHeight()/2;
  350.  
  351.         region = TextureRegion.split( new Texture("player/spr_character.png"),16,16);
  352.  
  353.         sprite = new Sprite(region[0][0]);
  354.  
  355.         rect_up = new Rectangle(x, y+sprite.getHeight(), sprite.getWidth(), 3);
  356.         rect_down = new Rectangle(x, y-3, sprite.getWidth(), 3);
  357.         rect_right = new Rectangle(x+sprite.getWidth(), y, 3, sprite.getHeight());
  358.         rect_left = new Rectangle(x-3, y, 3, sprite.getHeight());
  359.  
  360.  
  361.     }
  362.  
  363.  
  364.     @Override
  365.     public void render(SpriteBatch batch) {
  366.         batch.end();
  367.  
  368.         if (up && right) {
  369.             sprite.setRegion(region[0][3]);
  370.         } else if (up && left){
  371.             sprite.setRegion(region[0][5]);
  372.         } else if (down && right){
  373.             sprite.setRegion(region[0][1]);
  374.         } else if (down && left){
  375.             sprite.setRegion(region[0][7]);
  376.         } else if (up){
  377.             sprite.setRegion(region[0][4]);
  378.         } else if (down){
  379.             sprite.setRegion(region[0][0]);
  380.         } else if (right){
  381.             sprite.setRegion(region[0][2]);
  382.         } else if (left){
  383.             sprite.setRegion(region[0][6]);
  384.         }
  385.  
  386.         batch.begin();
  387.         batch.draw(sprite, x, y, 16, 16);
  388.  
  389.     }
  390.  
  391.     @Override
  392.     public void update() {
  393.         rect_up.x = x;
  394.         rect_up.y = y+sprite.getHeight();
  395.  
  396.         rect_down.x = x;
  397.         rect_down.y = y-3;
  398.  
  399.         rect_right.x = x+sprite.getWidth();
  400.         rect_right.y = y;
  401.  
  402.         rect_left.x = x-3;
  403.         rect_left.y = y;
  404.  
  405.         if (up) {
  406.             y = y + 1;
  407.         }
  408.         if (down) {
  409.             y = y - 1;
  410.         }
  411.         if (left) {
  412.             x = x - 1;
  413.         }if (right) {
  414.             x = x + 1;
  415.         }
  416.     }
  417.  
  418.     public void rendershape(ShapeRenderer shapeRenderer){
  419.         shapeRenderer.rect(rect_up.getX(), rect_up.getY(), rect_up.width, rect_up.height);
  420.         shapeRenderer.rect(rect_down.getX(), rect_down.getY(), rect_down.width, rect_down.height);
  421.         shapeRenderer.rect(rect_right.getX(), rect_right.getY(), rect_right.width, rect_right.height);
  422.         shapeRenderer.rect(rect_left.getX(), rect_left.getY(), rect_left.width, rect_left.height);
  423.     }
  424.  
  425.  
  426.     public Rectangle getRect_down() {
  427.         return rect_down;
  428.     }
  429.  
  430.     public Rectangle getRect_left() {
  431.         return rect_left;
  432.     }
  433.  
  434.     public Rectangle getRect_right() {
  435.         return rect_right;
  436.     }
  437.  
  438.     public Rectangle getRect_up(){
  439.         return rect_up;
  440.     }
  441.  
  442.     public void setX(int x) {
  443.         this.x = x;
  444.     }
  445.  
  446.     public void setY(int y) {
  447.         this.y = y;
  448.     }
  449.  
  450.     public int getX() {
  451.         return x;
  452.     }
  453.  
  454.     public int getY() {
  455.         return y;
  456.     }
  457.  
  458.     public void setUp(boolean up) {
  459.         this.up = up;
  460.     }
  461.  
  462.     public void setDown(boolean down) {
  463.         this.down = down;
  464.     }
  465.  
  466.     public void setLeft(boolean left) {
  467.         this.left = left;
  468.     }
  469.  
  470.     public void setRight(boolean right) {
  471.         this.right = right;
  472.     }
  473.  
  474. }
  475.  
  476. //Class GameObjekt
  477. public interface GameObjekt {
  478.  
  479.     public abstract void render(SpriteBatch batch);
  480.     public abstract void update();
  481.  
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement