com.github.unluckyninja.defenseofhuman.model.entity.Hook.java Source code

Java tutorial

Introduction

Here is the source code for com.github.unluckyninja.defenseofhuman.model.entity.Hook.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.entity;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.github.unluckyninja.defenseofhuman.model.GameWorld;

/**
 * @author UnluckyNinja
 */
public class Hook extends Entity {

    private final GameWorld gameWorld;
    private Body body;

    private Player.HookShooter shooter;
    private boolean hooked = false;

    public Hook(GameWorld gameWorld, Player.HookShooter shooter, Vector2 direction, float launchSpeed) {
        this.gameWorld = gameWorld;
        this.shooter = shooter;

        Vector2 dir = direction.cpy();

        BodyDef bodyDef = new BodyDef();
        bodyDef.position.set(shooter.getPlayer().getLaunchPosition());
        bodyDef.angle = (float) Math.toRadians(dir.angle());
        bodyDef.bullet = true;
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.linearVelocity.set(dir.nor().scl(launchSpeed));

        body = shooter.getPlayer().getWorld().createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.15f, 0.1f, new Vector2(0.15f, 0), 0);

        body.createFixture(shape, 0.01f).setSensor(true);

        shape.dispose();

        body.setUserData(this);

        // maybe we can move all the definitions to fields?
    }

    public void reset(Vector2 direction, float launchSpeed) {

    }

    @Override
    public void update(float delta) {

    }

    public Player.HookShooter getShooter() {
        return shooter;
    }

    public Body getBody() {
        return body;
    }

    public void dispose() {
        gameWorld.removeEntity(this);
    }

    @Override
    public void disposeByGameWorld() {
        //To change body of implemented methods use File | Settings | File Templates.
        gameWorld.getWorld().destroyBody(body);
    }

    public boolean isHooked() {
        return hooked;
    }
}