Advertisement
Nairo05

Untitled

Dec 17th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.31 KB | None | 0 0
  1. //Class PlayScreen
  2. package me.nroffler.Screens;
  3.  
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.Screen;
  7. import com.badlogic.gdx.graphics.Color;
  8. import com.badlogic.gdx.graphics.GL20;
  9. import com.badlogic.gdx.graphics.OrthographicCamera;
  10. import com.badlogic.gdx.graphics.g2d.ParticleEffect;
  11. import com.badlogic.gdx.maps.MapObject;
  12. import com.badlogic.gdx.maps.objects.RectangleMapObject;
  13. import com.badlogic.gdx.maps.tiled.TiledMap;
  14. import com.badlogic.gdx.maps.tiled.TmxMapLoader;
  15. import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
  16. import com.badlogic.gdx.math.Rectangle;
  17. import com.badlogic.gdx.math.Vector2;
  18. import com.badlogic.gdx.physics.box2d.Body;
  19. import com.badlogic.gdx.physics.box2d.BodyDef;
  20. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  21. import com.badlogic.gdx.physics.box2d.FixtureDef;
  22. import com.badlogic.gdx.physics.box2d.PolygonShape;
  23. import com.badlogic.gdx.physics.box2d.World;
  24. import com.badlogic.gdx.utils.viewport.FitViewport;
  25. import com.badlogic.gdx.utils.viewport.Viewport;
  26.  
  27. import box2dLight.PointLight;
  28. import box2dLight.RayHandler;
  29. import me.nroffler.main.DarkWorld;
  30. import me.nroffler.sprites.Player;
  31.  
  32. public class PlayScreen implements Screen {
  33.  
  34.     private boolean running = true;
  35.     private DarkWorld game;
  36.  
  37.     private TmxMapLoader maploader;
  38.     private TiledMap map;
  39.     private OrthogonalTiledMapRenderer renderer;
  40.  
  41.     private OrthographicCamera gamecam;
  42.     private Viewport gameport;
  43.  
  44.     private World world;
  45.     private Box2DDebugRenderer b2dr;
  46.  
  47.     private Player player;
  48.  
  49.     private RayHandler handler;
  50.     private PointLight light;
  51.  
  52.     private ParticleEffect particleEffect;
  53.     private float sclfkt = 0.001f;
  54.  
  55.     public PlayScreen(DarkWorld game, int level){
  56.         this.game = game;
  57.  
  58.         gamecam = new OrthographicCamera();
  59.         gameport = new FitViewport(DarkWorld.V_WIDTH / DarkWorld.PPM, DarkWorld.V_HEIGHT / DarkWorld.PPM, gamecam);
  60.  
  61.         maploader = new TmxMapLoader();
  62.         map = maploader.load("level/map1-1.tmx");
  63.         renderer = new OrthogonalTiledMapRenderer(map, 1 / DarkWorld.PPM);
  64.  
  65.         gamecam.position.set(gameport.getWorldWidth() / 2, gameport.getWorldHeight() / 2, 0);
  66.  
  67.         world = new World(new Vector2(0,-0.8f), true);
  68.  
  69.         b2dr = new Box2DDebugRenderer();
  70.  
  71.         player = new Player(this);
  72.  
  73.  
  74.         for (MapObject object : map.getLayers().get(1).getObjects().getByType(RectangleMapObject.class)){
  75.             Body pbody;
  76.             BodyDef bdef = new BodyDef();
  77.  
  78.             Rectangle rect = ((RectangleMapObject) object).getRectangle();
  79.  
  80.             bdef.type = BodyDef.BodyType.StaticBody;
  81.             bdef.position.set(new Vector2((rect.getX() + rect.getWidth() /2 )/ DarkWorld.PPM, (rect.getY() + rect.getHeight() / 2 )/ DarkWorld.PPM));
  82.             bdef.fixedRotation = true;
  83.  
  84.             pbody = world.createBody(bdef);
  85.  
  86.             PolygonShape shape = new PolygonShape();
  87.             shape.setAsBox(rect.getWidth() / 2 / DarkWorld.PPM, rect.getHeight() / 2 / DarkWorld.PPM);
  88.             pbody.createFixture(shape, 1.0f);
  89.             System.out.println("created");
  90.         }
  91.  
  92.         createBox(32/ DarkWorld.PPM,1f,100f / DarkWorld.PPM, 10f / DarkWorld.PPM, true);
  93.         //Shadows working
  94.  
  95.         handler = new RayHandler(world);
  96.         handler.setAmbientLight(0.0f);
  97.         light = new PointLight(handler, 200, Color.WHITE, 1.9f / DarkWorld.PPM, 0,0);
  98.         light.setSoftnessLength(0);
  99.  
  100.         particleEffect = new ParticleEffect();
  101.         particleEffect.load(Gdx.files.internal("particles/fine.pe"),Gdx.files.internal("particles/"));
  102.         particleEffect.getEmitters().first().setPosition(38.3f,1.6f);
  103.         particleEffect.scaleEffect(sclfkt);
  104.         particleEffect.start();
  105.         particleEffect.setDuration(30000000);
  106.  
  107.  
  108.  
  109.  
  110.     }
  111.  
  112.     @Override
  113.     public void show() {
  114.  
  115.     }
  116.  
  117.     public void update(float dt){
  118.  
  119.         handleInput(dt);
  120.  
  121.         player.update(dt);
  122.  
  123.         if (running) {
  124.             gamecam.position.x = player.b2body.getPosition().x;
  125.         }
  126.         gamecam.update();
  127.         renderer.setView(gamecam);
  128.  
  129.         world.step(1 / 60f,6,2);
  130.  
  131.         updatelighthandler();
  132.         //handler.update();
  133.     }
  134.  
  135.     private void updatelighthandler() {
  136.         light.setPosition(player.b2body.getPosition().x/DarkWorld.PPM,player.b2body.getPosition().y/DarkWorld.PPM);
  137.     }
  138.  
  139.     private void handleInput(float dt) {
  140.         if (Gdx.input.isTouched()){
  141.             move(dt);
  142.         }
  143.         if (Gdx.input.isKeyPressed(Input.Keys.SPACE)){
  144.             move(dt);
  145.         }
  146.     }
  147.  
  148.     private void move(float dt) {
  149.         if (running) {
  150.             if (player.b2body.getLinearVelocity().x < 0.7f) {
  151.                 player.b2body.applyLinearImpulse(new Vector2(200 * dt / DarkWorld.PPM, 0), player.b2body.getWorldCenter(), true);
  152.             }
  153.             player.b2body.applyLinearImpulse(new Vector2(0, 500 * dt / DarkWorld.PPM), player.b2body.getWorldCenter(), true);
  154.         }
  155.     }
  156.  
  157.  
  158.     @Override
  159.     public void render(float delta) {
  160.         update(delta);
  161.  
  162.         Gdx.gl.glClearColor(0.1f, 0.1f, 0.2f, 1);
  163.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  164.  
  165.         //Render tiled Map
  166.         renderer.render();
  167.         b2dr.render(world, gamecam.combined);
  168.  
  169.         //Render Light
  170.         handler.setCombinedMatrix(gamecam.combined.cpy().scl(DarkWorld.PPM));
  171.         handler.updateAndRender();
  172.  
  173.         game.batch.setProjectionMatrix(gamecam.combined);
  174.         game.batch.begin();
  175.         player.draw(game.batch);
  176.         if((light.getDistance() < 0.5f)) {
  177.             particleEffect.update(delta);
  178.             particleEffect.draw(game.batch);
  179.         }
  180.         game.batch.end();
  181.  
  182.         if (!running){
  183.             if (light.getDistance() < 0.3f) {
  184.                 world.setGravity(new Vector2(0,0));
  185.                 player.b2body.setLinearVelocity(0,0);
  186.                 light.setDistance(light.getDistance() + 0.001f);
  187.                 particleEffect.scaleEffect(sclfkt);
  188.                 sclfkt -= 0.0001f;
  189.             } else {
  190.                 game.setScreen(new MainMenu(game));
  191.             }
  192.         }
  193.  
  194.     }
  195.  
  196.     @Override
  197.     public void resize(int width, int height) {
  198.         gameport.update(width, height);
  199.     }
  200.  
  201.     @Override
  202.     public void pause() {
  203.  
  204.     }
  205.  
  206.     @Override
  207.     public void resume() {
  208.  
  209.     }
  210.  
  211.     @Override
  212.     public void hide() {
  213.  
  214.     }
  215.  
  216.     @Override
  217.     public void dispose() {
  218.         handler.dispose();
  219.         world.dispose();
  220.         map.dispose();
  221.         renderer.dispose();
  222.         player.dispose();
  223.         particleEffect.dispose();
  224.         light.dispose();
  225.     }
  226.  
  227.     public World getWorld() {
  228.         return world;
  229.     }
  230.  
  231.     public void setRunning(boolean running){
  232.         this.running = running;
  233.     }
  234.  
  235.     public boolean isRunning(){
  236.         return running;
  237.     }
  238.  
  239.     public Body createBox(float x, float y, float width, float height, boolean isStatic){
  240.         Body pBody;
  241.         BodyDef def = new BodyDef();
  242.  
  243.         if (isStatic){
  244.             def.type = BodyDef.BodyType.StaticBody;
  245.         } else {
  246.             def.type = BodyDef.BodyType.DynamicBody;
  247.         }
  248.         def.position.set(x / DarkWorld.PPM,y / DarkWorld.PPM);
  249.         def.fixedRotation = true;
  250.         pBody = world.createBody(def);
  251.  
  252.         PolygonShape shape = new PolygonShape();
  253.         shape.setAsBox(width/2 / DarkWorld.PPM, height/2 / DarkWorld.PPM);
  254.  
  255.         pBody.createFixture(shape, 1.0f);
  256.         shape.dispose();
  257.  
  258.         return pBody;
  259.     }
  260. }
  261. //Class Player
  262. package me.nroffler.sprites;
  263.  
  264. import com.badlogic.gdx.graphics.Texture;
  265. import com.badlogic.gdx.graphics.g2d.Sprite;
  266. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  267. import com.badlogic.gdx.physics.box2d.Body;
  268. import com.badlogic.gdx.physics.box2d.BodyDef;
  269. import com.badlogic.gdx.physics.box2d.CircleShape;
  270. import com.badlogic.gdx.physics.box2d.FixtureDef;
  271. import com.badlogic.gdx.physics.box2d.World;
  272. import com.badlogic.gdx.utils.Disposable;
  273.  
  274. import me.nroffler.Screens.PlayScreen;
  275. import me.nroffler.main.DarkWorld;
  276.  
  277. public class Player extends Sprite implements Disposable {
  278.  
  279.     private World world;
  280.     public Body b2body;
  281.     public TextureRegion region;
  282.     private PlayScreen screen;
  283.  
  284.     public Player(PlayScreen screen){
  285.         super(new TextureRegion(new Texture("Sprites/player01.png"), 16,16));
  286.         world = screen.getWorld();
  287.  
  288.         setBounds(0, 0, 16 / DarkWorld.PPM, 16 / DarkWorld.PPM);
  289.  
  290.         definePlayer();
  291.  
  292.         setTexture(new Texture("Sprites/player01.png"));
  293.         region = new TextureRegion(getTexture(), 16,16);
  294.         setRegion(region);
  295.  
  296.         this.screen = screen;
  297.     }
  298.  
  299.     private void definePlayer() {
  300.         BodyDef bdef = new BodyDef();
  301.         bdef.position.set(0 / DarkWorld.PPM, 64 / DarkWorld.PPM);
  302.         bdef.type = BodyDef.BodyType.DynamicBody;
  303.  
  304.         b2body = world.createBody(bdef);
  305.  
  306.         FixtureDef fdef = new FixtureDef();
  307.         CircleShape shape = new CircleShape();
  308.         shape.setRadius(6 / DarkWorld.PPM);
  309.  
  310.         fdef.shape = shape;
  311.         b2body.createFixture(fdef);
  312.     }
  313.  
  314.     public void update(float dt){
  315.         setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
  316.         if (b2body.getPosition().x > 38.3f && screen.isRunning()){  //38
  317.             screen.setRunning(false);
  318.  
  319.             //start Particle
  320.         }
  321.     }
  322.  
  323.     @Override
  324.     public void dispose() {
  325.         getTexture().dispose();
  326.     }
  327. }
  328. //Class Darkworld
  329. package me.nroffler.main;
  330.  
  331. import com.badlogic.gdx.ApplicationAdapter;
  332. import com.badlogic.gdx.Game;
  333. import com.badlogic.gdx.Gdx;
  334. import com.badlogic.gdx.graphics.GL20;
  335. import com.badlogic.gdx.graphics.Texture;
  336. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  337.  
  338. import me.nroffler.Screens.MainMenu;
  339. import me.nroffler.Screens.PlayScreen;
  340.  
  341. public class DarkWorld extends Game {
  342.  
  343.     public SpriteBatch batch;
  344.     public static final int V_WIDTH = 400;
  345.     public static final int V_HEIGHT = 208;
  346.     public static final float PPM = 100;
  347.  
  348.     @Override
  349.     public void create () {
  350.         batch = new SpriteBatch();
  351.         setScreen(new MainMenu(this));
  352.     }
  353.  
  354.     @Override
  355.     public void render () {
  356.         super.render();
  357.     }
  358.    
  359.     @Override
  360.     public void dispose () {
  361.         batch.dispose();
  362.     }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement