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

Modularizing and Improving the Rogue Game #279

Merged
merged 16 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adding item names to report on game end.
  • Loading branch information
benrick committed Nov 10, 2018
commit 1be68292f7396e00c7cad2f870e2ed67bcecf873
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DevChatter.Bot.Modules.WastefulGame.Hubs.Dtos
{
public class HeldItemDto
{
public string Name { get; set; }
public int Uses { get; set; }
}
}
6 changes: 4 additions & 2 deletions src/DevChatter.Bot.Modules.WastefulGame/Hubs/WastefulHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using System.Linq;
using DevChatter.Bot.Modules.WastefulGame.Hubs.Dtos;

namespace DevChatter.Bot.Modules.WastefulGame.Hubs
{
Expand All @@ -21,9 +22,10 @@ public WastefulHub(IList<IChatClient> chatClients,
}

public void GameEnd(int points, string playerName,
string userId, string endType, int levelNumber)
string userId, string endType, int levelNumber, List<HeldItemDto> items)
{
string message = $"{playerName} has {endType} on level {levelNumber} with {points} points.";
string itemDisplayText = items.Any() ? string.Join(", ", items.Select(x => x.Name)) : "nothing";
string message = $"{playerName} has {endType} on level {levelNumber} with {points} points while holding {itemDisplayText}.";
_chatClients.ForEach(c => c.SendMessage(message));

var gameEndRecord = new GameEndRecord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const EffectItemMessages = Object.freeze({

export class EffectItem extends Item {
/**
* @param {string} name
* @param {Wasteful} game
* @param {Sprite|null} sprite
* @param {ItemType} type
Expand All @@ -19,8 +20,8 @@ export class EffectItem extends Item {
* @param {number} uses
* @param {Array<{ItemEffectType: number}>} effectPointMap
*/
constructor(game, sprite, type, pickupType, effectTypes, uses, effectPointMap) {
super(game, sprite, type, pickupType, effectTypes, uses);
constructor(name, game, sprite, type, pickupType, effectTypes, uses, effectPointMap) {
super(name, game, sprite, type, pickupType, effectTypes, uses);

this._effectPointMap = effectPointMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export class ExitItem extends EffectItem {
* @param {Array<{ItemEffectType: number}>} effectPointMap
*/
constructor(game, sprite, type, pickupType, effectTypes, uses, effectPointMap) {
super(game, sprite, type, pickupType, effectTypes, uses, effectPointMap);
super('exit',game, sprite, type, pickupType, effectTypes, uses, effectPointMap);

const location = this.game.grid.lowerRightCorner;
this.setLocation(location.x - 1, Math.ceil(location.y / 2))
this.setLocation(location.x - 1, Math.ceil(location.y / 2));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ export const ItemMessages = Object.freeze({

export class Item extends Entity {
/**
* @param {string} name
* @param {Wasteful} game
* @param {Sprite|null} sprite
* @param {ItemType} type
* @param {ItemPickupType} pickupType
* @param {ItemEffectType|Array<ItemEffectType>|null} effectTypes
* @param {number} uses
*/
constructor(game, sprite, type, pickupType, effectTypes, uses = 1) {
constructor(name, game, sprite, type, pickupType, effectTypes, uses = 1) {
super(game, sprite, true);

this._name = name;
this._type = type;
this._pickupType = pickupType;

Expand Down Expand Up @@ -138,6 +140,14 @@ export class Item extends Entity {

}

/**
* @public
* @returns {string}
*/
get name() {
return this._name;
}

/**
* @public
* @returns {number}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ export class ItemBuilder {
*/
_createItemOfType(itemType) {
const items = [
new EffectItem(this._game, new Sprite('/images/ZedChatter/Taco-0.png', 1, 1, 1), ItemType.CONSUMABLE, ItemPickupType.INSTANT, [ItemEffectType.HEALTH, ItemEffectType.POINTS], 1, [
new EffectItem('Taco', this._game, new Sprite('/images/ZedChatter/Taco-0.png', 1, 1, 1), ItemType.CONSUMABLE, ItemPickupType.INSTANT, [ItemEffectType.HEALTH, ItemEffectType.POINTS], 1, [
{ [ItemEffectType.HEALTH]: 2 },
{ [ItemEffectType.POINTS]: 5 }
]),
new EffectItem(this._game, new Sprite('/images/ZedChatter/Pizza-0.png', 1, 1, 1), ItemType.CONSUMABLE, ItemPickupType.INSTANT, ItemEffectType.HEALTH, 1, [{ [ItemEffectType.HEALTH]: 3 }]),
new EffectItem(this._game, new Sprite('/images/ZedChatter/BlackCan-0.png', 1, 1, 1), ItemType.CONSUMABLE, ItemPickupType.INSTANT, [ItemEffectType.HEALTH, ItemEffectType.POINTS], 1, [
new EffectItem('Pizza', this._game, new Sprite('/images/ZedChatter/Pizza-0.png', 1, 1, 1), ItemType.CONSUMABLE, ItemPickupType.INSTANT, ItemEffectType.HEALTH, 1, [{ [ItemEffectType.HEALTH]: 3 }]),
new EffectItem('BlackCan', this._game, new Sprite('/images/ZedChatter/BlackCan-0.png', 1, 1, 1), ItemType.CONSUMABLE, ItemPickupType.INSTANT, [ItemEffectType.HEALTH, ItemEffectType.POINTS], 1, [
{ [ItemEffectType.HEALTH]: 1 },
{ [ItemEffectType.POINTS]: 15 }
]),
new Item(this._game, new Sprite('/images/ZedChatter/BaseballBat-1.png', 1, 1, 1), ItemType.WEAPON, ItemPickupType.INVENTORY, null, 1),
new Item(this._game, new Sprite('/images/ZedChatter/Axe-1.png', 1, 1, 1), ItemType.WEAPON, ItemPickupType.INVENTORY, null, 2),
new Item(this._game, new Sprite('/images/ZedChatter/Sword-1.png', 1, 1, 1), ItemType.WEAPON, ItemPickupType.INVENTORY, null, 3),
new Item('Bat', this._game, new Sprite('/images/ZedChatter/BaseballBat-1.png', 1, 1, 1), ItemType.WEAPON, ItemPickupType.INVENTORY, null, 1),
new Item('Axe', this._game, new Sprite('/images/ZedChatter/Axe-1.png', 1, 1, 1), ItemType.WEAPON, ItemPickupType.INVENTORY, null, 2),
new Item('Sword', this._game, new Sprite('/images/ZedChatter/Sword-1.png', 1, 1, 1), ItemType.WEAPON, ItemPickupType.INVENTORY, null, 3),
];
return [this._pickRandom(items.filter(item => item.type === itemType))];
}
Expand Down
21 changes: 21 additions & 0 deletions src/DevChatter.Bot.Web/wwwroot/js/wasteful-game/metadata.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const wastefulInfoWidth = 252;
const tileSize = 42;
const endTypeDied = 'died';
const endTypeEscaped = 'escaped';

export class MetaData {

Expand All @@ -19,3 +21,22 @@ export class MetaData {
return tileSize;
}
}

export class EndTypes {

/**
* @public
* @returns {string}
*/
static get died() {
return endTypeDied;
}

/**
* @public
* @returns {string}
*/
static get escaped() {
return endTypeEscaped;
}
}
19 changes: 15 additions & 4 deletions src/DevChatter.Bot.Web/wwwroot/js/wasteful-game/wasteful.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Info } from '/js/wasteful-game/info.js';
import { Grid } from '/js/wasteful-game/grid.js';
import { Background } from '/js/wasteful-game/background.js';
import { Level } from '/js/wasteful-game/level.js';
import { MetaData } from '/js/wasteful-game/metadata.js';
import { MetaData, EndTypes } from '/js/wasteful-game/metadata.js';
import { Player } from '/js/wasteful-game/entity/player.js';
import { MovableComponent } from '/js/wasteful-game/entity/components/movableComponent.js';
import { AttackableComponent } from '/js/wasteful-game/entity/components/attackableComponent.js';
Expand All @@ -24,6 +24,7 @@ export class Wasteful {
this._context = canvas.getContext('2d');
this._isRunning = false;
this._isGameOver = false;
this._endType = '';
this._lastMouseTarget = null;

const url = new URL(window.location.href);
Expand Down Expand Up @@ -103,7 +104,9 @@ export class Wasteful {

if (this._player.getComponent(AttackableComponent).isDead) {
this._isGameOver = true;
this._endType = EndTypes.died;
}
// TODO: Add an escape check here.
}

/**
Expand Down Expand Up @@ -145,12 +148,20 @@ export class Wasteful {
document.removeEventListener('mousedown', this._mouseDownHandle);
document.removeEventListener('keydown', this._keyDownHandle);
window.cancelAnimationFrame(this._animationHandle);

// TODO: Organize data better, so it's not coming from separate objects.
let heldItems = this.player.inventory.items.map(item => ({
name: item.name,
uses: item.remainingUses
}));

this._hub.invoke('GameEnd', this.player.points, this._userInfo.displayName, this._userInfo.userId, this._endType, this.level.levelNumber, heldItems)
.catch(err => console.error(err.toString()));

this._isRunning = false;
this._isGameOver = false;
this._clearCanvas();

// TODO: Organize data better, so it's not coming from separate objects.
this._hub.invoke('GameEnd', this.player.points, this._userInfo.displayName, this._userInfo.userId, 'died', this.level.levelNumber).catch(err => console.error(err.toString()));
this._endType = '';
}

/**
Expand Down