com.github.unluckyninja.defenseofhuman.model.GameWorld.java Source code

Java tutorial

Introduction

Here is the source code for com.github.unluckyninja.defenseofhuman.model.GameWorld.java

Source

/****************************************************************************************
 * Copyright (C) 2013 UnluckyNinja                                                      *
 *                                                                                      *
 * This program is free software; you can redistribute it and/or                        *
 * modify it under the terms of the GNU General Public License                          *
 * as published by the Free Software Foundation; either version 2                       *
 * of the License, or (at your option) any later version.                               *
 *                                                                                      *
 * This program is distributed in the hope that it will be useful,                      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                        *
 * GNU General Public License for more details.                                         *
 *                                                                                      *
 * You should have received a copy of the GNU General Public License                    *
 * along with this program; if not, see here:http://www.gnu.org/licenses/gpl-2.0.txt    *
 ****************************************************************************************/

package com.github.unluckyninja.defenseofhuman.model;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.utils.Array;
import com.github.unluckyninja.defenseofhuman.DefenseOfHuman;
import com.github.unluckyninja.defenseofhuman.controller.input.listeners.PlayerInputController;
import com.github.unluckyninja.defenseofhuman.model.entity.Entity;
import com.github.unluckyninja.defenseofhuman.model.entity.Hook;
import com.github.unluckyninja.defenseofhuman.model.entity.Player;
import com.github.unluckyninja.defenseofhuman.model.entity.Rope;

/**
 * Created with IntelliJ IDEA.
 * User: UnluckyNinja
 * Date: 13-10-12
 * Time: ?1:29
 * To change this template use File | Settings | File Templates.
 */
public class GameWorld implements Updatable {

    public final Vector2 cursorPosition = new Vector2();

    private final World world;
    private Player localPlayer;
    private Array<Entity> entities;
    private Array<Entity> toremove;

    private Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();

    public GameWorld() {
        this.world = new World(new Vector2(0, -10), true);

        entities = new Array<Entity>();
        toremove = new Array<Entity>();

        // for test
        Vector2 position = new Vector2(0, 0);
        BodyDef def = new BodyDef();
        def.position.set(0, 0);
        EdgeShape edge = new EdgeShape();
        edge.set(-50, -2, 50, -2);
        world.createBody(def).createFixture(edge, 0);
        edge.set(-10, 4, 10, 3);
        world.createBody(def).createFixture(edge, 0);
        localPlayer = createPlayer(position);
        edge.dispose();

        def.position.set(-5, 0);
        def.type = BodyDef.BodyType.StaticBody;

        Body body1 = world.createBody(def);
        def.position.set(5, 1);
        Body body2 = world.createBody(def);

        CircleShape circle = new CircleShape();
        circle.setRadius(0.4f);
        body1.createFixture(circle, 1);
        body2.createFixture(circle, 1);

        new Rope(this, body2, Vector2.X).append(10, Vector2.X);

        circle.dispose();
    }

    public Player createPlayer(Vector2 position) {
        Player player = new Player(this, position);
        entities.add(player);
        return player;
    }

    public Player createPlayer(Vector2 position, PlayerInputController playerInputController) {
        Player player = new Player(this, position, playerInputController);
        entities.add(player);
        return player;
    }

    public Hook createHook(Player.HookShooter shooter, Vector2 direction) {
        // TODO : Hookbody
        Hook hook = new Hook(this, shooter, direction, 30.0f);
        entities.add(hook);
        return hook;
    }

    public Rope createRope(Body bodyA, Vector2 direction) {
        Rope rope = new Rope(this, bodyA, direction);
        entities.add(rope);
        return rope;
    }

    /**
     * It's safe to use in update().
     *
     * @param entity
     */
    public void removeEntity(Entity entity) {
        if (toremove.contains(entity, true)) {
            return;
        }
        toremove.add(entity);
    }

    private final Vector3 cursor = new Vector3();

    @Override
    public void update(float delta) {
        DefenseOfHuman.game.unproject(cursor.set(Gdx.input.getX(), Gdx.input.getY(), 0));
        cursorPosition.set(cursor.x, cursor.y);

        //To change body of implemented methods use File | Settings | File Templates.
        for (Entity entity : entities) {
            entity.update(delta);
        }

        world.step(delta, 8, 3);

        while (world.isLocked()) {
        }
        for (Entity entity : toremove) {
            entities.removeValue(entity, true);
            entity.disposeByGameWorld();
        }
        toremove.clear();
    }

    public void drawDebug(SpriteBatch batch) {
        debugRenderer.render(world, batch.getProjectionMatrix());
    }

    public World getWorld() {
        return world;
    }

    public Player getLocalPlayer() {
        return localPlayer;
    }

    public void dispose() {
        world.dispose();
        debugRenderer.dispose();
    }
}