Skip to content

Commit

Permalink
Refactor map, dungeon, rooms, monsters
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarrough committed Jul 23, 2023
1 parent 1c51aab commit 2b92453
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 290 deletions.
12 changes: 8 additions & 4 deletions src/content/dungeon-encounters.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Dungeon from '../game/dungeon.js'
import {MonsterRoom, Monster} from '../game/dungeon-rooms.js'
import {random} from '../game/utils.js'
import {MonsterRoom} from '../game/rooms.js'
import {Monster} from '../game/monster.js'
import {random} from '../utils.js'

// A dungeon encounter is the combination of a Room and Monster(s).

// Hello. With the imported functions above you can create a dungeon with different rooms and monsters.
// Should be able to support even more monsters (4-5)
// This is the dungeon currently used.

// This is the efault dungeon currently used.
export const dungeonWithMap = () => {
return Dungeon({
width: 6,
Expand All @@ -18,7 +22,7 @@ export const dungeonWithMap = () => {
// This is the dungeon used in tests. Don't change it without running tests.
export const createTestDungeon = () => {
const dungeon = Dungeon({width: 1, height: 3})
// The tests rely on the first room having a single monster, second two monsters.
// The tests rely on the first room having a single monster, second room two monsters.
const intents = [{block: 7}, {damage: 10}, {damage: 8}, {}, {damage: 14}]
dungeon.graph[1][0].room = MonsterRoom(Monster({hp: 42, intents}))
dungeon.graph[2][0].room = MonsterRoom(Monster({hp: 24, intents}), Monster({hp: 13, intents}))
Expand Down
8 changes: 4 additions & 4 deletions src/game/action-manager.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import Queue from './queue.js'
import actions from './actions.js'
import Queue from '../utils.js'

/**
* @typedef {Object} FutureAction
* @typedef {object} FutureAction
* @prop {string} type - the name of a function in actions.js
* @prop {any} [any] - arguments are passed to the action
*/

/**
* @typedef {Object} PastAction
* @typedef {object} PastAction
* @prop {string} type - the name of a function in actions.js
* @prop {import('./actions.js').State} state
*/

/**
* The action manager makes use of queues to keep track of future and past actions in the game state + undo.
* @param {Object} props
* @param {object} props
* @prop {boolean} props.debug - whether to log actions to the console
*/
export default function ActionManager(props) {
Expand Down
40 changes: 20 additions & 20 deletions src/game/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,31 @@ import {isDungeonCompleted} from './utils-state.js'
// of the game state that should not be there.
// setAutoFreeze(false)

// In Slay the Web, we have one big object with game state.
// Whenever we want to change something, call an "action" from this file.
// Each action takes two arguments: 1) the current state, 2) an object of arguments.
/**
We don't mutate it directly, instead we run "action functions" on it.
An action function takes two arguments: 1) the current state, 2) an object of arguments.
* @template T
* @callback ActionFn
* @param {State} state the current state
* @param {T} [props] an object of arguments
* @returns {State} a new state object
*/

/**
* The big "game state" object
* @typedef {object} State
* @prop {Number} createdAt
* @prop {Number} endedAt
* @prop {Boolean} won
* @prop {number} turn
* @prop {Array} deck
* @prop {Array} drawPile
* @prop {Array} hand
* @prop {Array} discardPile
* @prop {Array} exhaustPile
* @prop {Player} player
* @prop {Object} dungeon
* @prop {Number} createdAt
* @prop {Number} endedAt
* @prop {Boolean} won
* @prop {import('./dungeon.js').Dungeon} [dungeon]
*/

/**
Expand All @@ -38,15 +46,7 @@ import {isDungeonCompleted} from './utils-state.js'
* @prop {number} currentHealth
* @prop {number} maxHealth
* @prop {number} block
* @prop {Object} powers
*/

/**
* @template T
* @callback ActionFn
* @param {State} state - first argument must be the state object
* @param {T} [props]
* @returns {State} returns a new state object
* @prop {object} powers
*/

/**
Expand All @@ -69,7 +69,7 @@ function createNewState() {
block: 0,
powers: {},
},
dungeon: {},
dungeon: undefined,
createdAt: new Date().getTime(),
endedAt: undefined,
won: false,
Expand Down Expand Up @@ -555,7 +555,7 @@ function addCardToDeck(state, {card}) {

/**
* Records a move on the dungeon map.
* @type {ActionFn<{move: {x: number, y: number}}}
* @type {ActionFn<{move: {x: number, y: number}}>}
*/
function move(state, {move}) {
let nextState = endEncounter(state)
Expand All @@ -577,7 +577,7 @@ function move(state, {move}) {

/**
* Deals damage to a target equal to the current player's block.
* @type {ActionFn<{target: CardTargets}}
* @type {ActionFn<{target: CardTargets}>}
*/
function dealDamageEqualToBlock(state, {target}) {
if (state.player.block) {
Expand Down Expand Up @@ -632,7 +632,7 @@ function setPower(state, {target, power, amount}) {

/**
* Stores a campfire choice on the room (useful for stats and whatnot)
* @type {ActionFn<{room: import('./dungeon-rooms.js').Room, choice: string, reward: import('./cards.js').CARD}>}
* @type {ActionFn<{room: import('./rooms.js').Room, choice: string, reward: import('./cards.js').CARD}>}
*/
function makeCampfireChoice(state, {choice, reward}) {
return produce(state, (draft) => {
Expand Down
4 changes: 2 additions & 2 deletions src/game/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export const CardTargets = {
*/

/**
* @typedef {Object} CardAction - allows the card to run all defined actions
* @typedef {object} CardAction - allows the card to run all defined actions
* @prop {string} type - name of the action to call. See game/actions.js
* @prop {Object} [parameter] - props to pass to the action
* @prop {object} [parameter] - props to pass to the action
* @prop {Array<{type: string}>} [conditions] - list of conditions
*/

Expand Down
2 changes: 1 addition & 1 deletion src/game/conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {getPlayerHealthPercentage} from './utils-state.js'

/**
* Conditions decide whether a card can be played or not.
* @typedef {Object} Condition — all other props will be passed to the condition as well
* @typedef {object} Condition — all other props will be passed to the condition as well
* @prop {string} type
* @prop {string=} cardType
* @prop {number=} percentage
Expand Down
111 changes: 0 additions & 111 deletions src/game/dungeon-rooms.js

This file was deleted.

Loading

0 comments on commit 2b92453

Please sign in to comment.