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
ractor actions
  • Loading branch information
smileynet committed Feb 26, 2023
commit d3693428afbe22b44096ed6edab8c43ff00a6e04
13 changes: 3 additions & 10 deletions src/components/thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ export default class Thing {
this.moveable = moveable;
}

takeDamage(damage = 1) {
if (this.health - damage >= 0) {
this._health -= damage;
log.debug(
`${this.id} took ${damage} damage, health is now ${this.health}`
);
} else {
this._health = 0;
log.debug(this.id + ' out of health, ready for destruction');
}
set health(health: number) {
this._health = health;
log.debug(`${this.id} health: ${this._health}`);
}

get health() {
Expand Down
72 changes: 5 additions & 67 deletions src/entities/actor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Directions } from '../systems';
import { log } from '../utilities';
import Interactable from './interactable';

export default class Actor extends Interactable {
protected focus?: Interactable;
protected speed = 100;
protected direction = 'down';
protected damage = 1;

constructor(id: string) {
super(id);
Expand All @@ -27,6 +25,10 @@ export default class Actor extends Interactable {
return this.focus;
}

getDamage() {
return this.damage;
}

update() {
if (this.sprite) {
if (this.thing.health < 1) {
Expand All @@ -51,68 +53,4 @@ export default class Actor extends Interactable {
clearFocus() {
this.focus = undefined;
}

attack() {
if (this.focus) {
this.focus.thing.takeDamage(1);
this.sprite?.play(`action-${this.direction}`);
} else {
log.debug('Nothing to attack');
}
}

move(direction: Directions) {
if (this.sprite) {
const normalizedVelocity = this.calcVelocity(direction);
this.sprite.setVelocity(
normalizedVelocity.x * this.speed,
normalizedVelocity.y * this.speed
);
this.sprite.play(`walk-${direction}`, true);
this.direction = direction;
this.focus = undefined;
}
}

calcVelocity(direction: string) {
const velocity = new Phaser.Math.Vector2(0, 0);
switch (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();
}

stop() {
if (this.sprite) {
this.sprite.setVelocity(0, 0);
this.sprite.play(`idle-${this.direction}`, true);
}
}

setSpeed(speed: number) {
this.speed = speed;
}

getSpeed() {
return this.speed;
}

setDirection(direction: string) {
this.direction = direction;
}

getDirection() {
return this.direction;
}
}
32 changes: 32 additions & 0 deletions src/entities/interactable.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import Thing from '../components/thing';
import { Directions } from '../systems';
import { log } from '../utilities';

export default class Interactable {
public thing: Thing;
public sprite?: Phaser.Physics.Arcade.Sprite;
protected speed = 100;
protected direction = Directions.down;

constructor(id: string) {
this.thing = new Thing(id);
}

setSpeed(speed: number) {
this.speed = speed;
}

getSpeed(): number {
return this.speed;
}

setSprite(sprite: Phaser.Physics.Arcade.Sprite) {
this.sprite = sprite;
}
Expand All @@ -16,6 +28,26 @@ export default class Interactable {
this.sprite?.destroy();
}

get health() {
return this.thing.health;
}

set health(health: number) {
this.thing.health = health;
}

get id() {
return this.thing.id;
}

setDirection(direction: string) {
this.direction = direction;
}

getDirection() {
return this.direction;
}

update() {
if (this.sprite) {
if (this.thing.health < 1) {
Expand Down
2 changes: 2 additions & 0 deletions src/scenes/gameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class GameScene extends Phaser.Scene {

this.initObjects();
log.debug('game scene created');
this.scene.launch('UI');
log.debug('ui scene launched');
}

private initObjects() {
Expand Down
29 changes: 29 additions & 0 deletions src/scenes/uiScene.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const sceneConfig: Phaser.Types.Scenes.SettingsConfig = {
active: false,
visible: false,
key: 'UI'
};

export class UIScene extends Phaser.Scene {
constructor() {
super(sceneConfig);
}

create() {
let playerHealth = this.add.text(-10, -10, 'Player: 3');
let benchHealth = this.add.text(-10, -10, 'Bench: 3');

let ourGame = this.scene.get('Game');

// Listen for events from it
ourGame.events.on(
'addScore',
function () {
this.score += 10;

info.setText('Score: ' + this.score);
},
this
);
}
}
68 changes: 63 additions & 5 deletions src/systems/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,78 @@ export class Action {
static performAction(action: Actions, actor: Actor) {
switch (action) {
case Actions.attack:
actor.attack();
this.attack(actor);
break;
case Actions.moveUp:
actor.move(Directions.up);
this.move(actor, Directions.up);
break;
case Actions.moveDown:
actor.move(Directions.down);
this.move(actor, Directions.down);
break;
case Actions.moveLeft:
actor.move(Directions.left);
this.move(actor, Directions.left);
break;
case Actions.moveRight:
actor.move(Directions.right);
this.move(actor, Directions.right);
break;
}
}

static takeDamage(interactable: Interactable, damage = 1) {
interactable.health -= damage;
log.info(
`${interactable.id} took ${damage} damage, health is now ${interactable.health}`
);
}

static attack(attacker: Actor) {
const focusInteractable = attacker.getFocus();
if (focusInteractable) {
this.takeDamage(focusInteractable, attacker.getDamage());
attacker.sprite?.play(`action-${attacker.getDirection()}`);
} else {
log.debug(`${attacker.id} has no focus to attack`);
}
}

static move(interactable: Interactable, direction: Directions) {
if (interactable.sprite) {
const normalizedVelocity = this.calcVelocity(direction);
interactable.sprite.setVelocity(
normalizedVelocity.x * interactable.getSpeed(),
normalizedVelocity.y * interactable.getSpeed()
);
interactable.setDirection(direction);
interactable.sprite.play(`walk-${direction}`, true);
if (interactable instanceof Actor) {
interactable.clearFocus();
}
}
}

static calcVelocity(direction: string) {
const velocity = new Phaser.Math.Vector2(0, 0);
switch (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();
}

stop(interactable: Interactable) {
if (interactable.sprite) {
interactable.sprite.setVelocity(0, 0);
interactable.sprite.play(`idle-${this.direction}`, true);
}
}
}