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

Nightmare #1

Merged
merged 5 commits into from Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 46 additions & 16 deletions app.ts
@@ -1,6 +1,5 @@
var RouteState = require('route-state');
var handleError = require('handle-error-web');
var update = require('./flows/update');
var render = require('./renderers/render');
var Probable = require('probable').createProbable;
var findWhere = require('lodash.findwhere');
Expand All @@ -9,7 +8,11 @@ var generateSouls = require('./generators/generate-souls');
var seedrandom = require('seedrandom');
var { version } = require('./package.json');

import { GameState, Command } from './types';
import { update } from './flows/update';
import { GameState, TargetTree, UpdateResult, Soul } from './types';
import { createTargetTree } from './target-tree';
import { init } from './flows/init';
import { createSoulTracker } from './ops/soul-tracker';

var randomid = require('@jimkang/randomid')();

Expand All @@ -21,11 +24,16 @@ var theGameState: GameState = {
},
gridsInit: false,
uiOn: false,
actionChoices: [],
cmdChoices: [],
cmdQueue: [],
lastClickedThingIds: [],
gameWon: false
gameWon: false,
soulTracker: createSoulTracker(),
turn: 0
};

var targetTree: TargetTree = createTargetTree();

var routeState = RouteState({
followRoute,
windowObject: window
Expand All @@ -45,32 +53,53 @@ function followRoute({ seed }) {
var random = seedrandom(seed);
var probable = Probable({ random });
theGameState.grids = generateGrids({ probable, random });
theGameState.souls = generateSouls({ random, grids: theGameState.grids });
theGameState.player = findWhere(theGameState.souls, { id: 'player' });
theGameState.soulTracker.addSouls(
theGameState.grids,
targetTree,
generateSouls({ random, grids: theGameState.grids })
);
theGameState.player = findWhere(theGameState.soulTracker.getSouls(), {
id: 'player'
});
init(theGameState, targetTree);
theGameState.soulTracker.incrementActorIndex();

advance({ gameState: theGameState });

function advance({
gameState,
recentClickX,
recentClickY,
commands
recentClickY
}: {
gameState: GameState;
recentClickX?: number;
recentClickY?: number;
commands?: Array<Command>;
}) {
incrementTurn(gameState);
if (gameState.allowAdvance) {
resetGameStateForNewTurn(gameState);
update({
let actor: Soul = gameState.soulTracker.getActingSoul();
let {
shouldAdvanceToNextSoul,
renderShouldWaitToAdvanceToNextUpdate
}: UpdateResult = update({
actor,
gameState,
recentClickX,
recentClickY,
commands,
probable
probable,
targetTree
});
console.log(actor.id, 'update done.');
if (shouldAdvanceToNextSoul) {
gameState.soulTracker.incrementActorIndex();
}

render({
gameState,
onMessageDismiss,
onAdvance: advance,
shouldWaitForInteraction: renderShouldWaitToAdvanceToNextUpdate
});
render({ gameState, onMessageDismiss, onAdvance: advance });
}
}
}
Expand All @@ -80,8 +109,9 @@ function onMessageDismiss() {
theGameState.displayMessage = '';
}

function resetGameStateForNewTurn(gameState: GameState) {
gameState.actionChoices.length = 0;
function incrementTurn(gameState: GameState) {
gameState.turn += 1;
console.log('Advancing to turn', gameState.turn);
}

function reportTopLevelError(msg, url, lineNo, columnNo, error) {
Expand Down
50 changes: 43 additions & 7 deletions defs/soul-defs.ts
@@ -1,7 +1,8 @@
import { getCanMoveHereFn } from './can-move-fns';
import { randomMove } from './moves';
import { spriteSize } from '../sizes';
import { SoulDef } from '../types';
import { SoulDef, CommandDef } from '../types';
import { cmdDefsById } from '../flows/commands/commands';

// New entry macro: @e
var defList: Array<SoulDef> = [
Expand All @@ -19,6 +20,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing
},
{
type: 'basicShell',
Expand Down Expand Up @@ -91,6 +93,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing
},
{
type: 'cat',
Expand All @@ -106,6 +109,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing
},
{
type: 'cloud',
Expand All @@ -122,6 +126,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-air'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing
},
{
type: 'corn',
Expand All @@ -137,6 +142,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing
},
// TODO: Get sleep, other animation in.
{
Expand All @@ -152,7 +158,7 @@ var defList: Array<SoulDef> = [
hitRadius: spriteSize * 0.75
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
hitDice: '1d8'
},
{
type: 'cup',
Expand Down Expand Up @@ -182,6 +188,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getMeanGuyInteractionsWithThing
},
{
type: 'fancyShell',
Expand Down Expand Up @@ -337,6 +344,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing,
itemRole: { itemPositioningStyle: 'shell' }
},
{
Expand Down Expand Up @@ -367,6 +375,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing
},
{
type: 'tearDrop',
Expand All @@ -382,6 +391,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing,
facingsAllowed: [[1, 0], [-1, 0]],
itemRole: { itemPositioningStyle: 'shell' }
},
Expand Down Expand Up @@ -413,6 +423,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-figures'],
hitDice: '1d8',
getInteractionsWithThing: getMeanGuyInteractionsWithThing
},
{
type: 'trilby',
Expand Down Expand Up @@ -453,7 +464,7 @@ var defList: Array<SoulDef> = [
hitRadius: spriteSize * 0.75
},
facingsAllowed: [[1, 0]],
allowedGrids: ['grid-figures'],
allowedGrids: ['grid-figures']
},
{
type: 'worm',
Expand All @@ -469,6 +480,7 @@ var defList: Array<SoulDef> = [
},
allowedGrids: ['grid-air'],
hitDice: '1d8',
getInteractionsWithThing: getGuyInteractionsWithThing,
itemRole: { itemPositioningStyle: 'shell' }
},
{
Expand All @@ -495,17 +507,41 @@ function addToDict(def: SoulDef) {
soulDefs[def.type] = def;
}

function getPlayerInteractionsWithThing(thing): Array<string> {
function getPlayerInteractionsWithThing(thing): Array<CommandDef> {
if (thing.type && thing.type === 'player') {
return ['blast'];
return [cmdDefsById.blast];
}
if (thing.categories) {
if (thing.categories.includes('item')) {
return ['take'];
return [cmdDefsById.take];
}
if (thing.categories.includes('guy')) {
return ['bonk'];
return [cmdDefsById.bonk];
}
}
return [];
}

function getGuyInteractionsWithThing(thing): Array<CommandDef> {
if (thing.categories) {
if (thing.categories.includes('item')) {
return [cmdDefsById.take];
}
if (thing.categories.includes('guy')) {
return [cmdDefsById.bonk];
}
}
return [];
}

function getMeanGuyInteractionsWithThing(thing): Array<CommandDef> {
if (thing.categories) {
if (thing.categories.includes('item')) {
return [cmdDefsById.take];
}
if (thing.categories.includes('guy')) {
return [cmdDefsById.bonk, cmdDefsById.take];
}
}
return [cmdDefsById.smallBlast];
}
37 changes: 17 additions & 20 deletions flows/commands/blast-command.ts
@@ -1,37 +1,39 @@
var callNextTick = require('call-next-tick');
import { Box, Done, CmdParams, Filter, Soul } from '../../types';

export function blastCmd(
{ gameState, targetTree, removeSouls }: CmdParams,
doneWithAnimationCompletionCallback: Done
) {
export function blastCmd({ gameState, targetTree, cmd }: CmdParams) {
// This probably should be based on something other than the sprite size.
var blastBox: Box = {
minX: gameState.player.x - 3 * gameState.player.sprite.width,
maxX: gameState.player.x + 3 * gameState.player.sprite.width,
minY: gameState.player.y - 3 * gameState.player.sprite.height,
maxY: gameState.player.y + 3 * gameState.player.sprite.height
minX: cmd.actor.x - cmd.params.blastSize * cmd.actor.sprite.width,
maxX: cmd.actor.x + cmd.params.blastSize * cmd.actor.sprite.width,
minY: cmd.actor.y - cmd.params.blastSize * cmd.actor.sprite.height,
maxY: cmd.actor.y + cmd.params.blastSize * cmd.actor.sprite.height
};
var thingsToRemove = getTargetsInBox({
targetTree,
filter: isBlastable,
box: blastBox
});
console.log('blasting:', thingsToRemove);
gameState.animations.push({
type: 'blast',
custom: {
cx: gameState.player.x,
cy: gameState.player.y,
r: 3 * gameState.player.sprite.width,
color: 'yellow'
cx: cmd.actor.x,
cy: cmd.actor.y,
r: cmd.params.blastSize * cmd.actor.sprite.width,
color: cmd.params.color
},
duration: 1000,
postAnimationGameStateUpdater: updateStatePostBlastAnimation
});

function updateStatePostBlastAnimation(notifyAnimationDone: Done) {
removeSouls(gameState, thingsToRemove);
doneWithAnimationCompletionCallback(null, notifyAnimationDone);
gameState.soulTracker.removeSouls(targetTree, thingsToRemove);
callNextTick(notifyAnimationDone);
}

function isBlastable(thing) {
// For now, only blast other souls.
return thing.id !== cmd.actor.id;
}
}

Expand All @@ -52,8 +54,3 @@ function getTargetsInBox({
}
return targets;
}

function isBlastable(thing) {
// For now, only blast other souls.
return thing.type && thing.type !== 'player';
}
29 changes: 12 additions & 17 deletions flows/commands/bonk-command.ts
@@ -1,31 +1,26 @@
import { Done, CmdParams, Soul } from '../../types';
import { getLastClickedSoul } from '../../tasks/game-state-ops';
import { CmdParams, Soul, Done } from '../../types';
var callNextTick = require('call-next-tick');

// TODO: Make this work symmetrically.
export function bonkCmd(
{ gameState, removeSouls }: CmdParams,
doneWithAnimationCompletionCallback: Done
) {
var soul: Soul = getLastClickedSoul(gameState);
console.log('bonking:', soul.id);
export function bonkCmd({ gameState, targetTree, cmd }: CmdParams) {
var target: Soul = cmd.targets[0];
gameState.animations.push({
type: 'bonk',
custom: {
bonkerSoul: gameState.player,
bonkeeSoul: soul
bonkerSoul: cmd.actor,
bonkeeSoul: target
},
duration: 900,
postAnimationGameStateUpdater: updateStatePostBonkAnimation
});

function updateStatePostBonkAnimation(notifyAnimationDone: Done) {
if (!isNaN(soul.hp)) {
soul.hp -= 3;
console.log('New hp for', soul.id, soul.hp, '/', soul.maxHP);
if (!isNaN(target.hp)) {
target.hp -= 3;
console.log('New hp for', target.id, target.hp, '/', target.maxHP);
}
if (soul.hp < 1) {
removeSouls(gameState, [soul]);
if (target.hp < 1) {
gameState.soulTracker.removeSouls(targetTree, [target]);
}
doneWithAnimationCompletionCallback(null, notifyAnimationDone);
callNextTick(notifyAnimationDone);
}
}