Java tutorial
/**************************************************************************************** * 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.*; import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef; import com.badlogic.gdx.utils.Array; import com.github.unluckyninja.defenseofhuman.model.GameWorld; /** * Created with IntelliJ IDEA. * User: UnluckyNinja * Date: 13-10-14 * Time: ?8:25 * To change this template use File | Settings | File Templates. */ public class Rope extends Entity { public final float width = 0.4f; public final float height = 0.1f; private Array<Body> pieces = new Array<Body>(true, 64); private Array<Joint> joints = new Array<Joint>(true, 64); private BodyDef bodyDef = new BodyDef(); private FixtureDef fixtureDef = new FixtureDef(); private PolygonShape polygonShape = new PolygonShape(); private GameWorld gameWorld; private World world; private Body bodyA; private Body bodyB; public Rope(GameWorld gameWorld, Body body, Vector2 direction) { this.gameWorld = gameWorld; world = body.getWorld(); bodyA = body; // ??? Vector2 oriPosition = new Vector2(body.getPosition()); if (direction.equals(Vector2.Zero)) { direction = Vector2.X; } Vector2 dir = new Vector2(direction); dir.nor(); Vector2 distance = dir.scl(width / 2 * 1.25f); // ,?1/4,? // ?body bodyDef.angle = (float) Math.toRadians(dir.angle()); bodyDef.position.set(distance.add(oriPosition)); bodyDef.linearDamping = 0.3f; bodyDef.type = BodyDef.BodyType.DynamicBody; Body piece = body.getWorld().createBody(bodyDef); piece.setGravityScale(0.1f); // ? // ??,???? fixtureDef.isSensor = true; fixtureDef.density = 0.001f; polygonShape.setAsBox(width / 2, height / 2); fixtureDef.shape = polygonShape; piece.createFixture(fixtureDef); // ? RevoluteJointDef rjDef = new RevoluteJointDef(); rjDef.bodyA = body; rjDef.bodyB = piece; rjDef.localAnchorA.x = 0; rjDef.localAnchorA.y = 0; rjDef.localAnchorB.x = -width / 2 * 1.25f; rjDef.localAnchorB.y = 0; joints.add(body.getWorld().createJoint(rjDef)); pieces.add(piece); piece.setUserData(this); } // remain for later use. private Rope(Body bodyA, Body bodyB, float extraLength, boolean tieOnA, boolean tieOnB) { } public Rope lengthen(float length) { return this; } public Rope shorten(float length) { return this; } public Rope append(int pieces, Vector2 direction) { //TODO ? if (pieces < 1) return this; for (int i = 0; i < pieces; i++) { append(direction); } return this; } // for temp use private Vector2 temp1 = new Vector2(); private Vector2 oriPosition = new Vector2(); private Vector2 dir = new Vector2(); private Rope append(Vector2 direction) { //TODO Body body = pieces.get(pieces.size - 1); // ??? oriPosition.set(body.getWorldPoint(temp1.set(width / 2 * 1.25f, 0))); if (direction.equals(Vector2.Zero)) { direction = Vector2.X; } dir.set(direction); dir.nor(); dir.scl(width / 2 * 1.25f); // ?body bodyDef.angle = (float) Math.toRadians(dir.angle()); bodyDef.position.set(dir.add(oriPosition)); Body piece = body.getWorld().createBody(bodyDef); piece.setGravityScale(0.1f); // ? fixtureDef.isSensor = true; fixtureDef.density = 0.001f; polygonShape.setAsBox(width / 2, height / 2); fixtureDef.shape = polygonShape; piece.createFixture(fixtureDef); // ?? RevoluteJointDef rjDef = new RevoluteJointDef(); rjDef.bodyA = body; rjDef.bodyB = piece; rjDef.localAnchorA.x = width / 2 * 1.25f; rjDef.localAnchorA.y = 0; rjDef.localAnchorB.x = -width / 2 * 1.25f; rjDef.localAnchorB.y = 0; joints.add(body.getWorld().createJoint(rjDef)); pieces.add(piece); body.setUserData(this); return this; } @Override public void update(float delta) { //To change body of implemented methods use File | Settings | File Templates. } /** * @param position between 0 and 1. * @return */ public Body getPiece(float position) { int index = Math.round(pieces.size * position) - 1; return pieces.get(index); } @Override public void disposeByGameWorld() { for (Joint joint : joints) { world.destroyJoint(joint); joint.setUserData(null); } for (Body body : pieces) { world.destroyBody(body); body.setUserData(null); } polygonShape.dispose(); } public void dispose() { gameWorld.removeEntity(this); } }