Skip to content

Commit

Permalink
Merge pull request #7 from justindujardin/ship-shape
Browse files Browse the repository at this point in the history
fix(ship): issue where ship did not persist in the right state
  • Loading branch information
justindujardin authored Feb 27, 2018
2 parents a9ea8d7 + 87390a0 commit 4e2bfb3
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 88 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/flex-layout": "^2.0.0-beta.8",
"@angular/flex-layout": "2.0.0-beta.8",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/material": "^2.0.0-beta.3",
"@angular/material": "2.0.0-beta.3",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/platform-server": "^4.0.0",
Expand Down Expand Up @@ -184,7 +184,7 @@
"ts-node": "^2.0.0",
"tslint": "~4.3.1",
"typedoc": "^0.5.3",
"typescript": "~2.2.2",
"typescript": "2.2.2",
"url-loader": "^0.5.7",
"v8-lazy-parse-webpack-plugin": "^0.3.0",
"webpack": "2.2.0",
Expand Down
9 changes: 9 additions & 0 deletions src/app/models/game-state/game-state.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ export class GameStateMoveAction implements Action {
}
}

export class GameStateBoardShipAction implements Action {
static typeId: 'GAME_BOARD_SHIP_STATE' = type('GAME_BOARD_SHIP_STATE');
type = GameStateBoardShipAction.typeId;

constructor(public payload: boolean) {
}
}

//
// Gold state actions
//
Expand Down Expand Up @@ -264,6 +272,7 @@ export type GameStateActions
| GameStateTravelFailAction
| GameStateSetBattleCounterAction
| GameStateMoveAction
| GameStateBoardShipAction
| GameStateAddGoldAction
| GameStateHealPartyAction
| GameStateEquipItemAction
Expand Down
4 changes: 2 additions & 2 deletions src/app/models/game-state/game-state.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class GameStateEffects {
* When a save action is dispatched, serialize the app state to local storage.
*/
@Effect() saveGameState$ = this.actions$.ofType(GameStateSaveAction.typeId)
.switchMap((state: AppState) => this.gameStateService.save())
.switchMap(() => this.gameStateService.save())
.map(() => new GameStateSaveSuccessAction())
.catch((e) => {
return Observable.of(new GameStateSaveFailAction(e.toString()));
Expand All @@ -59,7 +59,7 @@ export class GameStateEffects {
* When a delete action is dispatched, remove the saved state in localstorage.
*/
@Effect() clearGameState$ = this.actions$.ofType(GameStateDeleteAction.typeId)
.switchMap((state: AppState) => this.gameStateService.resetGame())
.switchMap(() => this.gameStateService.resetGame())
.map(() => new GameStateDeleteSuccessAction())
.catch((e) => {
return Observable.of(new GameStateDeleteFailAction(e.toString()));
Expand Down
5 changes: 5 additions & 0 deletions src/app/models/game-state/game-state.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export interface GameState {
*/
readonly position: IPoint;

/**
* True when the party is onboard the ship
*/
readonly boardedShip: boolean;

/**
* The position of the party ship in the world.
*/
Expand Down
34 changes: 30 additions & 4 deletions src/app/models/game-state/game-state.reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {gameStateFactory, gameStateReducer} from './game-state.reducer';
import {GameState} from './game-state.model';
import {
GameStateLoadAction,
GameStateAddGoldAction,
GameStateAddInventoryAction,
GameStateBoardShipAction,
GameStateEquipItemAction,
GameStateHealPartyAction,
GameStateTravelAction,
GameStateMoveAction,
GameStateAddGoldAction,
GameStateNewAction,
GameStateRemoveInventoryAction,
GameStateSetKeyDataAction,
GameStateAddInventoryAction, GameStateRemoveInventoryAction, GameStateNewAction, GameStateEquipItemAction,
GameStateTravelAction,
GameStateUnequipItemAction
} from './game-state.actions';
import {Item} from '../item';
Expand Down Expand Up @@ -195,6 +198,29 @@ describe('GameState', () => {
const actual = gameStateReducer(state, new GameStateMoveAction(expected));
expect(actual.position).toEqual(expected);
});
it('should update shipPosition when boardedShip is true', () => {
const state = gameStateFactory({
position: {x: 0, y: 0},
shipPosition: {x: 0, y: 0},
boardedShip: true
});
const expected = pointFactory({x: 10, y: 10});
const actual = gameStateReducer(state, new GameStateMoveAction(expected));
expect(actual.position).toEqual(expected);
expect(actual.shipPosition).toEqual(expected);
});
});
describe('GameStateBoardShipAction', () => {
it('should toggle boardedShip boolean', () => {
[true, false].forEach((value) => {
const state = gameStateFactory({
boardedShip: value
});
const expected = !value;
const actual = gameStateReducer(state, new GameStateBoardShipAction(expected));
expect(actual.boardedShip).toEqual(expected);
});
});
});
describe('GameStateAddInventoryAction', () => {
it('should store the id of the given item in the inventory array', () => {
Expand Down
25 changes: 22 additions & 3 deletions src/app/models/game-state/game-state.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
GameStateActions,
GameStateAddGoldAction,
GameStateAddInventoryAction,
GameStateBoardShipAction,
GameStateDeleteAction,
GameStateDeleteFailAction,
GameStateDeleteSuccessAction,
Expand Down Expand Up @@ -30,7 +31,7 @@ import * as Immutable from 'immutable';
import {Item} from '../item';
import {assertTrue, exhaustiveCheck, makeRecordFactory} from '../util';
import {TypedRecord} from 'typed-immutable-record';
import {pointFactory} from '../records';
import {pointFactory, PointRecord} from '../records';

/**
* Game state record.
Expand All @@ -55,7 +56,8 @@ export const gameStateFactory = makeRecordFactory<GameState, GameStateRecord>({
location: '',
combatZone: '',
position: pointFactory(),
shipPosition: pointFactory()
shipPosition: pointFactory(),
boardedShip: false
});

/**
Expand Down Expand Up @@ -115,8 +117,21 @@ export function gameStateReducer(state: GameStateRecord = gameStateFactory(), ac
case GameStateSaveFailAction.typeId:
return state;
case GameStateMoveAction.typeId: {
if (state.boardedShip) {
return state.merge({
position: action.payload,
shipPosition: action.payload,
});
}
else {
return state.merge({
position: action.payload
});
}
}
case GameStateBoardShipAction.typeId: {
return state.merge({
position: action.payload
boardedShip: action.payload
});
}
case GameStateSetBattleCounterAction.typeId: {
Expand Down Expand Up @@ -182,6 +197,10 @@ export function sliceShipPosition(state: GameState) {
return state.shipPosition;
}

export function sliceBoardedShip(state: GameState) {
return state.boardedShip;
}

export function sliceBattleCounter(state: GameState) {
return state.battleCounter;
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/models/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
sliceShipPosition,
slicePosition,
sliceGold,
slicePartyIds, sliceInventoryIds, sliceGameStateKeyData
slicePartyIds, sliceInventoryIds, sliceGameStateKeyData, sliceBoardedShip
} from './game-state/game-state.reducer';
import {
sliceWeaponIds,
Expand Down Expand Up @@ -146,6 +146,7 @@ export const getGamePartyIds = createSelector(sliceGameState, slicePartyIds);
export const getGamePartyGold = createSelector(sliceGameState, sliceGold);
export const getGamePartyPosition = createSelector(sliceGameState, slicePosition);
export const getGameShipPosition = createSelector(sliceGameState, sliceShipPosition);
export const getGameBoardedShip = createSelector(sliceGameState, sliceBoardedShip);
export const getGameMap = createSelector(sliceGameState, sliceMap);
export const getGameCombatZone = createSelector(sliceGameState, sliceCombatZone);
export const getGameBattleCounter = createSelector(sliceGameState, sliceBattleCounter);
Expand Down
23 changes: 12 additions & 11 deletions src/app/routes/combat/behaviors/actions/combat-run.behavior.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as _ from 'underscore';
import {CombatActionBehavior} from '../combat-action.behavior';
import {CombatRunSummary, CombatEscapeStateComponent} from '../../states/combat-escape.state';
import {CombatEscapeStateComponent, CombatRunSummary} from '../../states/combat-escape.state';
import {CombatEndTurnStateComponent} from '../../states/combat-end-turn.state';
import {Component, Input} from '@angular/core';
import {IPlayerActionCallback} from '../../states/combat.machine';
Expand All @@ -10,8 +10,8 @@ import {CombatComponent} from '../../combat.component';
selector: 'combat-run-behavior',
template: '<ng-content></ng-content>'
})
export class CombatRunBehavior extends CombatActionBehavior {
name: string = "run";
export class CombatRunBehaviorComponent extends CombatActionBehavior {
name: string = 'run';
@Input() combat: CombatComponent;

canTarget(): boolean {
Expand All @@ -22,24 +22,25 @@ export class CombatRunBehavior extends CombatActionBehavior {
if (!this.isCurrentTurn()) {
return false;
}
var success: boolean = this._rollEscape();
var data: CombatRunSummary = {
success: success,
let success: boolean = this._rollEscape();
let data: CombatRunSummary = {
success,
player: this.combat.machine.current
};
this.combat.machine.notify("combat:run", data, () => {
this.combat.machine.notify('combat:run', data, () => {
if (success) {
this.combat.machine.setCurrentState(CombatEscapeStateComponent.NAME);
}
else {
this.combat.machine.setCurrentState(CombatEndTurnStateComponent.NAME);
}
then && then(this);
if (then) {
then(this);
}
});
return true;
}


/**
* Determine if a run action results in a successful escape from
* combat.
Expand All @@ -50,8 +51,8 @@ export class CombatRunBehavior extends CombatActionBehavior {
* @private
*/
private _rollEscape(): boolean {
var roll: number = _.random(0, 200);
var chance: number = 100;
let roll: number = _.random(0, 200);
let chance: number = 100;
if (roll === 200) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/routes/combat/combat-player.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {SceneObjectBehavior} from '../../../game/pow2/scene/scene-object-behavio
import {CombatAttackBehaviorComponent} from './behaviors/actions/combat-attack.behavior';
// import {CombatMagicBehavior} from './behaviors/actions/combat-magic.behavior';
// import {CombatItemBehavior} from './behaviors/actions/combat-item.behavior';
import {CombatRunBehavior} from './behaviors/actions/combat-run.behavior';
import {CombatRunBehaviorComponent} from './behaviors/actions/combat-run.behavior';
// import {CombatGuardBehavior} from './behaviors/actions/combat-guard.behavior';
import {CombatComponent} from './combat.component';
import {Entity} from '../../models/entity/entity.model';
Expand Down Expand Up @@ -73,7 +73,7 @@ export const COMBAT_PLAYER_COMPONENTS = [
CombatAttackBehaviorComponent,
// CombatMagicBehavior,
// CombatItemBehavior,
CombatRunBehavior,
CombatRunBehaviorComponent,
// CombatGuardBehavior,
CombatPlayerComponent
];
30 changes: 28 additions & 2 deletions src/app/routes/world/behaviors/player-render.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {TickedBehavior} from '../../../../game/pow2/scene/behaviors/ticked-behav
import {TileObject} from '../../../../game/pow2/tile/tile-object';
import {Animator} from '../../../../game/pow2/core/animator';
import {Component} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';
export enum MoveFrames {
LEFT = 10,
RIGHT = 4,
Expand All @@ -39,17 +40,42 @@ export enum Headings {

@Component({
selector: 'player-render-behavior',
template: `<ng-content></ng-content>`
template: `
<ng-content></ng-content>`
})
export class PlayerRenderBehaviorComponent extends TickedBehavior {
host: TileObject;
private _animator: Animator = new Animator();
heading: Headings = Headings.WEST;
animating: boolean = false;
private _sourceAnimsFor: string = '';

private _changeSubscription: Subscription;

connectBehavior(): boolean {
if (!super.connectBehavior()) {
return false;
}

// When the host icon changes, invalidate source animation data
this._changeSubscription = this.host.onChangeIcon.subscribe(() => {
this.setHeading(this.heading, false);
});
return true;
}

disconnectBehavior(): boolean {
if (this._changeSubscription) {
this._changeSubscription.unsubscribe();
this._changeSubscription = null;
}
return super.disconnectBehavior();
}

setHeading(direction: Headings, animating: boolean) {
if (!this._animator.sourceAnims) {
if (!this._animator.sourceAnims || this._sourceAnimsFor !== this.host.icon) {
this._animator.setAnimationSource(this.host.icon);
this._sourceAnimsFor = this.host.icon;
}
this.heading = direction;
switch (this.heading) {
Expand Down
Loading

0 comments on commit 4e2bfb3

Please sign in to comment.