Advertisement
bindesh

Level.java

Aug 5th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.13 KB | None | 0 0
  1. package org.greh.game;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.greh.GrehUtils;
  6. import org.greh.GrehUtils.CellWithPosition;
  7. import org.greh.Greh_parallax;
  8. import org.greh.Greh_timer;
  9. import org.greh.game.stage_entities.Hero;
  10. import org.greh.game.stage_entities.TileMapObjectsManager;
  11. import org.greh.tween.TweenerBase;
  12.  
  13. import aurelienribon.tweenengine.BaseTween;
  14. import aurelienribon.tweenengine.Tween;
  15. import aurelienribon.tweenengine.TweenCallback;
  16.  
  17. import com.badlogic.gdx.Gdx;
  18. import com.badlogic.gdx.graphics.GL20;
  19. import com.badlogic.gdx.graphics.OrthographicCamera;
  20. import com.badlogic.gdx.graphics.Texture;
  21. import com.badlogic.gdx.graphics.g2d.Batch;
  22. import com.badlogic.gdx.maps.MapLayer;
  23. import com.badlogic.gdx.maps.MapObject;
  24. import com.badlogic.gdx.maps.MapProperties;
  25. import com.badlogic.gdx.maps.tiled.TiledMap;
  26. import com.badlogic.gdx.maps.tiled.TmxMapLoader;
  27. import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
  28. import com.badlogic.gdx.math.Rectangle;
  29. import com.badlogic.gdx.math.Vector2;
  30. import com.badlogic.gdx.utils.Disposable;
  31.  
  32. public class Level implements Disposable {
  33.     /** Parallax system */
  34.     private Greh_parallax m_parallax;
  35.     private float m_red = 1f, m_green = 1f, m_blue = 1f;
  36.     private int m_stars_collected = 0, m_level;
  37.     private boolean m_paused = false;
  38.  
  39.     public TiledMap map;
  40.     public Rectangle map_size;
  41.     public OrthogonalTiledMapRenderer renderer;
  42.     public OrthographicCamera camera_tileunit;
  43.     public TileMapObjectsManager object_manager;
  44.     public Vector2 exit_tile_position = new Vector2();
  45.     public Hero hero;
  46.     final public GameActive game;
  47.     final public App APP;
  48.  
  49.     /** HUD */
  50.     private Texture m_tex_heart;
  51.  
  52.     /** Count Down */
  53.     private Greh_timer m_countdown;
  54.  
  55.     /** Translate hero to starting position of level current position. */
  56.     private boolean m_tween_active = false;
  57.     private TweenCallback m_tween_hero_to_complete = new TweenCallback() {
  58.         @Override
  59.         public void onEvent(int type, BaseTween<?> source) {
  60.             hero.collision_enable = true;
  61.             hero.get_sprite().pause_animation(false);
  62.             m_paused = false;
  63.             m_tween_active = false;
  64.             m_countdown.reset(); // restart ready countdown
  65.         }
  66.     };
  67.  
  68.     public Level(App app, GameActive game, int level) {
  69.         APP = app;
  70.         this.game = game;
  71.         m_level = level;
  72.         map_size = new Rectangle();
  73.         m_countdown = new Greh_timer("countdown");
  74.  
  75.         // create an orthographic camera in Tile Units.
  76.         camera_tileunit = new OrthographicCamera();
  77.         camera_tileunit.setToOrtho(false, GameActive.TILES_SCREEN_X,
  78.                 GameActive.TILES_SCREEN_Y);
  79.         camera_tileunit.update();
  80.  
  81.         // load HUD items
  82.         m_tex_heart = new Texture("data/gfx/heart.png");
  83.  
  84.         // load TMX map
  85.         load();
  86.     }
  87.  
  88.     private void load_extra_map_data() {
  89.         // load externally managed objects
  90.         if (object_manager != null) {
  91.             object_manager.dispose();
  92.         }
  93.         object_manager = new TileMapObjectsManager(this);
  94.         hero = new Hero(this);
  95.         load_hero_pos();
  96.  
  97.         // get exit tile position, look for "type" property in tiles layer
  98.         // with value "exit", at-least 1 tile of exit tileset must have this
  99.         // value
  100.         ArrayList<CellWithPosition> cells = GrehUtils.get_cells(map, "exit");
  101.         if (cells.size() > 0) {
  102.             CellWithPosition cell = cells.get(0);
  103.             exit_tile_position.set(cell.x, cell.y);
  104.         } else { // use end of map as exit
  105.             exit_tile_position.set(map_size.width * 0.99f, 0);
  106.         }
  107.     }
  108.  
  109.     public void load_hero_pos() {
  110.         // get position of object "hero" and set to Hero.
  111.         MapObject obj_hero = map.getLayers().get("obj_hero").getObjects()
  112.                 .get("hero");
  113.         float posX = obj_hero.getProperties().get("x", float.class);
  114.         float posY = obj_hero.getProperties().get("y", float.class);
  115.  
  116.         // Objects are positioned in Pixels, convert to tile unit.
  117.         hero.set_hotspot_to(posX / GameActive.PPT, posY / GameActive.PPT);
  118.         hero.reset();
  119.     }
  120.  
  121.     /** reloads level */
  122.     final public void load() {
  123.         // load tile map
  124.         if (map != null) {
  125.             map.dispose();
  126.             renderer.dispose();
  127.         }
  128.  
  129.         try {
  130.             map = new TmxMapLoader().load("data/maps/level" + m_level + ".tmx");
  131.         } catch (com.badlogic.gdx.utils.SerializationException exp) {
  132.             // some error in parsing tmx file, load debug level
  133.             map = new TmxMapLoader().load("data/maps/debug.tmx");
  134.         }
  135.  
  136.         // save properties
  137.         MapProperties prop = map.getProperties();
  138.         int mapWidth = prop.get("width", Integer.class);
  139.         int mapHeight = prop.get("height", Integer.class);
  140.         map_size.set(0, 0, mapWidth, mapHeight);
  141.  
  142.         String bkg = (String) prop.get("backgroundcolor");
  143.         m_red = Integer.parseInt(bkg.substring(1, 3), 16) / 255f;
  144.         m_green = Integer.parseInt(bkg.substring(3, 5), 16) / 255f;
  145.         m_blue = Integer.parseInt(bkg.substring(5, 7), 16) / 255f;
  146.  
  147.         // load parallax layers using
  148.         m_parallax = new Greh_parallax(map);
  149.         m_parallax.auto_load_layers();
  150.  
  151.         // create tiledmap renderer
  152.         renderer = new OrthogonalTiledMapRenderer(map, 1 / GameActive.PPT);
  153.         load_extra_map_data();
  154.  
  155.         // make "collision" layer invisible.
  156.         set_visible_collision_layer(false);
  157.  
  158.     }
  159.  
  160.     /** set visibility of collision layer */
  161.     public void set_visible_collision_layer(boolean visible) {
  162.         MapLayer layer = map.getLayers().get("collision");
  163.         if (layer != null) {
  164.             layer.setVisible(visible);
  165.         }
  166.     }
  167.  
  168.     public int get_level_number() {
  169.         return m_level;
  170.     }
  171.  
  172.     /** Update score */
  173.     public void star_collected() {
  174.         m_stars_collected++;
  175.     }
  176.  
  177.     public int stars_collected() {
  178.         return m_stars_collected;
  179.     }
  180.  
  181.     public void render(float delta) {
  182.         // TODO: to save from big delta values we can cap delta between 0-1f
  183.         if (m_paused) {
  184.             delta = 0;
  185.         }
  186.  
  187.         // set delta to 0 if count down is active. countdown = 1 second
  188.         long time = m_countdown.get_elapsed_time() / 1000;
  189.         final boolean count_down_active = time < 1 && !m_tween_active;
  190.         if (count_down_active) {
  191.             delta = 0;
  192.         }
  193.  
  194.         // update hero
  195.         hero.update(delta);
  196.  
  197.         // check collision with Objects
  198.         object_manager.check_collision();
  199.  
  200.         // let cam follow hero, x-axis only, hero stays 25% of cam width
  201.         // left of cam. We add 25% of cam width in hero HotSpot position.
  202.         final float cam_width = camera_tileunit.viewportWidth
  203.                 * camera_tileunit.zoom;
  204.         final float cam_width_margin_left = cam_width * 0.25f;
  205.         final float cam_width_margin_right = cam_width * 0.5f;
  206.         camera_tileunit.position.x = hero.get_hotspot().x
  207.                 + cam_width_margin_left;
  208.  
  209.         // adjust when camera goes beyond Map limits
  210.         if (camera_tileunit.position.x < cam_width_margin_left) {
  211.             camera_tileunit.position.x = cam_width_margin_left;
  212.         } else if (camera_tileunit.position.x > map_size.width
  213.                 - cam_width_margin_right) {
  214.             camera_tileunit.position.x = map_size.width
  215.                     - cam_width_margin_right;
  216.         }
  217.         camera_tileunit.update();
  218.  
  219.         // since we have Objects layer as blend of collision and visual we call
  220.         // parallax render methods in order
  221.         // clear the screen
  222.         Gdx.gl.glClearColor(m_red, m_green, m_blue, 1f);
  223.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  224.  
  225.         m_parallax.render_begin(renderer, camera_tileunit);
  226.         object_manager.render_far_layers(m_paused, delta);
  227.  
  228.         // render visible layers without parallax i.e. trees, Fog etc without
  229.         // parallaxXY properties, Therefore saw_disc will get hidden below
  230.         // these things if parallaxXY property is removed. For getting rid of
  231.         // this saw_disc can be drawn after hero i.e. inside render_near_layers
  232.         renderer.render();
  233.         hero.render(delta);
  234.         object_manager.render_near_layers(m_paused, delta);
  235.         m_parallax.render_end(renderer, camera_tileunit);
  236.  
  237.         // render HUD i.e. life, score
  238.         final int life = hero.get_life();
  239.         final float tex_size = App.WIDTH * 0.05f;
  240.         float y = App.HEIGHT - tex_size;
  241.         /* Camera in pixels for HUD is used from GuiDrawUtil */
  242.         Batch batch = APP.gui_draw.batch;
  243.         batch.begin();
  244.         // draw life hearts
  245.         for (int i = 0; i < life; i++) {
  246.             batch.draw(m_tex_heart, i * tex_size, y, tex_size, tex_size);
  247.         }
  248.  
  249.         // draw score
  250.         APP.gui_draw.smallfont.draw(APP.gui_draw.batch, "Stars: "
  251.                 + m_stars_collected, 0, GrehUtils.screenY2glY(tex_size * 1.5f));
  252.  
  253.         if (count_down_active) {
  254.             APP.gui_draw.font.draw(batch, "Ready", App.WIDTH / 2
  255.                     - APP.gui_draw.font.getSpaceWidth() * 2, App.HEIGHT / 2);
  256.         }
  257.  
  258.         batch.end();
  259.     }
  260.  
  261.     /** Tween hero from current position to xy in specified time */
  262.     public void tween_hero_from(float x, float y, float time) {
  263.         // disable hero collision while tween is on
  264.         m_paused = true;
  265.         hero.collision_enable = false;
  266.         hero.get_sprite().pause_animation(true);
  267.         Tween twn = Tween
  268.                 .from(hero.get_hotspot_rectangle_for_tween(),
  269.                         TweenerBase.POSITION_XY, time).target(x, y)
  270.                 .start(APP.tweenmanager);
  271.  
  272.         twn.setCallback(m_tween_hero_to_complete).setCallbackTriggers(
  273.                 TweenCallback.COMPLETE);
  274.         m_tween_active = true;
  275.     }
  276.  
  277.     public boolean paused() {
  278.         return m_paused;
  279.     }
  280.  
  281.     public void pause() {
  282.         m_paused = true;
  283.         hero.pause();
  284.         m_countdown.pause(true);
  285.     }
  286.  
  287.     public void resume() {
  288.         m_paused = false;
  289.         hero.resume();
  290.         m_countdown.pause(false);
  291.     }
  292.  
  293.     @Override
  294.     public void dispose() {
  295.         if (object_manager != null) {
  296.             object_manager.dispose();
  297.         }
  298.         renderer.dispose();
  299.         map.dispose();
  300.         m_tex_heart.dispose();
  301.         hero.dispose();
  302.     }
  303.  
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement