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

Milestone 5 #12

Merged
merged 42 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
c9716c5
fix gh-pages asset loading
smileynet Feb 25, 2023
f8528b7
update name and favicon
smileynet Feb 26, 2023
8523cd5
fix asset loading for gh-pages
smileynet Feb 26, 2023
3e482fb
update readme
smileynet Feb 26, 2023
d369342
ractor actions
smileynet Feb 26, 2023
67ab753
add css editor settings
smileynet Feb 26, 2023
d5117b3
fix css style
smileynet Feb 26, 2023
3903620
refactor putout suggested changes
smileynet Feb 26, 2023
4843012
update project settings to add react & redux
smileynet Feb 26, 2023
6aa1835
add react as entry point
smileynet Feb 26, 2023
43c29c8
add App unit test
smileynet Feb 26, 2023
1777723
fix Directions type
smileynet Feb 27, 2023
22f6f6b
remove unused performAction method
smileynet Feb 27, 2023
c8f2b02
fix interactable direction reference
smileynet Feb 27, 2023
1ffef7a
refactor thing tests to interactable
smileynet Feb 27, 2023
e933e16
remove thing test
smileynet Feb 27, 2023
6fa325c
add react settings
smileynet Feb 27, 2023
f59cc4d
remove gh-pages
smileynet Feb 27, 2023
6964bcd
remove unused imports
smileynet Feb 27, 2023
7ae081d
fix jest tests
smileynet Feb 27, 2023
9e507fc
remove cypress
smileynet Feb 27, 2023
c709a00
config updates
smileynet Mar 4, 2023
f1eaff3
add player healthbar
smileynet Mar 4, 2023
e872e51
Add healthBar to object on player focus.
smileynet Mar 4, 2023
fd11bae
Add healthBar to object on player focus.
smileynet Mar 4, 2023
d54ba9c
Code cleanup - Fix #6 #7
smileynet Mar 4, 2023
20650fe
Code cleanup - Fix #6 #7
smileynet Mar 4, 2023
17ec584
Merge remote-tracking branch 'origin/milestone-5' into milestone-5
smileynet Mar 4, 2023
da1a740
add broken and destroyed cases to feature test
smileynet Mar 4, 2023
58c4f7a
refactor with even more composition
smileynet Mar 5, 2023
99683de
Update tests
smileynet Mar 5, 2023
36fbcbc
refactor event bus with logging
smileynet Mar 5, 2023
d743374
add multi-directional movement
smileynet Mar 5, 2023
9b0e3e3
Added extensive debugging system with toggles.
smileynet Mar 5, 2023
066a353
Fix health constructor
smileynet Mar 6, 2023
65ce756
Fix health constructor
smileynet Mar 6, 2023
f7391ae
fix universe interactable deletion, refactor playerControl and Action…
smileynet Mar 7, 2023
ecba055
fix universe interactable deletion, refactor playerControl and Action…
smileynet Mar 7, 2023
1f68554
fix healthBar movement
smileynet Mar 8, 2023
9ebbae5
Closing milestone 5
smileynet Mar 8, 2023
bcf25ec
Merge branch 'main' into milestone-5
smileynet Mar 8, 2023
6ee9557
build fixes
smileynet Mar 8, 2023
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
Prev Previous commit
Next Next commit
refactor with even more composition
Signed-off-by: Sam Biggins <sambiggins@gmail.com>
  • Loading branch information
smileynet committed Mar 5, 2023
commit 58c4f7ab37141dd392237a5e647a227819cc7995
20 changes: 10 additions & 10 deletions .yarn/releases/yarn-3.4.1.cjs

Large diffs are not rendered by default.

572 changes: 559 additions & 13 deletions README.md

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions src/components/attack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { EventBus } from '@src/systems';
import { IComponent } from '@components/component';

export class Attack implements IComponent {
private _eventBus!: EventBus;

constructor(amount: number) {
this._amount = amount;
}

private _target?: string | undefined;

get target() {
return this._target;
}

private _amount: number;

get amount() {
return this._amount;
}

set amount(amount) {
this._amount = amount;
}

public subscribe(eventBus: EventBus) {
this._eventBus = eventBus;
this._eventBus.subscribe('performAttack', this.performAttack);
this._eventBus.subscribe('focusChanged', this.focusChanged);
}

public destroy() {
this.unsubscribe();
}

private unsubscribe() {
this._eventBus.unsubscribe('performAttack', this.performAttack);
this._eventBus.unsubscribe('focusChanged', this.focusChanged);
}

private performAttack(universeEventBus: EventBus) {
universeEventBus.publish('attackPerformed', {
target: this._target,
damage: this._amount
});
}

private focusChanged(interactableId: string) {
this._target = interactableId;
}
}
11 changes: 11 additions & 0 deletions src/components/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EventBus } from '@src/systems';

export interface ComponentMap {
[componentName: string]: IComponent;
}

export interface IComponent {
subscribe?: (eventBus: EventBus) => void;
update?: () => void;
destroy?: () => void;
}
80 changes: 80 additions & 0 deletions src/components/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { log } from '@src/utilities';
import { IComponent } from './';
import { EventBus } from '@src/systems';

export interface DamageEvent {
target: string;
damage: number;
}

export class Health implements IComponent {
private readonly _maxHealth: number;
private readonly _brokenThreshold: number;
private _eventBus!: EventBus;

constructor(amount: number, maxHealth: number, brokenThreshold: number) {
this._amount = amount;
this._maxHealth = maxHealth;
this._brokenThreshold = brokenThreshold;
}

private _amount: number;

get amount() {
return this._amount;
}

set amount(amount) {
this._amount = amount;
this._eventBus.publish('healthChanged', this._amount);
if (this._amount <= 0) {
this.destroy();
}
if (this._amount < this._brokenThreshold) {
this._isBroken = true;
this._eventBus.publish('broken', this._amount);
log.info(`Object is broken`); // TODO: move logging to subscriber
} else {
this._isBroken = false;
this._eventBus.publish('fixed', this._amount);
log.info(`Object is no longer broken`);
}
}

private _isBroken = false;

get isBroken(): boolean {
return this._isBroken;
}

get maxHealth(): number {
return this._maxHealth;
}

public subscribe(eventBus: EventBus) {
this._eventBus = eventBus;
this._eventBus.subscribe('takeDamage', this.handleTakeDamage);
}

public update() {
// do nothing
}

public destroy() {
this._eventBus.publish('destroyed', this.amount);
log.info(`Object is destroyed`);
this.unsubscribe();
}

public handleTakeDamage = (amount: number) => {
this.takeDamage(amount);
};

public takeDamage(amount: number) {
this.amount = Math.max(0, this.amount - amount);
}

private unsubscribe() {
this._eventBus.unsubscribe('takeDamage', this.handleTakeDamage);
}
}
35 changes: 35 additions & 0 deletions src/components/healthBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IComponent } from './';
import { HealthBarUI } from '@src/ui';
import { EventBus } from '@src/systems';
import Vector2 = Phaser.Math.Vector2;

export class HealthBarComponent implements IComponent {
private _eventBus!: EventBus;

constructor(readonly _healthBar: HealthBarUI) {}

public subscribe(eventBus: EventBus) {
this._eventBus = eventBus;
this._eventBus.subscribe('healthChanged', this.updateHealth);
this._eventBus.subscribe('positionChanged', this.updatePosition);
}

public updateHealth(amount: number): void {
this._healthBar.updateHealth(amount);
}

public updatePosition(position: Vector2): void {
const { x, y } = position;
this._healthBar.updatePosition(x, y);
}

public destroy(): void {
this._healthBar.destroy();
this.unsubscribe();
}

private unsubscribe() {
this._eventBus.unsubscribe('healthChanged', this.updateHealth);
this._eventBus.unsubscribe('positionChanged', this.updatePosition);
}
}
19 changes: 17 additions & 2 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import Thing from './thing';
import { ComponentMap, IComponent } from './component';
import { DamageEvent, Health } from './health';
import { MoveEvent, Movement } from './movement';
import { BaseDamage, Speed, Stat } from './stats';
import { HealthBarComponent } from './healthBar';

export { Thing };
export {
type IComponent,
type ComponentMap,
type DamageEvent,
type MoveEvent,
Movement,
Health,
HealthBarComponent,
Stat,
BaseDamage,
Speed
};
71 changes: 71 additions & 0 deletions src/components/movement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { IComponent } from './';
import { Direction, EventBus } from '@src/systems';

export interface MoveEvent {
interactableId: string;
direction: Direction;
}

export class Movement implements IComponent {
private _direction = Direction.down;
private _velocity = new Phaser.Math.Vector2(0, 0);
private _eventBus!: EventBus;

constructor(private _speed: number) {}

public subscribe(eventBus: EventBus) {
this._eventBus = eventBus;
this._eventBus.subscribe('move', this.handleMove);
this._eventBus.subscribe('stop', this.handleStop);
}

handleStop() {
this._velocity.x = 0;
this._velocity.y = 0;
this._eventBus.publish('spriteAnimationShouldChange', {
velocity: this._velocity,
animationKey: `idle-${this._direction}`,
ignoreIfPlaying: false
});
}

public destroy(): void {
this.unsubscribe();
}

private handleMove(direction: Direction) {
this._direction = direction;
this._velocity.x = this.calcVelocity().x * this._speed;
this._velocity.y = this.calcVelocity().y * this._speed;

this._eventBus.publish('spriteShouldUpdate', {
velocity: this._velocity,
animationKey: `move-${this._direction}`,
ignoreIfPlaying: false
});
}

private calcVelocity() {
const velocity = new Phaser.Math.Vector2(0, 0);
switch (this._direction) {
case 'up':
velocity.y -= 1;
break;
case 'down':
velocity.y += 1;
break;
case 'left':
velocity.x -= 1;
break;
case 'right':
velocity.x += 1;
break;
}
return velocity.normalize();
}

private unsubscribe() {
this._eventBus.unsubscribe('move', this.handleMove);
this._eventBus.unsubscribe('stop', this.handleStop);
}
}
55 changes: 55 additions & 0 deletions src/components/sprite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { IComponent } from './';
import { EventBus } from '@src/systems';

export interface SpriteUpdate {
velocity: Phaser.Math.Vector2;
animationKey: string;
ignoreIfPlaying?: boolean;
}

export class Sprite implements IComponent {
private _eventBus!: EventBus;

constructor(private _sprite: Phaser.Physics.Arcade.Sprite) {}

get sprite() {
return this._sprite;
}

set sprite(sprite: Phaser.Physics.Arcade.Sprite) {
this._sprite = sprite;
}

public subscribe(eventBus: EventBus) {
this._eventBus = eventBus;
this._eventBus.subscribe('spriteShouldUpdate', this.handleSpriteUpdate);
}

public setPlayerSpriteProperties() {
this._sprite.setCollideWorldBounds(true);
this._sprite.setPushable(false);
this._sprite.setImmovable(false);
this._sprite.play('idle-down');
}

public handleSpriteUpdate = (update: SpriteUpdate) => {
this._sprite.play(update.animationKey, update.ignoreIfPlaying);
this._sprite.setVelocity(update.velocity.x, update.velocity.y);
};

public update(): void {
this._sprite.update();
}

public destroy(): void {
this._sprite.destroy();
this.unsubscribe();
}

private unsubscribe() {
this._eventBus.unsubscribe(
'spriteShouldUpdate',
this.handleSpriteUpdate
);
}
}
33 changes: 33 additions & 0 deletions src/components/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IComponent } from './';

export class Stat implements IComponent {
constructor(private _amount: number) {}

get amount() {
return this._amount;
}

set amount(amount) {
this._amount = amount;
}

public update() {
// do nothing
}

public destroy() {
// do nothing
}
}

export class Speed extends Stat {
constructor(amount: number) {
super(amount);
}
}

export class BaseDamage extends Stat {
constructor(amount: number) {
super(amount);
}
}
Loading