Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.greh.game;
- import java.util.ArrayList;
- import org.greh.GrehUtils;
- import org.greh.GrehUtils.CellWithPosition;
- import org.greh.Greh_parallax;
- import org.greh.Greh_timer;
- import org.greh.game.stage_entities.Hero;
- import org.greh.game.stage_entities.TileMapObjectsManager;
- import org.greh.tween.TweenerBase;
- import aurelienribon.tweenengine.BaseTween;
- import aurelienribon.tweenengine.Tween;
- import aurelienribon.tweenengine.TweenCallback;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Batch;
- import com.badlogic.gdx.maps.MapLayer;
- import com.badlogic.gdx.maps.MapObject;
- import com.badlogic.gdx.maps.MapProperties;
- import com.badlogic.gdx.maps.tiled.TiledMap;
- import com.badlogic.gdx.maps.tiled.TmxMapLoader;
- import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.utils.Disposable;
- public class Level implements Disposable {
- /** Parallax system */
- private Greh_parallax m_parallax;
- private float m_red = 1f, m_green = 1f, m_blue = 1f;
- private int m_stars_collected = 0, m_level;
- private boolean m_paused = false;
- public TiledMap map;
- public Rectangle map_size;
- public OrthogonalTiledMapRenderer renderer;
- public OrthographicCamera camera_tileunit;
- public TileMapObjectsManager object_manager;
- public Vector2 exit_tile_position = new Vector2();
- public Hero hero;
- final public GameActive game;
- final public App APP;
- /** HUD */
- private Texture m_tex_heart;
- /** Count Down */
- private Greh_timer m_countdown;
- /** Translate hero to starting position of level current position. */
- private boolean m_tween_active = false;
- private TweenCallback m_tween_hero_to_complete = new TweenCallback() {
- @Override
- public void onEvent(int type, BaseTween<?> source) {
- hero.collision_enable = true;
- hero.get_sprite().pause_animation(false);
- m_paused = false;
- m_tween_active = false;
- m_countdown.reset(); // restart ready countdown
- }
- };
- public Level(App app, GameActive game, int level) {
- APP = app;
- this.game = game;
- m_level = level;
- map_size = new Rectangle();
- m_countdown = new Greh_timer("countdown");
- // create an orthographic camera in Tile Units.
- camera_tileunit = new OrthographicCamera();
- camera_tileunit.setToOrtho(false, GameActive.TILES_SCREEN_X,
- GameActive.TILES_SCREEN_Y);
- camera_tileunit.update();
- // load HUD items
- m_tex_heart = new Texture("data/gfx/heart.png");
- // load TMX map
- load();
- }
- private void load_extra_map_data() {
- // load externally managed objects
- if (object_manager != null) {
- object_manager.dispose();
- }
- object_manager = new TileMapObjectsManager(this);
- hero = new Hero(this);
- load_hero_pos();
- // get exit tile position, look for "type" property in tiles layer
- // with value "exit", at-least 1 tile of exit tileset must have this
- // value
- ArrayList<CellWithPosition> cells = GrehUtils.get_cells(map, "exit");
- if (cells.size() > 0) {
- CellWithPosition cell = cells.get(0);
- exit_tile_position.set(cell.x, cell.y);
- } else { // use end of map as exit
- exit_tile_position.set(map_size.width * 0.99f, 0);
- }
- }
- public void load_hero_pos() {
- // get position of object "hero" and set to Hero.
- MapObject obj_hero = map.getLayers().get("obj_hero").getObjects()
- .get("hero");
- float posX = obj_hero.getProperties().get("x", float.class);
- float posY = obj_hero.getProperties().get("y", float.class);
- // Objects are positioned in Pixels, convert to tile unit.
- hero.set_hotspot_to(posX / GameActive.PPT, posY / GameActive.PPT);
- hero.reset();
- }
- /** reloads level */
- final public void load() {
- // load tile map
- if (map != null) {
- map.dispose();
- renderer.dispose();
- }
- try {
- map = new TmxMapLoader().load("data/maps/level" + m_level + ".tmx");
- } catch (com.badlogic.gdx.utils.SerializationException exp) {
- // some error in parsing tmx file, load debug level
- map = new TmxMapLoader().load("data/maps/debug.tmx");
- }
- // save properties
- MapProperties prop = map.getProperties();
- int mapWidth = prop.get("width", Integer.class);
- int mapHeight = prop.get("height", Integer.class);
- map_size.set(0, 0, mapWidth, mapHeight);
- String bkg = (String) prop.get("backgroundcolor");
- m_red = Integer.parseInt(bkg.substring(1, 3), 16) / 255f;
- m_green = Integer.parseInt(bkg.substring(3, 5), 16) / 255f;
- m_blue = Integer.parseInt(bkg.substring(5, 7), 16) / 255f;
- // load parallax layers using
- m_parallax = new Greh_parallax(map);
- m_parallax.auto_load_layers();
- // create tiledmap renderer
- renderer = new OrthogonalTiledMapRenderer(map, 1 / GameActive.PPT);
- load_extra_map_data();
- // make "collision" layer invisible.
- set_visible_collision_layer(false);
- }
- /** set visibility of collision layer */
- public void set_visible_collision_layer(boolean visible) {
- MapLayer layer = map.getLayers().get("collision");
- if (layer != null) {
- layer.setVisible(visible);
- }
- }
- public int get_level_number() {
- return m_level;
- }
- /** Update score */
- public void star_collected() {
- m_stars_collected++;
- }
- public int stars_collected() {
- return m_stars_collected;
- }
- public void render(float delta) {
- // TODO: to save from big delta values we can cap delta between 0-1f
- if (m_paused) {
- delta = 0;
- }
- // set delta to 0 if count down is active. countdown = 1 second
- long time = m_countdown.get_elapsed_time() / 1000;
- final boolean count_down_active = time < 1 && !m_tween_active;
- if (count_down_active) {
- delta = 0;
- }
- // update hero
- hero.update(delta);
- // check collision with Objects
- object_manager.check_collision();
- // let cam follow hero, x-axis only, hero stays 25% of cam width
- // left of cam. We add 25% of cam width in hero HotSpot position.
- final float cam_width = camera_tileunit.viewportWidth
- * camera_tileunit.zoom;
- final float cam_width_margin_left = cam_width * 0.25f;
- final float cam_width_margin_right = cam_width * 0.5f;
- camera_tileunit.position.x = hero.get_hotspot().x
- + cam_width_margin_left;
- // adjust when camera goes beyond Map limits
- if (camera_tileunit.position.x < cam_width_margin_left) {
- camera_tileunit.position.x = cam_width_margin_left;
- } else if (camera_tileunit.position.x > map_size.width
- - cam_width_margin_right) {
- camera_tileunit.position.x = map_size.width
- - cam_width_margin_right;
- }
- camera_tileunit.update();
- // since we have Objects layer as blend of collision and visual we call
- // parallax render methods in order
- // clear the screen
- Gdx.gl.glClearColor(m_red, m_green, m_blue, 1f);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- m_parallax.render_begin(renderer, camera_tileunit);
- object_manager.render_far_layers(m_paused, delta);
- // render visible layers without parallax i.e. trees, Fog etc without
- // parallaxXY properties, Therefore saw_disc will get hidden below
- // these things if parallaxXY property is removed. For getting rid of
- // this saw_disc can be drawn after hero i.e. inside render_near_layers
- renderer.render();
- hero.render(delta);
- object_manager.render_near_layers(m_paused, delta);
- m_parallax.render_end(renderer, camera_tileunit);
- // render HUD i.e. life, score
- final int life = hero.get_life();
- final float tex_size = App.WIDTH * 0.05f;
- float y = App.HEIGHT - tex_size;
- /* Camera in pixels for HUD is used from GuiDrawUtil */
- Batch batch = APP.gui_draw.batch;
- batch.begin();
- // draw life hearts
- for (int i = 0; i < life; i++) {
- batch.draw(m_tex_heart, i * tex_size, y, tex_size, tex_size);
- }
- // draw score
- APP.gui_draw.smallfont.draw(APP.gui_draw.batch, "Stars: "
- + m_stars_collected, 0, GrehUtils.screenY2glY(tex_size * 1.5f));
- if (count_down_active) {
- APP.gui_draw.font.draw(batch, "Ready", App.WIDTH / 2
- - APP.gui_draw.font.getSpaceWidth() * 2, App.HEIGHT / 2);
- }
- batch.end();
- }
- /** Tween hero from current position to xy in specified time */
- public void tween_hero_from(float x, float y, float time) {
- // disable hero collision while tween is on
- m_paused = true;
- hero.collision_enable = false;
- hero.get_sprite().pause_animation(true);
- Tween twn = Tween
- .from(hero.get_hotspot_rectangle_for_tween(),
- TweenerBase.POSITION_XY, time).target(x, y)
- .start(APP.tweenmanager);
- twn.setCallback(m_tween_hero_to_complete).setCallbackTriggers(
- TweenCallback.COMPLETE);
- m_tween_active = true;
- }
- public boolean paused() {
- return m_paused;
- }
- public void pause() {
- m_paused = true;
- hero.pause();
- m_countdown.pause(true);
- }
- public void resume() {
- m_paused = false;
- hero.resume();
- m_countdown.pause(false);
- }
- @Override
- public void dispose() {
- if (object_manager != null) {
- object_manager.dispose();
- }
- renderer.dispose();
- map.dispose();
- m_tex_heart.dispose();
- hero.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement