Created
June 28, 2018 15:29
-
-
Save jkrasnay/e741b59b8740911dda2de9331c24d7fe to your computer and use it in GitHub Desktop.
Tiny move-based game engine in Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var name = "Bar!"; | |
var dirs = { | |
n: [0,1], | |
s: [0,-1], | |
e: [1,0], | |
w: [-1,0]}; | |
function vadd(v1, v2) { | |
return v1.map((x,i) => x + v2[i]); | |
} | |
function dest(pos, dir) { | |
return vadd(pos, dirs[dir]); | |
} | |
var gs = { | |
player: { | |
pos: [0,0] | |
}, | |
nodes: { | |
"0,1": { | |
description: "A big room with lots of windows", | |
items: [] | |
} | |
}, | |
edges: { | |
"0,1,0,2": { | |
f: wall | |
} | |
} | |
}; | |
function node_desc(gs, nodev) { | |
let node = gs.nodes[nodev.toString()]; | |
if (node && node.description) { | |
return node.description; | |
} else { | |
return "Node " + nodev; | |
} | |
} | |
function wall(gs, edgev) { | |
return "You can't go there"; | |
} | |
function hall(gs, edgev) { | |
// edge is a vector like this [[0,1],[0,2]] | |
// update player position be the other position in the edge | |
let [p1,p2] = edgev; | |
if (gs.player.pos.toString() == p1) { | |
gs.player.pos = p2; | |
} else { | |
gs.player.pos = p1; | |
} | |
return "You are now in " + node_desc(gs, gs.player.pos); | |
} | |
function move(gs, dir) { | |
let d = dest(gs.player.pos, dir), | |
edgev = [gs.player.pos, d].sort(), | |
edge = gs.edges[edgev.toString()]; | |
if (edge && edge.f) { | |
return edge.f(gs, edgev); | |
} else { | |
return hall(gs, edgev); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment