public CannonBody(Body body, float radius, int direction, float offset, boolean searchLight) { super(body, radius, direction, offset, true, false); bodyType = BODY_TYPE.METAL; float random = MathUtils.random(); if (random < .5f) { image = ResourceFactory.getInstance().getItemImage("cannon1"); type = CANNON_TYPE.SINGLE; weapon = new Gun(); weapon.onPickup(this); } else { image = ResourceFactory.getInstance().getItemImage("cannon2"); type = CANNON_TYPE.DOUBLE; weapon = new Fireball(); weapon.onPickup(this); } if (searchLight) { addLight( LightFactory.getInstance() .getConeLight( getBody().getPosition(), Config.getDimensions().WORLD_WIDTH, 3, Color.RED, startDirection, 2)); light.setSoftnessLength(Config.TILE_SIZE_X); light.setActive(false); light.setStaticLight(false); light.setSoft(false); light.setXray(true); } }
@Override public void update() { if (!isActive()) return; posCpy.set(GameModel.getPlayer().position); diffToPlayer.set(posCpy.sub(position)); float wantedAngle = MathUtils.atan2(diffToPlayer.y, diffToPlayer.x); /* ^ 3,14 <--- |----> 0 */ if (direction == AbstractMap.N && (wantedAngle > MathUtils.PI || wantedAngle < 0)) return; else if (direction == AbstractMap.S && wantedAngle > 0) return; else if (direction == AbstractMap.W && (!(wantedAngle < MathUtils.PI / 2f && wantedAngle > -MathUtils.PI / 2f))) return; else if (direction == AbstractMap.E && (!(wantedAngle > MathUtils.PI / 2f || wantedAngle < -MathUtils.PI / 2f))) return; body.setTransform(position.x, position.y, wantedAngle); if (diffToPlayer.len() < Config.getDimensions().WORLD_WIDTH / 2f) if (MathUtils.random() < (type == CANNON_TYPE.SINGLE ? Config.BULLET_RATE_LOW : Config.BULLET_RATE_MEDIUM)) { float angle = body.getAngle() * MathUtils.radiansToDegrees; origin.set(position).add(diffToPlayer.scl(.1f)); posCpy.set(GameModel.getPlayer().position); if (type == CANNON_TYPE.SINGLE) { weapon.fire1(origin, posCpy.rotate(MathUtils.random(-1, 1))); createGunSmoke(angle); } else if (type == CANNON_TYPE.DOUBLE) { weapon.fire1(origin, posCpy.rotate(MathUtils.random(-1, 1))); weapon.fire2(origin, posCpy.rotate(MathUtils.random(-1, 1))); } } }