git @ Cat's Eye Technologies Jaccia / rel_1_1_2014_0109 demo / jaccia.html
rel_1_1_2014_0109

Tree @rel_1_1_2014_0109 (Download .tar.gz)

jaccia.html @rel_1_1_2014_0109raw · history · blame

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>Jaccia Demo</title>
</head>
<body>

<h1>Jaccia Demo</h1>

<button id="load">Load</button>
<button id="edit">Edit</button>
<button id="start">Start</button>
<button id="stop">Stop</button>
<button id="step">Step</button>
Speed: <input id="speed" type="range" min="0" max="200" value="0" />

<textarea id="input" rows="10">
########S##
#:::::::::#
#:###:###:#
#:#:#:::#:#
#:#:#:#:###
#:::#:#:#:#
#####:#:#:#
#:#:::#:::#
#:#:###:###
#:::#:::::#
#########F#
</textarea>

<div id="display_container">
  Depict with:
  <select id="select_depiction">
    <option>text</option>
    <option>canvas</option>
  </select>
  
  <pre id="display_text"></pre>
  <canvas id="display_canvas" width="400" height="250">
    There would be an HTML5 canvas here, but your browser isn't displaying it.
  </canvas>
</div>

</body>
<script src="yoob/controller.js"></script>
<script src="yoob/playfield.js"></script>
<script src="yoob/playfield-html-view.js"></script>
<script src="yoob/playfield-canvas-view.js"></script>
<script src="../script/jaccia.js"></script>
<script>
  var pf;

  // Text view
  var displayText = document.getElementById('display_text');
  var htmlView = new yoob.PlayfieldHTMLView().init(pf, displayText);
  htmlView.render = function(state) {
      return dumpMapper(state);
  };

  // Canvas view
  var displayCanvas = document.getElementById('display_canvas');
  var colourMap = {
      'Space': '#ffffff',
      'Wall':  '#000000',
      'Slime': '#00a000',
      'Food2': '#ff0000',
      'Food':  '#0000ff'
  };
  var canvasView = new yoob.PlayfieldCanvasView().init(pf, displayCanvas);
  canvasView.drawCell = function(ctx, value, playfieldX, playfieldY,
                                 canvasX, canvasY, cellWidth, cellHeight) {
      ctx.fillStyle = colourMap[value] || '#ffffff';
      ctx.fillRect(canvasX, canvasY, cellWidth, cellHeight);
  };

  // "View Manager"
  var currentView = 'text';
  var views = {
      'text': htmlView,
      'canvas': canvasView
  };
  var draw = function() {
      views[currentView].pf = pf;
      views[currentView].draw();
  };

  var selectDepiction = document.getElementById('select_depiction');
  selectDepiction.onchange = function() {
      var value = selectDepiction.options[selectDepiction.selectedIndex].value;
      if (value === 'text') {
          displayText.style.display = 'block';
          displayCanvas.style.display = 'none';
      } else {
          displayText.style.display = 'none';
          displayCanvas.style.display = 'block';
      }
      currentView = value;
      draw();
  };

  // Controller.  We don't subclass, we just monkeypatch.
  var c = new yoob.Controller();

  c.load = function(text) {
      pf = new yoob.Playfield();
      pf.setDefault('Space');
      pf.load(0, 0, text, loadMapper);
      draw();
  };

  c.step = function() {
      var newPf = new yoob.Playfield();
      newPf.setDefault('Space');
      evolve_playfield(pf, newPf);
      pf = newPf;
      draw();
  };

  c.connect({
    'start': 'start',
    'stop': 'stop',
    'step': 'step',
    'load': 'load',
    'edit': 'edit',
    'speed': 'speed',
    'source': 'input',
    'display': 'display_container'
  });
  
  c.click_load();
</script>