Minor changes

Fixed magic weapons using the wrong sound
Made some changes to better detect the weapon used to attack
This commit is contained in:
Kristian Knarvik 2018-03-20 11:05:15 +01:00
parent 30ba612e56
commit 6ad8eb93d7
9 changed files with 71 additions and 36 deletions

View File

@ -7,6 +7,7 @@ import inf101.v18.rogue101.items.Sword;
import inf101.v18.rogue101.objects.IItem;
import inf101.v18.rogue101.objects.INonPlayer;
import inf101.v18.rogue101.shared.NPC;
import inf101.v18.rogue101.states.Attack;
public class Boss implements INonPlayer {
private int hp = getMaxHealth();
@ -20,7 +21,7 @@ public class Boss implements INonPlayer {
@Override
public void doTurn(IGame game) {
loc = game.getLocation();
NPC.tryAttack(game, 1);
NPC.tryAttack(game, 1, Attack.MELEE);
}
@Override
@ -80,7 +81,7 @@ public class Boss implements INonPlayer {
@Override
public int getVision() {
return 2;
return 3;
}
@Override

View File

@ -7,10 +7,10 @@ import inf101.v18.rogue101.objects.IItem;
import inf101.v18.rogue101.objects.INonPlayer;
import inf101.v18.rogue101.shared.NPC;
import inf101.v18.rogue101.states.Age;
import inf101.v18.rogue101.states.Attack;
import inf101.v18.rogue101.states.Occupation;
import inf101.v18.rogue101.states.Personality;
import java.nio.channels.ClosedSelectorException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -146,16 +146,16 @@ public class Girl implements INonPlayer {
private boolean attack(IGame game) {
switch (occupation) {
case KNIGHT:
if (NPC.tryAttack(game, 1)) {
if (NPC.tryAttack(game, 1, Attack.MELEE)) {
return true;
}
break;
case MAGE:
if (NPC.tryAttack(game, 2)) {
if (NPC.tryAttack(game, 2, Attack.MAGIC)) {
return true;
}
case BOWSMAN:
if (NPC.tryAttack(game, 4)) {
if (NPC.tryAttack(game, 4, Attack.RANGED)) {
return true;
}
}

View File

@ -10,6 +10,7 @@ import inf101.v18.rogue101.game.IGame;
import inf101.v18.rogue101.objects.IItem;
import inf101.v18.rogue101.objects.INonPlayer;
import inf101.v18.rogue101.shared.NPC;
import inf101.v18.rogue101.states.Attack;
public class Rabbit implements INonPlayer {
private int food = 0;
@ -30,7 +31,7 @@ public class Rabbit implements INonPlayer {
return;
}
if (NPC.tryAttack(game, 1)) {
if (NPC.tryAttack(game, 1, Attack.MELEE)) {
return;
}

View File

@ -29,6 +29,7 @@ import inf101.v18.rogue101.map.IMapView;
import inf101.v18.rogue101.map.MapReader;
import inf101.v18.rogue101.objects.*;
import inf101.v18.rogue101.shared.NPC;
import inf101.v18.rogue101.states.Attack;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
@ -182,8 +183,7 @@ public class Game implements IGame {
if (!map.has(loc, target)) {
throw new IllegalMoveException("Target isn't there!");
}
//TODO: Detect the weapon used, and use its sound
IWeapon weapon = (IWeapon) currentActor.getItem(IWeapon.class);
IWeapon weapon = (IWeapon) currentActor.getItem(IMeleeWeapon.class);
if (weapon != null) {
NPC.playSound(weapon.getSound());
} else {
@ -209,9 +209,16 @@ public class Game implements IGame {
}
@Override
public ILocation rangedAttack(GridDirection dir, IItem target) {
public ILocation rangedAttack(GridDirection dir, IItem target, Attack type) {
ILocation loc = currentLocation;
IWeapon weapon = (IWeapon) currentActor.getItem(IWeapon.class);
IWeapon weapon = null;
switch (type) {
case MAGIC:
weapon = (IWeapon) currentActor.getItem(IMagicWeapon.class);
break;
case RANGED:
weapon = (IWeapon) currentActor.getItem(IRangedWeapon.class);
}
if (weapon != null) {
NPC.playSound(weapon.getSound());
} else {

View File

@ -12,6 +12,7 @@ import inf101.v18.rogue101.objects.IItem;
import inf101.v18.rogue101.objects.IActor;
import inf101.v18.rogue101.objects.INonPlayer;
import inf101.v18.rogue101.objects.IPlayer;
import inf101.v18.rogue101.states.Attack;
/**
* Game interface
@ -320,7 +321,7 @@ public interface IGame {
* A target item, which should in some square in the given direction
* @return Your new location if the attack resulted in you moving (unlikely)
*/
ILocation rangedAttack(GridDirection dir, IItem target);
ILocation rangedAttack(GridDirection dir, IItem target, Attack type);
/**
* @return A random generator

View File

@ -7,6 +7,6 @@ public interface IMagicWeapon extends IWeapon {
@Override
default String getSound() {
return "audio/Bow_Fire_Arrow-Stephan_Schutze-2133929391.wav";
return "audio/Large Fireball-SoundBible.com-301502490.wav";
}
}

View File

@ -5,6 +5,7 @@ import inf101.v18.grid.ILocation;
import inf101.v18.rogue101.game.IGame;
import inf101.v18.rogue101.items.*;
import inf101.v18.rogue101.shared.NPC;
import inf101.v18.rogue101.states.Attack;
import javafx.scene.input.KeyCode;
import java.util.ArrayList;
import java.util.List;
@ -73,30 +74,44 @@ public class Player implements IPlayer {
game.displayMessage("Please enter your name: ");
writing = true;
} else if (key == KeyCode.R) {
IItem item = getItem(IRangedWeapon.class);
if (item == null) {
game.displayMessage("You do not have a ranged weapon.");
} else {
turnConsumed = rangedAttack(game, 3);
if (!turnConsumed) {
game.displayMessage("No enemies within range.");
}
}
turnConsumed = nonMeleeAttack(game, 3, Attack.RANGED, "ranged");
} else if (key == KeyCode.F) {
IItem item = getItem(IMagicWeapon.class);
if (item == null) {
game.displayMessage("You do not have a magic weapon.");
} else {
turnConsumed = rangedAttack(game, 2);
if (!turnConsumed) {
game.displayMessage("No enemies within range.");
}
}
turnConsumed = nonMeleeAttack(game, 2, Attack.MAGIC, "magic");
}
showStatus(game);
return turnConsumed;
}
/**
* Attacks if the user is able to use the desired attack.
*
* @param game An IGame object
* @param range The range of the weapon
* @param type The type of the attack
* @param weapon The required weapon type
* @return True if an attack was possible. False otherwise
*/
private boolean nonMeleeAttack(IGame game, int range, Attack type, String weapon) {
boolean turnConsumed = false;
IItem item = null;
switch (type) {
case RANGED:
item = getItem(IRangedWeapon.class);
break;
case MAGIC:
item = getItem(IMagicWeapon.class);
}
if (item == null) {
game.displayMessage("You do not have a " + weapon + " weapon.");
} else {
turnConsumed = rangedAttack(game, range, type);
if (!turnConsumed) {
game.displayMessage("No enemies within range.");
}
}
return turnConsumed;
}
/**
* Lets the user write his/her name.
*
@ -428,7 +443,7 @@ public class Player implements IPlayer {
return 3;
}
private boolean rangedAttack(IGame game, int range) {
private boolean rangedAttack(IGame game, int range, Attack type) {
List<ILocation> neighbours = game.getVisible();
for (ILocation neighbour : neighbours) {
if (game.getMap().hasActors(neighbour)) {
@ -437,7 +452,7 @@ public class Player implements IPlayer {
IActor actor = game.getMap().getActors(neighbour).get(0); //We assume there is only one actor.
if (actor instanceof INonPlayer && current.gridDistanceTo(neighbour) <= range) {
GridDirection dir = dirs.get(0); //Only ever has one item
game.rangedAttack(dir, actor);
game.rangedAttack(dir, actor, type);
return true;
}
}

View File

@ -9,6 +9,7 @@ import inf101.v18.rogue101.objects.IActor;
import inf101.v18.rogue101.objects.IItem;
import inf101.v18.rogue101.objects.INonPlayer;
import inf101.v18.rogue101.objects.IPlayer;
import inf101.v18.rogue101.states.Attack;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
@ -31,7 +32,7 @@ public class NPC {
* @param range The range of the equipped weapon
* @return True if an attack or a move towards an enemy was successful. False otherwise
*/
public static boolean tryAttack(IGame game, int range) {
public static boolean tryAttack(IGame game, int range, Attack type) {
List<ILocation> neighbours = game.getVisible();
for (ILocation neighbour : neighbours) {
IMapView map = game.getMap();
@ -40,12 +41,16 @@ public class NPC {
List<GridDirection> dirs = game.locationDirection(current, neighbour);
IActor actor = game.getMap().getActors(neighbour).get(0); //We assume there is only one actor.
for (GridDirection dir : dirs) {
if (range == 1 && current.gridDistanceTo(neighbour) <= 1 && current.canGo(dir) && game.getMap().has(current.go(dir), actor)) {
if (type == Attack.MELEE
&& current.gridDistanceTo(neighbour) <= 1
&& current.canGo(dir)
&& game.getMap().has(current.go(dir), actor)
) {
game.attack(dir, actor);
return true;
}
if (range > 1 && current.gridDistanceTo(neighbour) <= range) {
game.rangedAttack(dir, actor);
game.rangedAttack(dir, actor, type);
return true;
}
}

View File

@ -0,0 +1,5 @@
package inf101.v18.rogue101.states;
public enum Attack {
MELEE, MAGIC, RANGED
}