public void onReload(Person person, long now) { if (person.isReload() && person.getShotCooldown() < now && person.getReloadCooldown() < now) { AbstractWeapon weapon = person.getWeapon(); if (weapon.getTotalClip() <= 0) { return; } person.setIsReload(false); int clipToReload = weapon.getClipSize(); if (clipToReload > weapon.getTotalClip()) { clipToReload = weapon.getTotalClip(); } weapon.setCurrentClip(clipToReload); } }
private void onFlameLifecycle(Room room, Flame flame, Integer flameBathcId) { if (!flame.isStoped()) { updatePosition(flame); } if (!flame.isStoped()) { if (flame.getxStart() <= 0 || flame.getxStart() >= room.getMap().getX() || flame.getyStart() <= 0 || flame.getyStart() >= room.getMap().getY()) { flame.setStoped(true); } } Circle flameCircle = new Circle(flame.getxStart(), flame.getyStart(), PersonWebSocketEndpoint.FIRE_RADIUS); if (!flame.isStoped()) { for (AbstractZone zone : room.getMap().getZones()) { if (!zone.isShootable()) { Point[] point = CircleService.circleBoundsIntersection( flameCircle, new Bounds(zone.getX(), zone.getY(), zone.getWidth(), zone.getHeight())); if (point.length > 0) { flame.setStoped(true); break; } } } } for (Person person : room.getPersons().values()) { if (person.getId() != flame.getPersonId()) { Circle personCircle = new Circle(person.getX(), person.getY(), PersonWebSocketEndpoint.PERSON_RADIUS); Point[] point = CircleService.circleCircleIntersection(flameCircle, personCircle); if (point.length > 0) { onDamage(room.getPersons().get(flame.getPersonId()), flame.getDamage(), person, room); room.getProjectiles().remove(flameBathcId); } } } }
public void changeWeapon(Person person, Integer weaponCode) { if (person.isReload()) { return; } String weaponTitle = ""; switch (weaponCode) { case 1: weaponTitle = "knife"; break; case 2: weaponTitle = "pistol"; break; case 3: weaponTitle = "shotgun"; break; case 4: weaponTitle = "assault"; break; case 5: weaponTitle = "sniper"; break; case 6: weaponTitle = "flamethrower"; break; case 7: weaponTitle = "minigun"; break; case 8: weaponTitle = "rocket"; break; } if (!"".equals(weaponTitle)) { for (AbstractWeapon weapon : person.getWeaponList()) { if (weapon.getName().equals(weaponTitle)) { person.setWeapon(weapon); break; } } } }
void handleFire(Person person, Room room) { if (person.isFire()) { AbstractWeapon weapon = person.getWeapon(); long now = System.currentTimeMillis(); if (!person.isReload() && weapon.getCurrentClip() <= 0) { if (weapon.getTotalClip() > 0) { person.setIsReload(true); person.setReloadCooldown(now + person.getWeapon().getReloadTimeout()); } } else if (!person.isReload() && weapon.getCurrentClip() > 0 && person.getShotCooldown() < now) { fire(person, room); } } }
private void onDamage(Person shooter, int damage, Person person, Room room) { int life = person.getLife(); int armor = person.getArmor(); if (armor > 0) { int armorDamage = damage * 2 / 3; person.setArmor(armor - armorDamage); damage = damage - armorDamage; if (person.getArmor() < 0) { damage -= person.getArmor(); person.setArmor(0); } } person.setLife(life - damage); if (person.getLife() <= 0) { personService.kill(person, room); personService.reward(shooter, person); } }
private void handleWeaponPick(Person player, AbstractWeapon weapon) { for (AbstractWeapon personHaving : player.getWeaponList()) { if (personHaving.getClass().equals(weapon.getClass())) { personHaving.setTotalClip(personHaving.getTotalClip() + weapon.getTotalClip()); personHaving.setCurrentClip(personHaving.getClipSize()); if (player.isReload() && personHaving == player.getWeapon()) { player.setReload(false); player.setReloadCooldown(0); } if (personHaving.getTotalClip() > personHaving.getMaxClip()) { personHaving.setTotalClip(weapon.getMaxClip()); } return; } } player.getWeaponList().add(weapon); }
private void onRocketLifecycle(Room room, Rocket rocket, Integer rocketBatchId) { updatePosition(rocket); Circle rocketCircle = new Circle(rocket.getxStart(), rocket.getyStart(), PersonWebSocketEndpoint.ROCKET_RADIUS); Explosion explosion = null; if (rocket.getxStart() <= 0 || rocket.getxStart() >= room.getMap().getX() || rocket.getyStart() <= 0 || rocket.getyStart() >= room.getMap().getY()) { explosion = new Explosion((int) rocket.getxStart(), (int) rocket.getyStart()); } if (explosion == null) { for (AbstractZone zone : room.getMap().getZones()) { if (!zone.isShootable()) { Point[] point = CircleService.circleBoundsIntersection( rocketCircle, new Bounds(zone.getX(), zone.getY(), zone.getWidth(), zone.getHeight())); if (point.length > 0) { explosion = new Explosion((int) point[0].getX(), (int) point[0].getY()); break; } } } } if (explosion == null) { for (Person person : room.getPersons().values()) { if (person.getId() != rocket.getPersonId()) { Circle personCircle = new Circle(person.getX(), person.getY(), PersonWebSocketEndpoint.PERSON_RADIUS); Point[] point = CircleService.circleCircleIntersection(rocketCircle, personCircle); if (point.length > 0) { explosion = new Explosion((int) point[0].getX(), (int) point[0].getY()); onDamage(room.getPersons().get(rocket.getPersonId()), rocket.getDamage(), person, room); break; } } } } if (explosion != null) { explosion.setxEnd((int) explosion.getxStart()); explosion.setyEnd((int) explosion.getyStart()); int id = gameContext.getProjectilesIds().getAndIncrement(); room.getProjectiles().put(id, new Projectile[] {explosion}); Circle explosionCircle = new Circle(explosion.getxStart(), explosion.getyStart(), explosion.getRadius()); Person shooter = room.getPersons().get(rocket.getPersonId()); for (Person person : room.getPersons().values()) { double damageFactor = PointService.pointBelongToCircle( new Point(person.getX(), person.getY()), explosionCircle); if (damageFactor > -1) { double damage = explosion.getDamage() + damageFactor * explosion.getDamageFactor(); onDamage(shooter, (int) damage, person, room); } } room.getProjectiles().remove(rocketBatchId); } }
private float getNewProjectileAngle(Person person) { float spread = (float) person.getWeapon().getSpread(); return person.getAngle() + random.nextFloat() * (spread * 2) - spread; }
private Projectile getCompatibleProjectile(Person person) { float angle = getNewProjectileAngle(person); if (person.getWeapon() instanceof Knife) { return new KnifeAmmo((int) person.getX(), (int) person.getY(), angle); } else if (person.getWeapon() instanceof Pistol || person.getWeapon() instanceof AssaultRifle || person.getWeapon() instanceof Minigun) { return new Bullet((int) person.getX(), (int) person.getY(), angle); } else if (person.getWeapon() instanceof Shotgun) { return new Shot((int) person.getX(), (int) person.getY(), angle); } else if (person.getWeapon() instanceof SniperRifle) { return new SniperBullet((int) person.getX(), (int) person.getY(), angle); } else if (person.getWeapon() instanceof RocketLauncher) { return new Rocket((int) person.getX(), (int) person.getY(), angle, person.getId()); } else if (person.getWeapon() instanceof Flamethrower) { return new Flame((int) person.getX(), (int) person.getY(), angle, person.getId()); } else { throw new RuntimeException("Person have no weapon!"); } }
private void fire(Person person, Room room) { AbstractWeapon gun = person.getWeapon(); person.setShotCooldown(System.currentTimeMillis() + gun.getShotTimeout()); gun.setTotalClip(gun.getTotalClip() - 1); gun.setCurrentClip(gun.getCurrentClip() - 1); Integer id = gameContext.getProjectilesIds().getAndIncrement(); Projectile[] projectilesBatch = new Projectile[gun.getBulletsPerShot()]; for (int i = 0; i < projectilesBatch.length; i++) { Projectile projectile = getCompatibleProjectile(person); projectile.setId(i); projectilesBatch[i] = projectile; float angle = projectile.getAngle(); if (angle == 90) { projectile.setxEnd((int) person.getX()); int gunLimit = (int) (projectile.getRadius() + person.getY()); projectile.setyEnd(gunLimit > room.getMap().getY() ? room.getMap().getY() : gunLimit); } else if (angle == 180) { projectile.setyEnd((int) person.getY()); int gunLimit = (int) person.getX() - (int) projectile.getRadius(); projectile.setyEnd(gunLimit > 0 ? gunLimit : 0); } else if (angle == 270) { projectile.setxEnd((int) person.getX()); int gunLimit = (int) person.getY() - (int) projectile.getRadius(); projectile.setyEnd(gunLimit > 0 ? gunLimit : 0); } else if (angle == 0) { projectile.setyEnd((int) person.getY()); int gunLimit = (int) person.getX() + (int) projectile.getRadius(); projectile.setyEnd(gunLimit > room.getMap().getX() ? room.getMap().getX() : gunLimit); } else { double y = projectile.getRadius() * Math.sin(angle * Math.PI / 180); double x = projectile.getRadius() * Math.cos(angle * Math.PI / 180); projectile.setxEnd((int) x + (int) person.getX()); projectile.setyEnd((int) y + (int) person.getY()); } if (projectile.isInstant()) { calculateInstantImpacts(person, projectile, room); } } room.getProjectiles().put(id, projectilesBatch); }
void doShot(Person person, String status, Room room) { boolean isFire = "1".equals(status); person.setIsFire(isFire); handleFire(person, room); }
private synchronized void calculateInstantImpacts( Person shooter, Projectile projectile, Room room) { double xStart = projectile.getxStart(); double yStart = projectile.getyStart(); Person closestPerson = null; Point closestPoint = null; for (AbstractZone zone : room.getMap().getZones()) { if (zone.isShootable()) { continue; } Bounds zoneBounds = GeometryService.getRectangle(zone); Point[] zoneIntersection = LineService.lineBoundsIntersections( new Line( new Point(shooter.getX(), shooter.getY()), new Point(projectile.getxEnd(), projectile.getyEnd())), zoneBounds); Point closest = isMoreClose( (int) projectile.getxStart(), (int) projectile.getyStart(), new Point(projectile.getxEnd(), projectile.getyEnd()), zoneIntersection); if (closest != null) { closestPoint = closest; projectile.setxEnd((int) closest.getX()); projectile.setyEnd((int) closest.getY()); } } for (Person person : room.getPersons().values()) { Point linePointB = new Point((double) projectile.getxEnd(), (double) projectile.getyEnd()); Point[] intersectionPoints = LineService.lineIntersectCircle( new Line(new Point(xStart, yStart), linePointB), new Circle(person.getX(), person.getY(), PersonWebSocketEndpoint.PERSON_RADIUS)); if (shooter == person) { projectile.setxStart((int) intersectionPoints[0].getX()); projectile.setyStart((int) intersectionPoints[0].getY()); continue; } if (intersectionPoints != null && intersectionPoints.length > 0) { Point closest = isMoreClose( (int) shooter.getX(), (int) shooter.getY(), closestPoint, intersectionPoints); if (closest != null) { if (projectile.isPiercing()) { onDamage(shooter, projectile.getDamage(), person, room); } else { closestPoint = closest; closestPerson = person; } } } } if (closestPerson != null) { onDamage(shooter, projectile.getDamage(), closestPerson, room); } if (closestPoint != null) { projectile.setxEnd((int) closestPoint.getX()); projectile.setyEnd((int) closestPoint.getY()); } }
public void onGetItem(AbstractItem item, Person player) { if (!item.isAvailable()) { return; } switch (item.getType()) { // weapons case "assault": handleWeaponPick(player, new AssaultRifle()); break; case "sniper": handleWeaponPick(player, new SniperRifle()); break; case "minigun": handleWeaponPick(player, new Minigun()); break; case "rocket": handleWeaponPick(player, new RocketLauncher()); break; case "flame": handleWeaponPick(player, new Flamethrower()); break; case "shotgun": handleWeaponPick(player, new Shotgun()); break; // items case "medkit": int life = player.getLife(); if (life < 100) { life += 25; if (life > 100) { life = 100; } } else { return; } player.setLife(life); break; case "armor": int armor = player.getArmor(); if (armor < 100) { armor += 100; if (armor > 100) { armor = 100; } } else { return; } player.setArmor(armor); break; case "helm": int armor1 = player.getArmor(); if (armor1 < 150) { armor1 += 50; if (armor1 > 150) { armor1 = 150; } } else { return; } player.setArmor(armor1); break; default: throw new RuntimeException("Unknonwn weapon name - " + item.getType()); } item.setAvailable(false); item.setTimeout(System.currentTimeMillis() + item.getTime()); }