List of usage examples for com.badlogic.gdx.math Vector2 cpy
@Override
public Vector2 cpy()
From source file:com.dongbat.game.buff.effects.RandomDestinationSchedule.java
@Override public void durationStart(World world, Entity source, Entity target) { UnitMovement unitComponent = EntityUtil.getComponent(world, target, UnitMovement.class); float posX = (float) (ECSUtil.getRandom(world).getFloat(-1, 1) * scaleX); float posY = (float) (ECSUtil.getRandom(world).getFloat(-1, 1) * scaleY); Vector2 position = PhysicsUtil.getPosition(world, target); Vector2 destination = (new Vector2(posX, posY)).cpy().sub(position.cpy()); unitComponent.setDirectionVelocity(destination.cpy().nor()); }
From source file:com.dongbat.game.buff.effects.SelfDefense.java
@Override public void update(World world, Entity source, Entity target) { if (ECSUtil.getFrame(world) - lastFrameCast > framePerFood) { lastFrameCast = ECSUtil.getFrame(world); Detection detection = EntityUtil.getComponent(world, target, Detection.class); UUID id = detection.getNearestPlayer(); if (id != null) { Entity e = UuidUtil.getEntityByUuid(world, id); if (e == null || !e.isActive() || PhysicsUtil.getBody(world, e) == null) { return; }/*from w ww . j ava 2 s . c om*/ Vector2 entityPos = PhysicsUtil.getPosition(world, e); Vector2 queenPos = PhysicsUtil.getPosition(world, source); Vector2 destination = entityPos.cpy().sub(queenPos.cpy()); for (int i = 0; i < 1; i++) { float d = ECSUtil.getRandom(world).getFloat(-30, 30); Vector2 direction = destination.cpy().scl(1).nor().rotate(d); Entity food = EntityFactory.createFood(world, queenPos, UuidUtil.getUuid(source)); PhysicsUtil.setRadius(world, food, foodRadius); BuffUtil.addBuff(world, source, food, "ToBeRemoved", 1000, 1); BuffUtil.addBuff(world, source, food, "ToxicFood", 1000, 1); BuffUtil.addBuff(world, source, food, "Forced", (int) (400 * ECSUtil.getRandom(world).getFloat(0.5f, 1.25f)), 1, "forceStrength", 5f, "direction", direction); } } } }
From source file:com.dongbat.game.util.MovementUtil.java
/** * Move unit to specific location on map * * @param entity entity want to move// w w w. j a va 2 s .c o m * @param destination location that entity will move to */ public static void moveTo(Entity entity, Vector2 destination) { World world = entity.getWorld(); Physics physicsComponent = EntityUtil.getComponent(world, entity, Physics.class); Stats stat = EntityUtil.getComponent(world, entity, Stats.class); Body body = physicsComponent.getBody(); float mass = body.getMass(); Vector2 currentVelocity = body.getLinearVelocity(); Vector2 position = body.getPosition(); float scale = stat.getBaseMovementSpeed() + stat.getModifierSpeed(); Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(scale); Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass * 10); PhysicsUtil.applyImpulse(entity.getWorld(), entity, impulse); }
From source file:com.dongbat.game.util.PlaneMathCalculator.java
public static float getAlpha(Vector2 centerPosition, Vector2 collisionPosition, Vector2 bulletDirection) { Vector2 perpendicularVector = getPerpendicularVector(bulletDirection.cpy()); Vector2 centerToConner = collisionPosition.cpy().sub(centerPosition.cpy()); float angleRad = centerToConner.angleRad(perpendicularVector); return angleRad * 2; }
From source file:com.dongbat.game.util.WorldQueryUtil.java
public static Array<Entity> filterEntityInRay(World world, Array<Entity> entities, Vector2 origin, Vector2 direction, float degree) { Array<Entity> filter = new Array<Entity>(); for (Entity e : entities) { Vector2 entityPos = getPosition(world, e); float angle = entityPos.cpy().sub(origin.cpy()).angle(direction.cpy()); if (angle <= degree && angle >= -degree) { filter.add(e);//w w w . j a va 2 s . c o m } } return filter; }
From source file:com.dongbat.game.util.WorldQueryUtil.java
public static boolean isNearer(World world, Entity base, Entity newEntity) { if (base == null || newEntity == null) { return false; }/*from w w w.j av a 2 s . c o m*/ Detection detect = EntityUtil.getComponent(world, base, Detection.class); UUID lastNearestUuid = detect.getNearestPlayer(); if (base.equals(newEntity)) { return false; } Entity lastNearestEntity = UuidUtil.getEntityByUuid(world, lastNearestUuid); if (lastNearestEntity == null) { return true; } Vector2 basePos = getPosition(world, base); Vector2 newEntityPos = getPosition(world, newEntity); Vector2 oldPos = getPosition(world, lastNearestEntity); float distanceFromOld = basePos.cpy().sub(oldPos.cpy()).len2(); float distanceFromNew = basePos.cpy().sub(newEntityPos.cpy()).len2(); return distanceFromNew <= distanceFromOld; }
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()); }/*from ww w . ja v a 2 s .c om*/ 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.SplitStunBullet.java
@Override public void collided(Entity bullet, Entity enemy) { if (disabled) { return;/*from w w w.j a va2 s . co m*/ } DamageUtil.damageSingle(enemy, Constants.BULLET.DEFAULT_DAMAGE); BuffUtil.addBuff(bullet, enemy, "stun", duration); if (level > 0) { Vector2 bulletPosition = PhysicsUtil.getPosition(bullet); Vector2 enemyPosition = PhysicsUtil.getPosition(enemy); Vector2 fireVector = bulletPosition.cpy().sub(enemyPosition); float enemyRadius = PhysicsUtil.getRadius(enemy); for (int i = 0; i < Constants.BULLET.DEFAULT_NUMBER_SUBBULLET; i++) { // float angle = (float) (Math.random() * 340 + 20); // Vector2 enemyPosition = new Vector2(PhysicsUtil.getPosition(enemy)); // Vector2 v = new Vector2(enemyRadius + 10, 0); // Vector2 vRotation = v.rotate(angle); // Vector2 fireVector = new Vector2(vRotation).add(enemyPosition); // Entity subBullet = BulletRegistry.createBullet("splitStun", fireVector); // SplitStunBullet type = (SplitStunBullet) EntityUtil.getComponent(subBullet, Bullet.class).getType(); // PhysicsUtil.applyImpulse(subBullet, new Vector2(vRotation).scl(10)); // type.level = level - 1; // type.startTime = TimeUtil.getGameTime(); // type.isSubBullet = true; // Body body = EntityUtil.getComponent(enemy, Physics.class).getBody(); Entity subBullet = BulletRegistry.createBullet("splitStun", bulletPosition.cpy()); SplitStunBullet type = (SplitStunBullet) EntityUtil.getComponent(subBullet, Bullet.class).getType(); PhysicsUtil.applyImpulse(subBullet, fireVector.cpy().rotate(20 * (i - Constants.BULLET.DEFAULT_NUMBER_SUBBULLET / 2)).scl(10)); type.level = level - 1; type.disabled = true; } } BulletUtil.destroy(bullet); }
From source file:com.dongbat.invasion.component.DisplayPosition.java
public DisplayPosition(Vector2 position) { this.position = position.cpy(); }
From source file:com.dongbat.invasion.system.EnemyMovementSystem.java
@Override protected void process(Entity entity) { Delay delay = delayMapper.get(entity); if (delay != null && delay.getDuration() > 0) { long timePassed = TimeUtil.getGameTimeSince(delay.getStartTime()); if (timePassed <= delay.getDuration()) { return; }//from w w w . java 2 s. c o m } // Vector2 prevVelocity = PhysicsUtil.getVelocity(entity).cpy(); Vector2 position = PhysicsUtil.getPosition(entity); EnemyMovement movement = EntityUtil.getComponent(entity, EnemyMovement.class); PhysicsUtil.setVelocity(entity, new Vector2()); Vector2 intent = movement.getIntent().cpy(); Vector2 travel; MovementUtil.moveEnemyTo(entity, intent); // TODO check free enemy intent.y = position.y; // TODO free movement unit use moveTo Vector2 distance = intent.cpy().sub(position); travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta); if (travel.dot(distance) > 0 && travel.len2() - distance.len2() > 0) { PhysicsUtil.setPosition(entity, intent); PhysicsUtil.setVelocity(entity, new Vector2()); } if (TimeUtil.getGameTimeSince(movement.getStartDrag()) >= movement.getDragDuration()) { movement.setDraggedTo(null); } if (movement.getDraggedTo() == null) { return; } float max = movement.getDraggedTo().cpy().sub(movement.getDraggedFrom()).len2(); float actual = position.cpy().sub(movement.getDraggedFrom()).len2(); if (actual >= max) { return; } Vector2 drag = movement.getDraggedTo().cpy().sub(position); float dragLen2 = drag.len2(); if (dragLen2 > movement.getDragForce() * movement.getDragForce()) { drag.nor().scl(movement.getDragForce()); } PhysicsUtil.applyImpulseOnEnemy(entity, drag); // TODO fix this similar to above travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta); if (travel.sub(drag).len2() <= Constants.GAME.EPSILON_SQUARED) { PhysicsUtil.setPosition(entity, movement.getDraggedTo()); PhysicsUtil.applyImpulseOnEnemy(entity, drag.scl(-1)); } }