Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rifle #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.pipai.wf.battle.damage.TargetedActionCalculator;
import com.pipai.wf.battle.damage.WeaponDamageFunction;
import com.pipai.wf.battle.log.BattleEvent;
import com.pipai.wf.battle.weapon.Rifle;
import com.pipai.wf.battle.weapon.Weapon;
import com.pipai.wf.exception.IllegalActionException;
import com.pipai.wf.unit.ability.Ability;
Expand Down Expand Up @@ -57,6 +58,9 @@ protected void performImpl() throws IllegalActionException {
}
Agent target = getTarget();
Weapon w = a.getCurrentWeapon();
if (!(w instanceof Rifle)) {
throw new IllegalActionException("Rifle needed to use Precision Shot");
}
if (w.needsAmmunition() && w.currentAmmo() == 0) {
throw new IllegalActionException("Not enough ammo to fire " + w.name());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.pipai.wf.battle.action;

import com.pipai.wf.battle.agent.Agent;
import com.pipai.wf.battle.damage.DamageCalculator;
import com.pipai.wf.battle.damage.DamageResult;
import com.pipai.wf.battle.damage.PercentageModifier;
import com.pipai.wf.battle.damage.PercentageModifierList;
import com.pipai.wf.battle.damage.TargetedActionCalculator;
import com.pipai.wf.battle.damage.WeaponDamageFunction;
import com.pipai.wf.battle.log.BattleEvent;
import com.pipai.wf.battle.weapon.Weapon;
import com.pipai.wf.exception.IllegalActionException;
import com.pipai.wf.unit.ability.SnapShotAbility;

public class RifleAttackAction extends RangedWeaponAttackAction{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curly brace


public RifleAttackAction(Agent performerAgent, Agent targetAgent) {
super(performerAgent, targetAgent);
}

@Override
public PercentageModifierList getHitCalculation() {
Agent a = getPerformer();
Agent target = getTarget();
PercentageModifierList p = TargetedActionCalculator.baseHitCalculation(a, target);
if (a.hasUsedAP() && a.getAbilities().hasAbility(SnapShotAbility.class)) {
p.add(new PercentageModifier("Snap Shot", -10));
}
return p;
}

@Override
protected void performImpl() throws IllegalActionException {
Agent a = getPerformer();
Agent target = getTarget();
Weapon w = a.getCurrentWeapon();
if (!a.getAbilities().hasAbility(SnapShotAbility.class) && a.hasUsedAP())
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curly brace needs to be on line above this

throw new IllegalActionException("Cannot fire rifle after moving");
}
if (w.needsAmmunition() && w.currentAmmo() == 0) {
throw new IllegalActionException("Not enough ammo to fire " + w.name());
}
if (a.hasUsedAP()) {

}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this unnecessary block

DamageResult result = DamageCalculator.rollDamageGeneral(this, new WeaponDamageFunction(w), 0);
a.setAP(0);
target.takeDamage(result.damage);
log(BattleEvent.rangedWeaponAttackEvent(a, target, w, result));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pipai.wf.battle.action;

import com.pipai.wf.battle.agent.Agent;
import com.pipai.wf.battle.weapon.Rifle;
import com.pipai.wf.battle.weapon.SpellWeapon;
import com.pipai.wf.battle.weapon.Weapon;

Expand All @@ -10,6 +11,8 @@ public static TargetedWithAccuracyActionOWCapable defaultWeaponAction(Agent perf
Weapon weapon = performer.getCurrentWeapon();
if (weapon instanceof SpellWeapon) {
return new TargetedSpellWeaponAction(performer, target);
} else if (weapon instanceof Rifle) {
return new RifleAttackAction(performer, target);
} else {
return new RangedWeaponAttackAction(performer, target);
}
Expand All @@ -19,6 +22,8 @@ public static Class<? extends TargetedWithAccuracyActionOWCapable> defaultWeapon
Weapon weapon = performer.getCurrentWeapon();
if (weapon instanceof SpellWeapon) {
return TargetedSpellWeaponAction.class;
} else if (weapon instanceof Rifle) {
return RifleAttackAction.class;
} else {
return RangedWeaponAttackAction.class;
}
Expand All @@ -28,6 +33,8 @@ public static String defaultWeaponActionName(Agent performer) {
Weapon weapon = performer.getCurrentWeapon();
if (weapon instanceof SpellWeapon) {
return new TargetedSpellWeaponAction(performer, null).name();
} else if (weapon instanceof Rifle) {
return new RifleAttackAction(performer, null).name();
} else {
return new RangedWeaponAttackAction(performer, null).name();
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/pipai/wf/battle/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public int getMaxAP() {
return this.maxAP;
}

public boolean hasUsedAP() {
return (this.ap < this.maxAP);
}

public int getHP() {
return this.hp;
}
Expand Down
68 changes: 68 additions & 0 deletions core/src/main/java/com/pipai/wf/battle/weapon/Rifle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.pipai.wf.battle.weapon;

import com.pipai.wf.battle.action.RifleAttackAction;
import com.pipai.wf.battle.action.TargetedAction;
import com.pipai.wf.battle.action.TargetedActionable;
import com.pipai.wf.battle.agent.Agent;
import com.pipai.wf.math.LinearFunction;

public class Rifle extends Weapon implements TargetedActionable {

@Override
public int flatAimModifier() {
return 0;
}

@Override
public int rangeAimModifier(float distance) {
int minOptimalRange = 7;
int maxRangePenalty = -20;

if (distance <= minOptimalRange) {
return (int) (new LinearFunction(minOptimalRange, 0, 0, maxRangePenalty).eval(distance));
}

return 0;
}

@Override
public int flatCritProbabilityModifier() {
return 20;
}

@Override
public int rangeCritModifier(float distance) {
return 0;
}

@Override
public int minBaseDamage() {
return 4;
}

@Override
public int maxBaseDamage() {
return 6;
}

@Override
public boolean needsAmmunition() {
return true;
}

@Override
public int baseAmmoCapacity() {
return 3;
}

@Override
public String name() {
return "Rifle";
}

@Override
public TargetedAction getAction(Agent performer, Agent target) {
return new RifleAttackAction(performer, target);
}

}
3 changes: 2 additions & 1 deletion core/src/main/java/com/pipai/wf/gui/BattleGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.pipai.wf.battle.map.MapGraph;
import com.pipai.wf.battle.spell.FireballSpell;
import com.pipai.wf.battle.vision.FogOfWar;
import com.pipai.wf.battle.weapon.Rifle;
import com.pipai.wf.battle.weapon.SpellWeapon;
import com.pipai.wf.battle.weapon.Weapon;
import com.pipai.wf.exception.IllegalActionException;
Expand Down Expand Up @@ -639,7 +640,7 @@ public void onKeyDown(int keycode) {
// Skill
if (this.mode == Mode.MOVE) {
for (Ability a : selectedAgent.getAgent().getAbilities()) {
if (a instanceof PrecisionShotAbility && !a.isOnCooldown()) {
if (a instanceof PrecisionShotAbility && !a.isOnCooldown() && selectedAgent.getAgent().getCurrentWeapon() instanceof Rifle) {
this.switchToTargetMode((PrecisionShotAbility) a);
break;
}
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/com/pipai/wf/unit/ability/SnapShotAbility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.pipai.wf.unit.ability;

public class SnapShotAbility extends PassiveAbility {

public SnapShotAbility() {
super(0);
}

@Override
public String name() {
return "Snap Shot";
}

@Override
public String description() {
return "Can fire rifle after moving with -10 to-hit penalty";
}

@Override
public Ability clone() {
return new SnapShotAbility();
}

}
71 changes: 71 additions & 0 deletions core/src/test/java/com/pipai/wf/test/battle/AbilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@
import com.pipai.wf.battle.Team;
import com.pipai.wf.battle.action.PrecisionShotAction;
import com.pipai.wf.battle.action.ReloadAction;
import com.pipai.wf.battle.action.TargetedWithAccuracyActionOWCapable;
import com.pipai.wf.battle.agent.Agent;
import com.pipai.wf.battle.agent.AgentState;
import com.pipai.wf.battle.agent.AgentStateFactory;
import com.pipai.wf.battle.log.BattleEvent;
import com.pipai.wf.battle.map.BattleMap;
import com.pipai.wf.battle.map.GridPosition;
import com.pipai.wf.battle.spell.FireballSpell;
import com.pipai.wf.battle.weapon.Pistol;
import com.pipai.wf.battle.weapon.Rifle;
import com.pipai.wf.exception.IllegalActionException;
import com.pipai.wf.test.MockGUIObserver;
import com.pipai.wf.unit.ability.FireballAbility;
import com.pipai.wf.unit.ability.PrecisionShotAbility;
import com.pipai.wf.unit.ability.QuickReloadAbility;
import com.pipai.wf.unit.ability.RegenerationAbility;
import com.pipai.wf.unit.ability.SnapShotAbility;
import com.pipai.wf.util.UtilFunctions;

public class AbilityTest {

Expand Down Expand Up @@ -125,4 +131,69 @@ public void testPrecisionShotCooldown() {
fail(e.getMessage());
}
}

@Test
public void testNoSnapShot() {
BattleMap map = new BattleMap(5, 5);
GridPosition playerPos = new GridPosition(1, 1);
GridPosition enemyPos = new GridPosition(2, 1);
AgentState playerState = AgentStateFactory.newBattleAgentState(Team.PLAYER, playerPos, 3, 5, 2, 5, 1000, 0);
playerState.weapons.add(new Rifle());
map.addAgent(playerState);
map.addAgent(AgentStateFactory.newBattleAgentState(Team.ENEMY, enemyPos, 3, 5, 2, 5, 65, 0));
BattleController battle = new BattleController(map);
MockGUIObserver observer = new MockGUIObserver();
battle.registerObserver(observer);
Agent player = map.getAgentAtPos(playerPos);
Agent enemy = map.getAgentAtPos(enemyPos);
assertFalse(player == null || enemy == null);
assertFalse(player.hasUsedAP());
player.useAP(1);
assertTrue(player.hasUsedAP());
TargetedWithAccuracyActionOWCapable atk = (TargetedWithAccuracyActionOWCapable) ((Rifle) player.getCurrentWeapon()).getAction(player, enemy);
assertTrue(atk.toHit() == 100);
try {
battle.performAction(atk);
fail("Expected exception not thrown");
} catch (IllegalActionException e) {
}
}

@Test
public void testSnapShot() {
BattleMap map = new BattleMap(5, 5);
GridPosition playerPos = new GridPosition(1, 1);
GridPosition enemyPos = new GridPosition(2, 1);
AgentState playerState = AgentStateFactory.newBattleAgentState(Team.PLAYER, playerPos, 3, 5, 2, 5, 1000, 0);
playerState.abilities.add(new SnapShotAbility());
playerState.weapons.add(new Rifle());
map.addAgent(playerState);
map.addAgent(AgentStateFactory.newBattleAgentState(Team.ENEMY, enemyPos, 3, 5, 2, 5, 65, 0));
BattleController battle = new BattleController(map);
MockGUIObserver observer = new MockGUIObserver();
battle.registerObserver(observer);
Agent player = map.getAgentAtPos(playerPos);
Agent enemy = map.getAgentAtPos(enemyPos);
assertFalse(player == null || enemy == null);
assertFalse(player.hasUsedAP());
player.useAP(1);
assertTrue(player.hasUsedAP());
TargetedWithAccuracyActionOWCapable atk = (TargetedWithAccuracyActionOWCapable) ((Rifle) player.getCurrentWeapon()).getAction(player, enemy);
assertTrue(atk.toHit() == 100);
try {
battle.performAction(atk);
} catch (IllegalActionException e) {
fail(e.getMessage());
}
BattleEvent ev = observer.ev;
assertTrue(ev.getType() == BattleEvent.Type.RANGED_WEAPON_ATTACK);
assertTrue(ev.getPerformer() == player);
assertTrue(ev.getTarget() == enemy);
assertTrue(ev.getChainEvents().size() == 0);
// Player has 1000 aim, cannot miss
assertTrue(ev.getDamageResult().hit);
int expectedHP = UtilFunctions.clamp(0, enemy.getMaxHP(), enemy.getMaxHP() - ev.getDamage());
assertTrue(enemy.getHP() == expectedHP);
assertTrue(player.getHP() == player.getMaxHP());
}
}