public void actBehavior(float deltaTime) { Random rand = new Random(); switch (state) { case RESTING: if (stateTime > restTime) { state = MOVING; accel.x = rand.nextFloat() * 2 - 1; // -1 to 1 accel.y = rand.nextFloat() * 2 - 1; accel.nor().scl(ACCEL); stateTime = 0.0f; } else { // still resting... } break; case MOVING: if (stateTime > moveTime) { state = RESTING; accel.set(0, 0); velocity.set(0, 0); stateTime = 0.0f; } else { // still moving... // Continue accel'ing in current direction accel.set(velocity.x, velocity.y).nor().scl(ACCEL); } break; default: break; } stateTime += deltaTime; }
public void lvlChanged(int bouclier) { positionEmiter.nor(); positionEmiter.scl(originalScale); positionEmiter.scl(1 + (bouclier * 0.6f)); speed = 40; speed += (bouclier * 0.66f) * speed; }
public static Vector2 flee(Vector2 targetPos, Vehicle v) { Vector2 desiredVelocity = v.getCenter().cpy().sub(targetPos); Vector2 velocity = v.getVelocity(); desiredVelocity.nor(); desiredVelocity.mul(v.getMaxVelocity()); desiredVelocity.sub(velocity).mul(CORRECTION); return desiredVelocity; }
/** * Slack, like weapon recoil on the joint. * * @param attached */ protected void updateSlack(final Attached attached) { float len = vTmp.set(attached.slackX, attached.slackY).len() - world.delta * attached.tension; if (len > 0) { vTmp.nor().scl(len); } else { vTmp.set(0, 0); } attached.slackX = vTmp.x; attached.slackY = vTmp.y; }
public static Vector2 arrive(Vector2 targetPos, Vehicle v) { Vector2 desiredVelocity = v.getCenter().cpy().sub(targetPos); Vector2 velocity = v.getVelocity(); float len = desiredVelocity.len(); if (len > 0.05f) { desiredVelocity.nor(); float min = Math.min(len, v.getMaxVelocity()); desiredVelocity.mul(min); desiredVelocity.sub(velocity).mul(-CORRECTION); } return desiredVelocity; }
/** * Ray-cast the world in constant steps specified in {@link #setRay(Vector2, Vector2, float)} * * @see World#rayCast(RayCastCallback, Vector2, Vector2) * <p>Note: this implementation asumes that the raycast will hit. */ public void process() { Vector2 step = new Vector2(endPoint); step.sub(beginPoint); step.nor(); step.scl(stepLength); Vector2 endPoint = new Vector2(beginPoint); endPoint.add(step); while (!hit) { world.rayCast(this, beginPoint, endPoint); endPoint.add(step); } }
/** Shoots a {@link Bullet} at the specified position */ public void shoot(Vector2 position) { Vector2 direction = new Vector2(position); direction.sub(this.getPosition()); direction.nor(); this.setAngle(direction.angleRad()); this.setTransform(body.getPosition(), direction.angleRad()); Vector2 velocity = new Vector2(direction.x * BULLET_SPEED, direction.y * BULLET_SPEED); Bullet bullet = new Bullet(level, this.getPosition(), velocity); bullet.setAngle(direction.angleRad()); level.add(bullet); }
/** * Update position & velocity vectors given acceleration and deltaTime * * @param deltaTime */ private void updatePosition(float deltaTime) { if (accel.len() > 0) { accel.scl(deltaTime); // dv = a * dt velocity.add(accel.x, accel.y); // vf = vi + dv // Limit velocity to max if (velocity.len() > MAX_VELOCITY) { velocity.nor().scl(MAX_VELOCITY); } } Vector2 deltaPos = new Vector2(velocity.x * deltaTime, velocity.y * deltaTime); Vector2 newDelta = Collision.tryMove(this, bounds, deltaPos); if (!newDelta.equals(deltaPos)) { // We hit something stopMoving(); } position.add(newDelta); // d = d*dt }
private void updateCamera() { direction.set(0.0f, 0.0f); int mouseX = Gdx.input.getX(); int mouseY = Gdx.input.getY(); int width = Gdx.graphics.getWidth(); int height = Gdx.graphics.getHeight(); if (Gdx.input.isKeyPressed(Input.Keys.LEFT) || (Gdx.input.isTouched() && mouseX < width * 0.75f)) { direction.x = -1; } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) || (Gdx.input.isTouched() && mouseX > width * 0.75f)) { direction.x = 1; } if (Gdx.input.isKeyPressed(Input.Keys.UP) || (Gdx.input.isTouched() && mouseY < height * 0.75f)) { direction.y = 1; } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) || (Gdx.input.isTouched() && mouseY > height * 0.75f)) { direction.y = -1; } direction.nor().scl(CAMERA_SPEED * Gdx.graphics.getDeltaTime()); camera.position.x += direction.x; camera.position.y += direction.y; TiledMapTileLayer layer = (TiledMapTileLayer) tiledMap.getLayers().get(0); float cameraMinX = viewport.getWorldWidth() * 0.5f; float cameraMinY = viewport.getWorldHeight() * 0.5f; float cameraMaxX = layer.getWidth() * layer.getTileWidth() + (playerWidth - cameraMinX); float cameraMaxY = layer.getHeight() * layer.getTileHeight() - cameraMinY; camera.position.x = MathUtils.clamp(sprite.getX(), cameraMinX, cameraMaxX); camera.position.y = MathUtils.clamp(sprite.getY(), cameraMinY, cameraMaxY); camera.update(); }
@Override public void update(float deltaTime) { // Compute player input direction if (cmds.isEmpty() == false) { dir.x = 0; dir.y = 0; if (cmds.contains(Command.MoveUp)) { dir.y += 1; } if (cmds.contains(Command.MoveDown)) { dir.y -= 1; } if (cmds.contains(Command.MoveLeft)) { dir.x -= 1; } if (cmds.contains(Command.MoveRight)) { dir.x += 1; } dir.nor(); // Set the velocity of the player object to the direction * speed // This enables immediate turns super.setVelocity(dir.scl(speed)); super.position.x += velocity.x * deltaTime; super.position.y += velocity.y * deltaTime; bounds.setCenter(super.position); System.out.println(position.toString()); } if (timeLeftOnEmpowered > 0.0f) { timeLeftOnEmpowered -= deltaTime; if (timeLeftOnEmpowered < 0.0f) { timeLeftOnEmpowered = 0.0f; } } }
public void update(float deltaTime) { processKeys(); if (state == FOLLOW) { target.set(map.bob.pos); if (map.bob.dir == Bob.RIGHT) target.x--; if (map.bob.dir == Bob.LEFT) target.x++; target.y += 0.2f; vel.set(target).sub(pos).scl(Math.min(4, pos.dst(target)) * deltaTime); if (vel.len() > MAX_VELOCITY) vel.nor().scl(MAX_VELOCITY); tryMove(); } if (state == CONTROLLED) { accel.scl(deltaTime); vel.add(accel.x, accel.y); if (accel.x == 0) vel.x *= DAMP; if (accel.y == 0) vel.y *= DAMP; if (vel.x > MAX_VELOCITY) vel.x = MAX_VELOCITY; if (vel.x < -MAX_VELOCITY) vel.x = -MAX_VELOCITY; if (vel.y > MAX_VELOCITY) vel.y = MAX_VELOCITY; if (vel.y < -MAX_VELOCITY) vel.y = -MAX_VELOCITY; vel.scl(deltaTime); tryMove(); vel.scl(1.0f / deltaTime); } if (state == FIXED) { if (stateTime > 5.0f) { stateTime = 0; state = FOLLOW; } } stateTime += deltaTime; }
private void doAttack(Array<GameTile> hitTiles, final GameEntity entity, Item weapon) { final GameTile source = entity.tile[0][0]; // Get all the attacked tiles Array<GameTile> attackedTiles = new Array<GameTile>(); if (weapon == null || weapon.wepDef == null || weapon.wepDef.hitType == Item.WeaponDefinition.HitType.ALL) { attackedTiles.addAll(hitTiles); } else if (weapon.wepDef.hitType == Item.WeaponDefinition.HitType.CLOSEST) { int num = weapon.wepDef.hitData != null ? Integer.parseInt(weapon.wepDef.hitData) : 1; Array<GameTile> validEntityTiles = new Array<GameTile>(); Array<GameTile> validEnvironmentTiles = new Array<GameTile>(); // Get tiles valid to hit for (GameTile tile : hitTiles) { if (tile.entity != null && !tile.entity.isAllies(entity)) { validEntityTiles.add(tile); } else if (tile.environmentEntity != null && tile.environmentEntity.canTakeDamage) { validEnvironmentTiles.add(tile); } } Comparator<GameTile> comp = new Comparator<GameTile>() { @Override public int compare(GameTile o1, GameTile o2) { int dist1 = Math.abs(o1.x - source.x) + Math.abs(o1.y - source.y); int dist2 = Math.abs(o2.x - source.x) + Math.abs(o2.y - source.y); return dist1 - dist2; } }; // sort by distance validEntityTiles.sort(comp); validEnvironmentTiles.sort(comp); for (int i = 0; i < num && i < validEntityTiles.size; i++) { attackedTiles.add(validEntityTiles.get(i)); } for (int i = 0; i < num - validEntityTiles.size && i < validEnvironmentTiles.size; i++) { attackedTiles.add(validEnvironmentTiles.get(i)); } } else if (weapon.wepDef.hitType == Item.WeaponDefinition.HitType.RANDOM) { int num = weapon.wepDef.hitData != null ? Integer.parseInt(weapon.wepDef.hitData) : 1; Array<GameTile> validEntityTiles = new Array<GameTile>(); Array<GameTile> validEnvironmentTiles = new Array<GameTile>(); // Get tiles valid to hit for (GameTile tile : hitTiles) { if (tile.entity != null && !tile.entity.isAllies(entity)) { validEntityTiles.add(tile); } else if (tile.environmentEntity != null && tile.environmentEntity.canTakeDamage) { validEnvironmentTiles.add(tile); } } if (validEntityTiles.size > 0) { for (int i = 0; i < num; i++) { attackedTiles.add(validEntityTiles.random()); } } else if (validEnvironmentTiles.size > 0) { for (int i = 0; i < num; i++) { attackedTiles.add(validEnvironmentTiles.random()); } } } Sprite hitEffect = null; if (weapon == null) { hitEffect = entity.defaultHitEffect; } else { hitEffect = weapon.getWeaponHitEffect(); } Point minPoint = Global.PointPool.obtain().set(Integer.MAX_VALUE, Integer.MAX_VALUE); Point maxPoint = Global.PointPool.obtain().set(0, 0); int hitCount = weapon != null && weapon.wepDef != null ? weapon.wepDef.hitCount : 1; float animdelay = 0; for (int i = 0; i < hitCount; i++) { // Do the attack for (GameTile tile : attackedTiles) { // do misses int hitPercent = weapon != null && weapon.wepDef != null ? weapon.wepDef.hitPercent : 100; if (hitPercent < 100) { if (MathUtils.random(100) > hitPercent) { // Argh! a miss! Hit a random surrounding tile Direction dir = Direction.values()[MathUtils.random(Direction.values().length - 1)]; GameTile newTile = tile.level.getGameTile(tile.x + dir.getX(), tile.y + dir.getY()); if (newTile != null) { tile = newTile; } } } if (weapon == null || weapon.wepDef == null || weapon.wepDef.hitType != Item.WeaponDefinition.HitType.ALL) { int[] diff = tile.getPosDiff(source); Sprite sprite = hitEffect.copy(); if (sprite.spriteAnimation != null) { int distMoved = (Math.abs(diff[0]) + Math.abs(diff[1])) / Global.TileSize; sprite.spriteAnimation.set(0.05f * distMoved, diff); } Vector2 vec = new Vector2(diff[0] * -1, diff[1] * -1); vec.nor(); float x = vec.x; float y = vec.y; double dot = 0 * x + 1 * y; // dot product double det = 0 * y - 1 * x; // determinant float angle = (float) Math.atan2(det, dot) * MathUtils.radiansToDegrees; sprite.rotation = angle; sprite.renderDelay = animdelay; animdelay += 0.1f; boolean isMoving = sprite.spriteAnimation != null && sprite.spriteAnimation instanceof MoveAnimation; final SoundInstance sound = hitEffect.sound; final GameTile hitTile = tile; final GameEntity hitEntity = hitTile.entity; final EnvironmentEntity hitEnvEntity = hitTile.environmentEntity; sprite.spriteAction = new SpriteAction( isMoving ? SpriteAction.FirePoint.End : SpriteAction.FirePoint.Start) { @Override public void evaluate() { // do on hit for (GameEventHandler handler : entity.getAllHandlers()) { handler.onHit(entity, hitTile); } if (hitEntity != null && !hitEntity.isAllies(entity)) { entity.attack(hitEntity, dir); } else if (hitEnvEntity != null && !hitEnvEntity.passableBy.intersect(entity.getTravelType())) { entity.attack(hitEnvEntity, dir); } if (sound != null) { sound.play(hitTile); } } }; SpriteEffect effect = new SpriteEffect( sprite, Direction.CENTER, weapon != null && weapon.light != null ? weapon.light.copyNoFlag() : null); tile.spriteEffects.add(effect); } else { // do on hit for (GameEventHandler handler : entity.getAllHandlers()) { handler.onHit(entity, tile); } if (tile.entity != null && !tile.entity.isAllies(entity)) { entity.attack(tile.entity, dir); } else if (tile.environmentEntity != null && !tile.environmentEntity.passableBy.intersect(entity.getTravelType())) { entity.attack(tile.environmentEntity, dir); } if (tile.x < minPoint.x) { minPoint.x = tile.x; } if (tile.x > maxPoint.x) { maxPoint.x = tile.x; } if (tile.y < minPoint.y) { minPoint.y = tile.y; } if (tile.y > maxPoint.y) { maxPoint.y = tile.y; } } } } if (weapon != null && weapon.wepDef != null && weapon.wepDef.hitType == Item.WeaponDefinition.HitType.ALL) { // Use a joined sprite Sprite sprite = hitEffect.copy(); sprite.rotation = dir.getAngle(); sprite.baseScale[0] = (maxPoint.x - minPoint.x) + 1; sprite.baseScale[1] = (maxPoint.y - minPoint.y) + 1; if (dir == Direction.WEST || dir == Direction.EAST) { float temp = sprite.baseScale[0]; sprite.baseScale[0] = sprite.baseScale[1]; sprite.baseScale[1] = temp; } SpriteEffect effect = new SpriteEffect( sprite, Direction.CENTER, weapon != null && weapon.light != null ? weapon.light.copyNoFlag() : null); int px = minPoint.x; int py = minPoint.y; float dx = (maxPoint.x - minPoint.x) / 2.0f; float dy = (maxPoint.y - minPoint.y) / 2.0f; px += dir.getX() < 0 ? Math.ceil(dx) : Math.floor(dx); py += dir.getY() < 0 ? Math.ceil(dy) : Math.floor(dy); GameTile tile = attackedTiles.first().level.getGameTile(px, py); tile.spriteEffects.add(effect); SoundInstance sound = hitEffect.sound; if (sound != null) { sound.play(tile); } } }
protected Vector2 calculateDirectionVector(FlowTouchPad widget) { touchPercentile.set(widget.getKnobPercentX(), widget.getKnobPercentY()); touchPercentile.nor(); return touchPercentile; }