Skip to content

Commit

Permalink
Merge pull request #45 from justindujardin/refactor/combat-components
Browse files Browse the repository at this point in the history
Refactor/combat components
  • Loading branch information
justindujardin authored Nov 3, 2022
2 parents 9fb9673 + 320034f commit f7e1e99
Show file tree
Hide file tree
Showing 26 changed files with 325 additions and 470 deletions.
2 changes: 0 additions & 2 deletions src/app/behaviors/animated-sprite.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export interface AnimatedSpriteComponentOptions {
export class AnimatedSpriteBehavior extends TickedBehavior {
host: TileObject;
_elapsed: number = 0;
private _renderFrame: number = 3;
lengthMS: number = 500;
spriteComponent: SpriteComponent;
spriteName: string;
Expand All @@ -47,7 +46,6 @@ export class AnimatedSpriteBehavior extends TickedBehavior {
}

connectBehavior(): boolean {
this._renderFrame = 0;
this._elapsed = 0;
return super.connectBehavior();
}
Expand Down
5 changes: 0 additions & 5 deletions src/app/behaviors/base-player.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ export class BasePlayerComponent extends MovableBehavior {

host: TileObject;
passableKeys: string[] = ['passable'];
private _lastFrame: number = 3;
private _renderFrame: number = 3;
heading: Point = new Point(0, -1);
sprite: PlayerRenderBehaviorComponent | null = null;
collideComponentType: any = TileObjectBehavior;
Expand All @@ -56,9 +54,6 @@ export class BasePlayerComponent extends MovableBehavior {
}

tick(elapsed: number): void {
// There are four states and two rows. The second row is all alt states, so mod it out
// when a move ends.
this._lastFrame = this._renderFrame > 3 ? this._renderFrame - 4 : this._renderFrame;
super.tick(elapsed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import { getCombatEntityEquipment } from '../../../../models/selectors';
import { assertTrue } from '../../../../models/util';
import { GameEntityObject } from '../../../../scene/objects/game-entity-object';
import { GameWorld } from '../../../../services/game-world';
import { CombatPlayerComponent } from '../../combat-player.component';
import { CombatComponent } from '../../combat.component';
import { CombatAttackSummary, IPlayerActionCallback } from '../../combat.types';
import { CombatActionBehavior } from '../combat-action.behavior';
import { CombatPlayerRenderBehaviorComponent } from '../combat-player-render.behavior';

/**
* Attack another entity in combat.
*/
Expand Down Expand Up @@ -77,9 +78,11 @@ export class CombatAttackBehaviorComponent extends CombatActionBehavior {
attackerModel && defenderModel,
'invalid attacker/defender model attack behavior'
);
const playerRender = attacker.findBehavior<CombatPlayerRenderBehaviorComponent>(
CombatPlayerRenderBehaviorComponent
);
const playerRender =
attacker instanceof CombatPlayerComponent
? (attacker as CombatPlayerComponent)
: null;

const attack = () => {
const attEquip = this.store.select(getCombatEntityEquipment(attackerModel.eid));
const defEquip = this.store.select(getCombatEntityEquipment(defenderModel.eid));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Component, Input } from '@angular/core';
import { Store } from '@ngrx/store';
import { CombatPlayerRenderBehaviorComponent } from '..';
import { AppState } from '../../../../app.model';
import { AnimatedSpriteBehavior } from '../../../../behaviors/animated-sprite.behavior';
import { SoundBehavior } from '../../../../behaviors/sound-behavior';
Expand All @@ -10,6 +9,7 @@ import { CombatAttack } from '../../../../models/combat/combat.model';
import { GameStateRemoveInventoryAction } from '../../../../models/game-state/game-state.actions';
import { assertTrue } from '../../../../models/util';
import { GameEntityObject } from '../../../../scene/objects/game-entity-object';
import { CombatPlayerComponent } from '../../combat-player.component';
import { CombatComponent } from '../../combat.component';
import { IPlayerActionCallback } from '../../combat.types';
import { CombatEndTurnStateComponent } from '../../states/combat-end-turn.state';
Expand Down Expand Up @@ -62,9 +62,7 @@ export class CombatItemBehavior extends CombatActionBehavior {
assertTrue(userModel, 'invalid item user model');
assertTrue(targetModel, 'invalid item target model');
assertTrue(item, 'invalid item target model');
const userRender = user.findBehavior<CombatPlayerRenderBehaviorComponent>(
CombatPlayerRenderBehaviorComponent
);
const userRender = user as CombatPlayerComponent;
assertTrue(userRender, 'item user has no render behavior');
userRender.magic(() => {
// TODO: We need some kind of effects registry and a way to specify which
Expand Down
11 changes: 3 additions & 8 deletions src/app/routes/combat/behaviors/actions/combat-magic.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as Immutable from 'immutable';
import { combineLatest } from 'rxjs';
import { first, map } from 'rxjs/operators';
import * as _ from 'underscore';
import { CombatPlayerRenderBehaviorComponent } from '..';
import { CombatComponent } from '../..';
import { AppState } from '../../../../../app/app.model';
import { EntityType, IPartyMember } from '../../../../../app/models/base-entity';
Expand Down Expand Up @@ -34,6 +33,7 @@ import {
} from '../../../../models/game-data/game-data.model';
import { IMagicTargetDelta } from '../../../../models/mechanics';
import { assertTrue } from '../../../../models/util';
import { CombatPlayerComponent } from '../../combat-player.component';
import { CombatAttackSummary, IPlayerActionCallback } from '../../combat.types';
import { CombatEndTurnStateComponent } from '../../states';
import { CombatActionBehavior } from '../combat-action.behavior';
Expand Down Expand Up @@ -103,10 +103,7 @@ export class CombatMagicBehavior extends CombatActionBehavior {
assertTrue(casterModel, 'CombatMagicBehavior: invalid caster source model');
assertTrue(targetModel, 'CombatMagicBehavior: invalid caster target model');

const attackerPlayer = caster.findBehavior(
CombatPlayerRenderBehaviorComponent
) as CombatPlayerRenderBehaviorComponent;

const attackerPlayer = caster as CombatPlayerComponent;
attackerPlayer.magic(() => {
var healAmount: number = -spell.value;
const healData: CombatAttack = {
Expand Down Expand Up @@ -161,9 +158,7 @@ export class CombatMagicBehavior extends CombatActionBehavior {
const targetModel: any = target.model;
assertTrue(casterModel, 'CombatMagicBehavior: invalid caster source model');
assertTrue(targetModel, 'CombatMagicBehavior: invalid caster target model');
const attackerPlayer = caster.findBehavior<CombatPlayerRenderBehaviorComponent>(
CombatPlayerRenderBehaviorComponent
);
const attackerPlayer = caster as CombatPlayerComponent;
const castSpell = () => {
combineLatest([
this.store.select(getCombatEntityEquipment(casterModel.eid)),
Expand Down
7 changes: 3 additions & 4 deletions src/app/routes/combat/behaviors/choose-action.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { GameEntityObject } from '../../../scene/objects/game-entity-object';
import { Scene } from '../../../scene/scene';
import { GameWorld } from '../../../services/game-world';
import { CombatActionBehavior } from '../behaviors/combat-action.behavior';
import { CombatPlayerRenderBehaviorComponent } from '../behaviors/combat-player-render.behavior';
import { CombatPlayerComponent } from '../combat-player.entity';
import { CombatPlayerComponent } from '../combat-player.component';
import { CombatSceneClick, ICombatMenuItem, IPlayerAction } from '../combat.types';
import {
CombatChooseActionStateComponent,
Expand Down Expand Up @@ -49,7 +48,7 @@ export type CombatChooseActionStateNames =
export class ChooseActionStateMachine extends StateMachine<CombatChooseActionStateNames> {
current: GameEntityObject | null = null;
target: GameEntityObject | null = null;
player: CombatPlayerRenderBehaviorComponent | null = null;
player: CombatPlayerComponent | null = null;
action: CombatActionBehavior | null = null;
spell: ITemplateMagic | null = null;
item: Item | null = null;
Expand Down Expand Up @@ -94,7 +93,7 @@ export class ChooseActionType extends State<CombatChooseActionStateNames> {
throw new Error('Requires Current Player');
}
const p: CombatPlayerComponent = machine.current as CombatPlayerComponent;
machine.player = p.render;
machine.player = p;
if (!machine.player) {
throw new Error('Requires player render component for combat animations.');
}
Expand Down
49 changes: 0 additions & 49 deletions src/app/routes/combat/behaviors/combat-camera.behavior.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/app/routes/combat/behaviors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './combat-action.behavior';
export * from './combat-camera.behavior';
export * from './combat-player-render.behavior';
5 changes: 5 additions & 0 deletions src/app/routes/combat/combat-enemy.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<sprite-render-behavior
[icon]="(model$ | async)?.icon || null"
></sprite-render-behavior>
<combat-attack-behavior [combat]="combat"></combat-attack-behavior>
<ng-content></ng-content>
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import { CombatAttackBehaviorComponent } from './behaviors/actions/combat-attack

@Component({
selector: 'combat-enemy',
template: `
<sprite-render-behavior
[icon]="(model$ | async)?.icon || null"
></sprite-render-behavior>
<combat-attack-behavior [combat]="combat"></combat-attack-behavior>
<ng-content></ng-content>
`,
templateUrl: 'combat-enemy.component.html',
})
export class CombatEnemyComponent
extends GameEntityObject
Expand Down
18 changes: 0 additions & 18 deletions src/app/routes/combat/combat-map.entity.html

This file was deleted.

Loading

0 comments on commit f7e1e99

Please sign in to comment.