public void addItem(float x, float y) throws SlickException { int j = random.nextInt(2); switch (j) { case 0: HealthPack HP = new HealthPack(20); items.addHealthPack(HP); HP.initItem(); HP.setPosition((int) x, (int) y); break; case 1: int k = random.nextInt(10) + 1; if (k < 7) { Weapon gun = new Weapon(150, 1000, -10, "Gun"); items.addWeapon(gun); gun.initItem(); gun.setPosition((int) x, (int) y); } else if (k >= 7) { Weapon shotGun = new Weapon(70, 1500, -30, "Shotgun"); items.addWeapon(shotGun); shotGun.initItem(); shotGun.setPosition((int) x, (int) y); } break; } }
@Override public void updateGameLoop(long timeSinceLastFrame) { // TODO Auto-generated method stub super.updateGameLoop(timeSinceLastFrame); time += (timeSinceLastFrame / 1000000); realtime += time / 1000; if (time > secondsPerDay) { time = time % secondsPerDay; /* daybreak! spawn more items and mobs */ for (int i = 0; i < mobSpawnCount; i++) { this.mobs.add((MobAI) level.spawnMob()); } mobSpawnCount += 20; Item ammo = new AmmoPack(); ammo.origin = level.getSpawnLocation(); this.level.addItem(ammo); HealthPack h = new HealthPack(); h.origin = this.level.randomSpawnPoint(); this.level.addItem(h); } ArrayList<Projectile> died = new ArrayList<Projectile>(); ArrayList<Mob> diedMobs = new ArrayList<Mob>(); for (Projectile p : projectiles) { p.update(timeSinceLastFrame); for (Mob m : mobs) { if (m.hitTest(p) && !diedMobs.contains(m)) { died.add(p); m.dealDamage(p.damage); if (m.health == 0) { diedMobs.add(m); score++; } } } mobs.removeAll(diedMobs); diedMobs.clear(); if (p.ttl < 0) { died.add(p); } } projectiles.removeAll(died); if (player.weapon != null && player.fireButtonPressed && player.weapon.readyToFire()) { Projectile[] p = player.weapon.fire(player); for (Projectile projectile : p) { projectiles.add(projectile); } } player.update(timeSinceLastFrame); for (MobAI mob : mobs) { Vector2f dist = new Vector2f(mob.origin.x - player.origin.x, mob.origin.y - player.origin.y); if (Math.abs(dist.x) > Math.abs(dist.y)) { mob.setDirection((dist.x < 0) ? Sprite.Direction.RIGHT : Sprite.Direction.LEFT); } else { mob.setDirection((dist.y < 0) ? Sprite.Direction.DOWN : Sprite.Direction.UP); } mob.update(timeSinceLastFrame); if (mob.hitTest(player.origin, 2)) { player.dealDamage(1); } } if (player.health == 0) { this.launcher.setGameWindow(new ZedGameOverMenu(this.score)); } for (Item item : level.items) { if (item != null) item.update(timeSinceLastFrame); } }