Roguelike development in JavaScript

Roguelike development in JavaScript

Is it scary?

(Hint: no)

This talk

Contents

  1. Using JS for rapid prototyping/development
  2. Common myths and FUDs busted
  3. Typical JS scenarios

JavaScript is cool, because…

JavaScript is expressive

JavaScript is expressive

Example: fiery explosion

var nearby = map.getNearby(x, y, r); var damaged = nearby.filter(function(entity) { if (!entity.damage) { return false; } /* not damagable */ entity.damage(5); Math.random() > 0.5 && (entity.catchFire || entity.burn)(); return true; }); alert(damaged.length + " entities were damaged");

But JavaScript is slow?

But JavaScript is broken?

But JavaScript has this weird OOP?

But JavaScript needs cross-browser thingiez?

But JavaScript is all async and I am afraid of synchronization?

JS Scenarios: main loop

while (true) { processInput(); update(); render(); }

JS Scenarios: main loop

window.addEventListener("keypress", function() { processInput(); update(); render(); });

JS Scenarios: input APIs

JS Scenarios: KB input

var KBHandler = { handleEvent: function(e) { var what = null; if (e.type == "keydown") { what = e.keyCode; } /* hardware key code */ if (e.type == "keypress") { what = String.fromCharCode(e.charCode); } /* unicode code point */ switch (what) { case 37: /* move left */ break; case "d": /* drop stuff */ break; } } } window.addEventListener("keydown", KBHandler); window.addEventListener("keypress", KBHandler);

JS Scenarios: output APIs

JS Scenarios: 2D output

<canvas width="640" height="480"> var canvas = document.querySelector("canvas"); var ctx = canvas.getContext("2d"); ctx.font = "20px monospace"; ctx.fillStyle = "blue"; ctx.fillText(50, 50, "@"); ctx.beginPath(); ctx.moveTo(100, 50); ctx.lineTo(50, 100); ctx.stroke(); ctx.drawImage(imageOrCanvas, 100, 100);

JS Scenarios: misc APIs

You see a scroll labeled "Questions?"