List of usage examples for com.badlogic.gdx.math Vector2 Vector2
public Vector2(float x, float y)
From source file:com.dongbat.game.util.factory.UnitFactory.java
public static Entity createQueen(World world, Vector2 position, float radius) { Entity e = world.createEntity(UUID.randomUUID()); Stats stats = new Stats(); stats.setAllowComsumming(false);//www . jav a 2s. c o m stats.setConsumable(true); stats.setBaseRateSpeed(2000); BuffComponent buff = new BuffComponent(); Physics physics = new Physics(); physics.setBody(PhysicsUtil.createBody(PhysicsUtil.getPhysicsWorld(world), position, radius, e)); physics.getBody().setUserData(UuidUtil.getUuid(e)); UnitMovement movement = new UnitMovement(); float posX = (float) (ECSUtil.getRandom(world).getFloat(-1, 1) * scaleX); float posY = (float) (ECSUtil.getRandom(world).getFloat(-1, 1) * scaleY); movement.setDirectionVelocity(new Vector2(posX, posY)); Display display = new Display(); e.edit().add(physics).add(stats).add(new CollisionComponent()).add(buff).add(new Queen()) .add(new Detection()).add(movement).add(display); BuffUtil.addBuff(world, e, e, "QueenTeleportSchedule", -1, 1); BuffUtil.addBuff(world, e, e, "ProduceFoodSchedule", -1, 1); BuffUtil.addBuff(world, e, e, "FeedSmaller", -1, 1, "feedPerSecond", 0.2f); BuffUtil.addBuff(world, e, e, "SelfDefense", -1, 1, "framePerFood", 10); BuffUtil.addBuff(world, e, e, "QueenGrowth", -1, 1, "cap", 20); display.setPosition(PhysicsUtil.getPosition(world, e)); display.setRadius(PhysicsUtil.getRadius(world, e)); display.setRotation(EntityUtil.getComponent(world, e, UnitMovement.class).getDirectionVelocity().angle()); TextureAtlas queen = AssetUtil.getUnitAtlas().get("queen"); Animation animation = new Animation(0.1f, queen.getRegions()); animation.setPlayMode(Animation.PlayMode.LOOP); display.setDefaultAnimation(new AnimatedSprite(animation)); return e; }
From source file:com.dongbat.game.util.FoodSpawningUtil.java
/** * Spawn food to the map of the world/*from w ww . j a v a2s. c om*/ * * @param world artemis world */ public static void spawnFood(World world) { if (reachMaxFood(world) || ECSUtil.getFrame(world) % 1 != 0) { return; } float width = (float) (ECSUtil.getRandom(world).getFloat(0, 1) * scaleX * 2 - scaleX); float height = (float) (ECSUtil.getRandom(world).getFloat(0, 1) * scaleY * 2 - scaleY); for (int i = 0; i < 1; i++) { EntityFactory.createFood(world, new Vector2(width, height), null); } }
From source file:com.dongbat.game.util.PhysicsUtil.java
/** * Initialize a box2d world and put it into libGdx ObjectMap * * @param world artemis world// ww w . j a v a2 s. com */ public static void init(com.artemis.World world) { if (physicsWorldMap.get(world) != null) { return; } World physicsWorld = new World(new Vector2(0, Constants.PHYSICS.DEFAULT_GRAVITY), false); physicsWorldMap.put(world, physicsWorld); artemisWorldMap.put(physicsWorld, world); }
From source file:com.dongbat.game.util.PhysicsUtil.java
/** * Create box2d Edge, use for making world, floor, ... * * @param world artemis world//from www .j a v a2 s.co m * @param type Body type * @param x1 x start * @param y1 x finish * @param x2 y start * @param y2 y finish * @param density densisty of edge * @return Edge that was just created */ public static Body createEdge(com.artemis.World world, BodyDef.BodyType type, float x1, float y1, float x2, float y2, float density) { BodyDef def = new BodyDef(); def.type = type; Body box = getPhysicsWorld(world).createBody(def); EdgeShape poly = new EdgeShape(); poly.set(new Vector2(0, 0), new Vector2(x2 - x1, y2 - y1)); // box.createFixture(poly, density); box.setTransform(x1, y1, 0); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = poly; // fixtureDef.filter.maskBits = 2; // fixtureDef.filter.categoryBits = 2 | 1; box.createFixture(fixtureDef); // box.setUserData(new Box2dSteeringEntity(box, true, 0.1f)); poly.dispose(); return box; }
From source file:com.dongbat.game.util.PlaneMathCalculator.java
public static Vector2 getPerpendicularVector(Vector2 oldVector) { Vector2 newVector = new Vector2(oldVector.y, -oldVector.x); return newVector; }
From source file:com.dongbat.invasion.ability.types.Barrier.java
@Override public void cast(Entity caster) { Vector2 pos = PhysicsUtil.getPosition(caster); Enemy enemyCom = EntityUtil.getComponent(caster, Enemy.class); Vector2 barrierCenter = new Vector2(0, 0); barrierCenter.y = MathUtils.random(Constants.PHYSICS.GROUND_Y + OFFSET, Constants.ENEMY.AIR_START_Y - OFFSET); float start;// www.ja va2s . c o m if (enemyCom.isAirUnit()) { start = Constants.ENEMY.AIR_START_X + 20; } else { start = Constants.ENEMY.GROUND_START_X + 20; } barrierCenter.x = MathUtils.random(start, Constants.ENEMY.PICK_UP_POINT_X - 20); int angle; if (enemyCom.isAirUnit()) { angle = MathUtils.random(0, 90); } else { angle = MathUtils.random(90, 180); } Array<Vector2> positions = getComponentPositions(barrierCenter, angle, componentCount); for (Vector2 comPos : positions) { Entity component = EnemyRegistry.createEnemy("barrierComponent"); component.edit().add(new Expires(expires / 1000.0f)).remove(EnemyMovement.class); PhysicsUtil.setPosition(component, comPos); } }
From source file:com.dongbat.invasion.ability.types.Barrier.java
private Array<Vector2> getComponentPositions(Vector2 center, int angle, int count) { Array<Vector2> positions = new Array<Vector2>(count); if (count % 2 == 1) { positions.add(center.cpy());/*w w w . j ava 2 s. c o m*/ } float sin = MathUtils.sinDeg(angle); float cos = MathUtils.cosDeg(angle); Float m = cos == 0 ? null : (sin / cos); int sideCount = count / 2; for (int i = 0; i < sideCount; i++) { int distance = count % 2 == 0 ? radius : 2 * radius; distance += 2 * radius * i; if (cos == 0) { positions.add(new Vector2(center.x, center.y - distance)); positions.add(new Vector2(center.x, center.y + distance)); } else if (sin == 0) { positions.add(new Vector2(center.x - distance, center.y)); positions.add(new Vector2(center.x + distance, center.y)); } else { float diffX = (float) Math.sqrt(distance * distance / (m * m + 1)); positions.add(new Vector2(center.x - diffX, center.y - m * diffX)); positions.add(new Vector2(center.x + diffX, center.y + m * diffX)); } } return positions; }
From source file:com.dongbat.invasion.bullet.types.BouncingPiercingBullet.java
@Override public void update(Entity e) { Vector2 velocity = PhysicsUtil.getVelocity(e); OutOfBound outOfBound = PhysicsUtil.isGoingOutOfBound(e); if (outOfBound == OutOfBound.HORIZONTAL) { PhysicsUtil.setVelocity(e, new Vector2(-velocity.x, velocity.y)); } else if (outOfBound == OutOfBound.VERTICAL) { PhysicsUtil.setVelocity(e, new Vector2(velocity.x, -velocity.y)); }// w w w. j a v a 2 s . c o m if (outOfBound != OutOfBound.NONE) { times--; } if (times < 0) { BulletUtil.destroy(e); } }
From source file:com.dongbat.invasion.bullet.types.BouncingVacumnExplosiveBullet.java
@Override public void update(Entity e) { Collision collision = EntityUtil.getComponent(e, Collision.class); if (collision.collidedList.size <= 0) { disable = false;/* w ww.j av a 2 s . c o m*/ } Vector2 velocity = PhysicsUtil.getVelocity(e); PhysicsUtil.OutOfBound outOfBound = PhysicsUtil.isGoingOutOfBound(e); if (outOfBound == PhysicsUtil.OutOfBound.HORIZONTAL) { PhysicsUtil.setVelocity(e, new Vector2(-velocity.x, velocity.y)); } else if (outOfBound == PhysicsUtil.OutOfBound.VERTICAL) { PhysicsUtil.setVelocity(e, new Vector2(velocity.x, -velocity.y)); } if (outOfBound != PhysicsUtil.OutOfBound.NONE) { times--; } if (times <= 0) { BulletUtil.destroy(e); } }
From source file:com.dongbat.invasion.bullet.types.VacumnExplosiveBullet.java
@Override public void collided(Entity bullet, Entity enemy) { Vector2 bulletPosition = PhysicsUtil.getPosition(bullet); Array<Entity> enemies = PhysicsUtil.findEnemiesInRadius(bulletPosition, 20); for (Entity e : enemies) { PhysicsUtil.setPosition(e, new Vector2(bulletPosition.x, PhysicsUtil.getPosition(enemy).y)); }//from w w w.j ava 2 s . c o m DamageUtil.damageAoe(bulletPosition, Constants.BULLET.DEFAULT_DAMAGE, Constants.BULLET.DEFAULT_EXPLOSIVE_RADIUS); }