Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="game-wrapper"></div>
              
            
!

CSS

              
                // base CSS from http://bettermotherfuckingwebsite.com/
body {
  margin: 40px auto;
  max-width: 640px;
  line-height: 1.6;
  font-size: 18px;
  font-family: sans-serif;
  color: #444;
  padding: 0 10px;
}

h1 {
  line-height: 1.2;
}

$tileSize: 16px;
$playerColor: lime;
$wallColor: rebeccapurple;
$enemyColor: red;
$weaponColor: blue;
$healthColor: gold;
$exitColor: #444;

#game-wrapper {
  display: flex;
  flex-flow: column;
}

#game {
  width: 640px;
  height: 480px;
  line-height: 0;
  
  .weapon {
    background-color: $weaponColor;
  }

  .health {
    background-color: $healthColor;
  }
}

.gameover {
  .tile {
    opacity: 0.4;
  }
  
  .hidden {
    opacity: 0.2;
    visibility: visible;
  }
  position: relative;
  z-index: -1;
  
  &::before {
    content: "GAME OVER";
    display: block;
    text-align: center;
    line-height: 480px;
    width: 640px;
    height: 480px;
    font-size: 38px;
    z-index: 100;
    opacity: 1;
    position: absolute;
    left: 0px;
    top: 0px;
  }
  
  + #global-controls :not(.reset), ~ #infos {
    opacity: 0.2;
    pointer-events: none;
  }
}

.gamewon {
  @extend .gameover;
  
  &::before {
    content: "YOU WIN!";
  }
}

.tile {
  width: percentage($tileSize / 640px);
  height: percentage($tileSize / 480px);
  display: inline-block;
  background-color: gray;
  position: relative;
  
  > span {
    position: absolute;
    bottom: 50%;
    left: 0px;
    text-align: center;
    font-size: $tileSize * 2 / 3;
  }
}

.player.p1 {
  background: repeating-linear-gradient(to bottom right, $playerColor, $playerColor 6px, red 6px, red 8px);
}

.player.p2 {
  background: repeating-linear-gradient(to bottom left, $playerColor, $playerColor 6px, blue 6px, blue 8px);
}

.player.dead {
  $playerColor: #444;
  &.p1 {
    background: repeating-linear-gradient(to bottom right, $playerColor, $playerColor 6px, red 6px, red 8px);
  }
  &.p2 {
    background: repeating-linear-gradient(to bottom left, $playerColor, $playerColor 6px, blue 6px, blue 8px);
  }
}

.wall {
  background-color: $wallColor;
}

.enemy {
  background-color: $enemyColor;
}

.boss {
  > span {
    left: 2px;
  }
  
  box-sizing: border-box;
  background: radial-gradient(circle farthest-corner, $enemyColor, darken($enemyColor, 30%));
  // border: 3px solid darken($enemyColor, 10%);
  z-index: 3;
  
  animation-duration: 2s;
  animation-name: bossspin;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: linear;
  
  @keyframes bossspin {
    from {
      transform: scale(1);
    }
    
    85% {
      transform: scale(1);
    }
    
    to {
      transform: scale(1.5);
    }
  }
}

.hidden {
  visibility: hidden;
}

.exit {
  box-sizing: border-box;
  background-color: $exitColor;
  border: 0px solid lighten($exitColor, 10%);
    
  animation-duration: 2s;
  animation-name: exitflash;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: ease-in-out;
  
  @keyframes exitflash {
    from {
      border-width: 0px;
    }

    to {
      border-width: $tileSize / 3;
    }
  }
}
  
.floor {
  flex: none;
}

#infos {
  display: flex;
  flex-flow: column;
}

#global-controls, .info {
  display: flex;
  flex-flow: row;
  align-items: center;
  margin: 0.5em 0;
}

#global-controls {
  justify-content: space-between;
}

.info {
  width: 100%;
  
  .player {
    width: $tileSize;
    height: $tileSize;
    flex: none;
    margin-right: 1em;
  }
  
  .controls {
    flex: none;
    position: relative;
    width: 60px;
    height: 40px;
    
    button {
      width: 20px;
      height: 20px;
      padding: 0px;
    }
    
    button:nth-child(1) {
      position: absolute;
      left: 20px;
      top: 0px;
    }
    
    button:nth-child(2) {
      position: absolute;
      left: 0px;
      top: 10px;
    }
    
    button:nth-child(3) {
      position: absolute;
      left: 20px;
      top: 20px;
    }
    
    button:nth-child(4) {
      position: absolute;
      left: 40px;
      top: 10px;
    }
  }
  
  > span {
    text-align: center;
  }
  
  .health, .level {
    flex: auto;
    position: relative;
    z-index: -2;
    background: none;
    
    .health_bar, .xp_bar {
      z-index: -1;
      position: absolute;
      left: 0px;
      top: 0px;
      height: 100%;
    }
  }
  
  .health_bar, .xp_bar {
    transition: width 0.25s linear;
  }
  
  .health_bar {
    background-color: $healthColor;
  }
  
  .xp_bar {
    background-color: lighten($enemyColor, 25%);
  }
  
  .level {
    width: 6em;
    flex: none;
  }
  
  .weapon {
    width: 12em;
    flex: none;
    border: 2px solid $weaponColor;
    box-sizing: border-box;
  }
  
  .weapon.new {
    @keyframes flashnew {
      from {
        background-color: $weaponColor;
        color: white;
      }
      
      to {
        background-color: white;
        color: #444;
      }
    }
    
    animation-duration: 0.5s;
    animation-name: flashnew;
  }
}

              
            
!

JS

              
                const fowEnabled = true;

var SoundURLs = {
  "heal": "https://cdn.glitch.com/2252d86c-892d-4ebd-91fc-c38d44e2159b%2Fheal.wav?1499560301918",
  "die": "https://cdn.glitch.com/2252d86c-892d-4ebd-91fc-c38d44e2159b%2Fdie.wav?1499560301996",
  "kill": "https://cdn.glitch.com/2252d86c-892d-4ebd-91fc-c38d44e2159b%2Fkill.wav?1499560302253",
  "move": "https://cdn.glitch.com/2252d86c-892d-4ebd-91fc-c38d44e2159b%2Fmove.wav?1499560302572",
  "floor": "https://cdn.glitch.com/2252d86c-892d-4ebd-91fc-c38d44e2159b%2Ffloor.wav?1499560302629",
  "weapon": "https://cdn.glitch.com/2252d86c-892d-4ebd-91fc-c38d44e2159b%2Fweapon.wav?1499560302755",
  "attack": "https://cdn.glitch.com/2252d86c-892d-4ebd-91fc-c38d44e2159b%2Fattack.wav?1499560303081"
}
var Sounds = {};
for (var sound in SoundURLs) {
  Sounds[sound] = new Audio(SoundURLs[sound]);
}
Sounds.play = function(sound) {
  Sounds[sound].currentTime = 0;
  Sounds[sound].play();
}

// GAME LOGIC CODE
// ===============
var Logic = {};
Logic.canvas = {width: 640, height: 480};
Logic.tileSize = 16;
Logic.world = {width: Logic.canvas.width / Logic.tileSize, height: Logic.canvas.height / Logic.tileSize};
Logic.weaponList = [
  {name: "Stick", damage: "2-4", weight: 0},
  {name: "Lousy Sword", damage: "4-7", weight: 2},
  {name: "Sword", damage: "5-8", weight: 3},
  {name: "Nice Sword", damage: "6-9", weight: 2},
  {name: "Mace", damage: "4-10", weight: 2},
  {name: "RNG Bait", damage: "1-15", weight: 1},
  {name: "Golden Dagger", damage: "100-100", weight: 0.1}
];
Logic.playerVisionRange = function({level}) {
  return _.clamp(5 + level, 0, Logic.world.width + Logic.world.height);
};
Logic.enemyActiveRangeFactor = 2;
Logic.selectWeapon = function(floor) {
  var weaponList = _.cloneDeep(Logic.weaponList);
  var totalWeight = _.sumBy(weaponList, 'weight');
  weaponList = weaponList.map(w => ({...w, weight: w.weight / totalWeight}));
  weaponList = weaponList.reduce((list, next) => list.concat({...next, weight: next.weight + _.last(list).weight}), [{weight: 0}]);
  var rawWeaponIdx = Math.random();
  var weaponIdx = 1 - (rawWeaponIdx ** (1 + floor / 10));
  var result = weaponList.find(({weight}) => weight >= weaponIdx);
  // console.log("Rolled " + rawWeaponIdx + " which scaled to " + weaponIdx + " and got", result);
  return result;
};

Logic.isEmpty = function({x, y}, world) {
  if (x < 0 || x > Logic.world.width - 1 || y < 0 || y > Logic.world.height - 1) return false;
  const tile = world[y][x];
  if (!tile.type) return true;
  return false;
};
Logic.getEmptyCoords = function(world) {
  var x, y;
  do {
    x = _.random(0, Logic.world.width - 1);
    y = _.random(0, Logic.world.height - 1);
  } while (!Logic.isEmpty({x, y}, world));
  return {x, y};
};

Logic.make = {
  world: function() {
    var world = [];
    for (var y = 0; y < Logic.world.height; y++) {
      world[y] = [];
      for (var x = 0; x < Logic.world.width; x++) {
        world[y][x] = {};
        if (x === 0 || x === Logic.world.width - 1 || y === 0 || y === Logic.world.height - 1) world[y][x].type = "wall";
      }
    }

    return world;
  },
  rooms: function(world) {
    function placeWall(y, x) {
      var newWorld = _.cloneDeep(world);
      newWorld[y][x].type = "wall";
      if (Logic.isConnected(newWorld)) {
        world[y][x].type = "wall";
      }
    }

    const heightFactors = _.range(Logic.world.height / 3).filter((x) => Logic.world.height % x === 0 && x > 1);
    const widthFactors = _.range(Logic.world.width / 3).filter((x) => Logic.world.width % x === 0 && x > 1);
    const roomsY = _.sample(heightFactors);
    const roomsX = _.sample(widthFactors);
    const totalRooms = roomsY * roomsX;
    const roomWidth = Logic.world.width / roomsX;
    const roomHeight = Logic.world.height / roomsY;

    var buildWall = () => Math.random() > 0.3 / (totalRooms / 12);
    var buildPiece = _.memoize((i, j, size) => {
      var include = Array(size - 2).fill(true);
      include[0] = Math.random() > 0.7;
      include = _.shuffle(include);
      include.push(true);
      include.unshift(true);
      for (var k = 0; k < i; k++) include.push(...include);
      return include;
    }, (...args) => args.join("-"));

    for (var j = 0; j < roomsY; j++) {
      for (var i = 0; i < roomsX; i++) {
        if (buildWall()) {
          for (var x = roomWidth * i; x < roomWidth * i + roomWidth; x++) {
            if (buildPiece(i, j, roomWidth)[x]) placeWall(roomHeight * j, x);
          }
        }
        if (buildWall()) {
          for (var y = roomHeight * j; y < roomHeight * j + roomHeight; y++) {
            if (buildPiece(j, i, roomHeight)[y]) placeWall(y, roomWidth * i);
          }
        }
      }
    }
  },
  revealedTable: function() {
    var revealed = [];
    for (var y = 0; y < Logic.world.height; y++) {
      revealed[y] = Array(Logic.world.width).fill(false);
    }
    return revealed;
  }
};

Logic.spawn = function(type, world, count, floor = 1) {
  var results = [];
  for (var i = 0; i < count; i++) {
    var {x, y} = Logic.getEmptyCoords(world);
    var tile = world[y][x];
    tile.type = type;
    if (type === "weapon") {
      tile.weapon = Logic.selectWeapon(floor);
    }
    if (type === "enemy") {
      tile.health = 5 * floor;
      tile.damage = Math.floor((floor + 1) / 2);
      tile.id = _.uniqueId(type);
    }
    if (type === "boss") {
      tile.health = 15 * floor;
      tile.damage = floor + 1;
      tile.id = _.uniqueId(type);
    }
    if (type === "player") {
      tile.level = 1;
      tile.health = 10;
      tile.weapon = Logic.weaponList[0];
      tile.xp = 0;
      tile.x = x;
      tile.y = y;
    }
    results.push(tile);
  }
  return results;
};

Logic.distance = function(point1, point2) {
  // Pathfinding? Never heard of it!
  let dx = Math.abs(point1.x - point2.x);
  let dy = Math.abs(point1.y - point2.y);
  return dx + dy;
};

Logic.runEnemyAI = function(players, world) {
  var idsSeen = [];
  var playerPaths = players.map(p => [p, Logic.buildPathsTo(p, world)]);
  for (var origY = 0; origY < world.length; origY++) {
    for (var origX = 0; origX < world[0].length; origX++) {
      var tile = world[origY][origX];
      if (tile.type !== "enemy" && tile.type !== "boss") continue;
      if (idsSeen.indexOf(tile.id) > -1) continue;
      var closePlayerPaths = playerPaths.map(([player, path]) => {
        return [player, path, Logic.distance(player, {x: origX, y: origY})];
      }).filter(([player, path, dist]) => dist < Logic.enemyActiveRangeFactor * Logic.playerVisionRange(player));
      if (closePlayerPaths.length > 0) {
        var [player, path] = _.minBy(closePlayerPaths, 2);
        var playerX = player.x;
        var playerY = player.y;
        var pathEntry = path[origY][origX];
        if (pathEntry === false) {
          pathEntry = {dx: 0, dy: 0};
        }
        var {dx, dy} = pathEntry;
        var xClear = Logic.isEmpty({x: origX + dx, y: origY}, world);
        var yClear = Logic.isEmpty({x: origX, y: origY + dy}, world);
        if (!xClear && !yClear) continue;
        var [newX, newY] = [origX + dx, origY + dy];
        if (newX === playerX && newY === playerY) continue;
        if (Logic.isEmpty({x: newX, y: newY}, world)) {
          idsSeen.push(tile.id);
          world[origY][origX] = world[newY][newX];
          world[newY][newX] = tile;
        }
      }
    }
  }
};

Logic.attemptMove = function({x, y}, world) {
  if (x < 0 || x > Logic.world.width - 1 || y < 0 || y > Logic.world.height - 1) return ["revert"];
  const tile = world[y][x];
  if (!tile.type) return [];
  if (tile.type === "wall") return ["revert"];
  if (tile.type === "player") return ["revert"];
  if (tile.type === "enemy") return ["revert", "damage " + tile.damage, "edamage"];
  if (tile.type === "boss") return ["revert", "damage " + tile.damage, "bdamage"];
  if (tile.type === "weapon") return ["collectWeapon"];
  if (tile.type === "health") return ["healthBoost"];
  if (tile.type === "exit") return ["nextFloor"];
  alert("Unexpected tile type: " + tile.type + " at " + x + ", " + y);
  return false;
};
Logic.reveal = function(players, revealed) {
  players.forEach((player) => {
    var radius = Logic.playerVisionRange(player);
    var {x, y} = player;
    for (var dx = -radius; dx <= radius; dx++) {
      var w = radius - Math.abs(dx);
      for (var dy = -w; dy <= w; dy++) {
        var rx = _.clamp(x + dx, 0, Logic.world.width - 1);
        var ry = _.clamp(y + dy, 0, Logic.world.height - 1);
        revealed[ry][rx] = true;
      }
    }
  });
  return revealed.every(r => r.every(x => x));
};
Logic.rollDamage = function(weapon, level) {
  var [min, max] = weapon.damage.split("-");
  return Math.round(_.random(min, max, true) * (level + 3) / 4);
};

Logic.maxHealth = function(player) {
  return player.level * 5 + 5;
};
Logic.nextXP = function(player) {
  return 5 * Math.floor(player.level ** 1.5);
};
Logic.buildPathsTo = function(origin, world) {
  var path = [];
  for (var y = 0; y < Logic.world.height; y++) {
    path[y] = Array(Logic.world.width).fill(false);
  }
  path[origin.y][origin.x] = {dx: 0, dy: 0};
  function available({x, y}) {
    var passable = Logic.isEmpty({x, y}, world);
    if (world[y][x] && (world[y][x].type === "enemy" || world[y][x].type === "boss")) {
      passable = true;
    }
    return passable && path[y][x] === false;
  }
  var pending = [origin];
  while (pending.length > 0) {
    var {x, y} = pending.shift();
    if (available({x: x - 1, y})) {
      path[y][x - 1] = {dx: 1, dy: 0};
      pending.push({x: x - 1, y});
    }
    if (available({x: x + 1, y})) {
      path[y][x + 1] = {dx: -1, dy: 0};
      pending.push({x: x + 1, y});
    }
    if (available({x, y: y - 1})) {
      path[y - 1][x] = {dx: 0, dy: 1};
      pending.push({x, y: y - 1});
    }
    if (available({x, y: y + 1})) {
      path[y + 1][x] = {dx: 0, dy: -1};
      pending.push({x, y: y + 1});
    }
  }
  return path;
};

Logic.isConnected = function(world) {
  var paths = Logic.buildPathsTo({x: 1, y: 1}, world);
  for (var y = 0; y < paths.length; y++) {
    for (var x = 0; x < paths[y].length; x++) {
      if (paths[y][x] === false && Logic.isEmpty({x, y}, world)) {
        return false;
      }
    }
  }
  return true;
};

Logic.addXP = function(player, xp) {
  player.xp += xp;
  while (player.xp >= Logic.nextXP(player)) {
    player.xp -= Logic.nextXP(player);
    player.level++;
    player.health = Logic.maxHealth(player);
  }
}

// UI CODE
// =======

var UI = {
  Arrows: [
    ["\u2191", "\u2190", "\u2193", "\u2192"],
    ["W", "A", "S", "D"]
  ],
  Tile: React.createClass({
    render: function() {
      var cl = "tile";
      if (this.props.type) {
        cl += " " + this.props.type;
      }
      if (this.props.className) {
        cl += " " + this.props.className;
      }
      if (!this.props.revealed && fowEnabled) {
        cl += " hidden";
      }
      var healthText;
      if (this.props.health) healthText = <span>{this.props.health}</span>;
      return (
        <div className={cl}>{healthText}</div>
      )
    }
  }),
  Game: React.createClass({
    getInitialState: function() {
      var world = Logic.make.world();
      Logic.make.rooms(world);
      Logic.spawn("enemy", world, 5);
      Logic.spawn("weapon", world, 2);
      Logic.spawn("health", world, 3);
      Logic.spawn("exit", world, 1);

      var players = Logic.spawn("player", world, 2);
      players.forEach((p, i) => {
        p.className = "p" + (i + 1);
      });
      
      var floor = 1;

      var revealed = Logic.make.revealedTable();
      Logic.reveal(players, revealed);
      var fullyRevealed = false;
      return {world, floor, players, revealed, fullyRevealed};
    },
    move: function(i, dx, dy) {
      return () => {
        var players = _.clone(this.state.players);
        var player = _.clone(players[i]);
        if (player.won || player.health <= 0) return;
        var {world, floor, fullyRevealed} = this.state;
        var revealed = _.cloneDeep(this.state.revealed);
        var sound = "";
        player.x += dx;
        player.y += dy;
        player.weapon.new = false;
        var response = Logic.attemptMove(player, world);
        if (response.indexOf("revert") > -1) {
          player.x -= dx;
          player.y -= dy;
        } else {
          world[player.y - dy][player.x - dx] = world[player.y][player.x];
          world[player.y][player.x] = player;
          sound = "move";
        }
        if (response.indexOf("collectWeapon") > -1) {
          var oldWeapon = player.weapon;
          player.weapon = _.clone(world[player.y - dy][player.x - dx].weapon);
          player.weapon.new = true;
          world = _.cloneDeep(world);
          world[player.y - dy][player.x - dx].weapon = _.clone(oldWeapon);
          sound = "weapon";
        }
        if (response.indexOf("healthBoost") > -1) {
          player.health = Logic.maxHealth(player);
          world = _.cloneDeep(world);
          delete world[player.y - dy][player.x - dx].type;
          sound = "heal";
        }
        if (response.indexOf("nextFloor") > -1) {
          floor++;
          world = Logic.make.world();
          Logic.make.rooms(world);
          Logic.spawn("enemy", world, 5 + floor, floor);
          Logic.spawn("weapon", world, 2, floor);
          Logic.spawn("health", world, 3 + floor / 2, floor);
          Logic.spawn("exit", world, 1);
          if (Math.random() < (floor - 4) / 4) {
            Logic.spawn("boss", world, 1, floor);
          }
          players[i] = player;
          players.forEach(p => {
            var {x, y} = Logic.getEmptyCoords(world);
            p.health = _.clamp(p.health + floor, 0, Logic.maxHealth(p));
            p.x = x;
            p.y = y;
            world[y][x] = p;
          });
          revealed = Logic.make.revealedTable();
          Logic.reveal(players, revealed);
          fullyRevealed = false;
          sound = "floor";
        }
        if (response.filter((x) => x.indexOf("damage") === 0).length > 0) {
          var damages = response.filter((x) => x.indexOf("damage") === 0);
          damages.forEach((x) => {
            var [damage, amt] = x.split(" ");
            player.health -= parseInt(amt);
          });
          sound = "attack";
          if (player.health <= 0) {
            sound = "die";
          }
        }
        if (response.indexOf("edamage") > -1 || response.indexOf("bdamage") > -1) {
          var ex = player.x + dx;
          var ey = player.y + dy;
          world = _.cloneDeep(world);
          world[ey][ex].health -= Logic.rollDamage(player.weapon, player.level);
          if (world[ey][ex].health <= 0) {
            world[ey][ex] = {};
            Logic.addXP(player, floor);
            if (response.indexOf("bdamage") > -1) {
              player.won = true;
            }
            sound = "kill";
          }
        }
        players[i] = player;
        world[player.y][player.x] = player;
        Logic.runEnemyAI(players, world);
        if (Logic.reveal(players, revealed) && !fullyRevealed) {
          players.forEach(p => Logic.addXP(p, floor));
          fullyRevealed = true;
        }
        this.setState({
          players,
          world,
          revealed,
          floor,
          fullyRevealed,
          inbound: false
        });
        this.playSound({sound, inbound: false});
        // Needed when used as keyboard event handler
        return false;
      }
    },
    cheat: function() {
      function cheatedPlayer(player) {
        return {
          ...player,
          level: 1337,
          health: 9001,
          xp: 0,
          weapon: {name: 'H4X', damage: "30-50"}
        };
      }
      this.setState({
        players: this.state.players.map(cheatedPlayer),
        floor: 100
      });
    },
    reset: function() {
      this.setState(this.getInitialState());
    },
    playSound: function({sound, inbound}) {
      if (sound !== '') {
        Sounds.play(sound);
        if (this.props.sendSound && !inbound) {
          this.props.sendSound(sound);
        }
      }
    },
    swap: function() {
      var [p1, p2] = _.cloneDeep(this.state.players);
      var world = this.state.world;
      [p1.x, p2.x] = [p2.x, p1.x];
      [p1.y, p2.y] = [p2.y, p1.y];
      world[p1.y][p1.x] = p1;
      world[p2.y][p2.x] = p2;
      var players = [p1, p2];
      this.setState({players, world});
    },
    splitXP: function() {
      var [p1, p2] = _.clone(this.state.players);
      var newXP = Math.floor((p1.xp + p2.xp) / 2);
      p1.xp = newXP;
      p2.xp = newXP;
      this.setState({players: [p1, p2]});
    },
    componentDidMount: function() {
      this.props.listenForState((state) => {
        state.inbound = true;
        this.setState(state);
      });
      this.props.listenForSound((sound) => {
        this.playSound({sound, inbound: true});
      });
    },
    componentDidUpdate: function(oldProps, oldState) {
      if (this.props.sendState && !this.state.inbound) {
        this.props.sendState(this.state);
      }
    },
    render: function() {
      var makeTiles = (row, y) => row.map((tile, x) => {
        // if (this.state.player.x === x && this.state.player.y === y) return <UI.Player />;
        var className = tile.className;
        if (tile.health <= 0) {
          className += " dead";
        }
        return <UI.Tile
                 type={tile.type}
                 health={tile.type !== "player" && tile.health}
                 revealed={this.state.revealed[y][x]}
                 className={className}
                 />;
      });
      var makeInfo = (player, i) => {
        var healthPct = player.health / Logic.maxHealth(player) * 100 + "%";
        var xpPct = player.xp / Logic.nextXP(player) * 100 + "%";
        var lvlMult = "";
        if (player.level > 1) {
          lvlMult = " x" + (player.level + 3) / 4;
        }
        return (
          <div className="info" key={i}>
            <div className={"tile player " + player.className + (player.health <= 0 ? " dead" : "")}></div>
            <div className="controls">
              <button onClick={this.move(i, 0, -1)}>{UI.Arrows[i][0]}</button>
              <button onClick={this.move(i, -1, 0)}>{UI.Arrows[i][1]}</button>
              <button onClick={this.move(i, 0, 1)}>{UI.Arrows[i][2]}</button>
              <button onClick={this.move(i, 1, 0)}>{UI.Arrows[i][3]}</button>
            </div>
            <span className="health">
              <div className="health_bar" style={{width: healthPct}} />
              <div>
                {player.health}/{Logic.maxHealth(player)}
              </div>
            </span>
            <span className="level">
              <div className="xp_bar" style={{width: xpPct}} />
              <div>
                Level {player.level}
              </div>
            </span>
            <span className={(player.weapon.new ? "new " : "") + "weapon"}>
              {player.weapon.name} ({player.weapon.damage}{lvlMult})
            </span>
          </div>
        );
      };
      var gameClass = "";
      if (_.every(this.state.players, p => p.health <= 0)) {
        gameClass = "gameover";
      } else if (_.some(this.state.players, 'won')) {
        gameClass = "gamewon";
      }
      return (
        <div>
          <div id="game" className={gameClass}>
            {this.state.world.map(makeTiles)}
          </div>
          <div id="global-controls">
            <span className="floor">Floor {this.state.floor}</span>
            <span>
              <button className="swap" onClick={this.swap}>Switch Places</button>
              <button className="split" onClick={this.splitXP}>Split XP</button>
            </span>
            <button className="reset" onClick={this.reset}>Reset</button>
          </div>
          <div id="infos">
            {this.state.players.map(makeInfo)}
          </div>
        </div>
      );
    }
  }),
  NetworkLayer: React.createClass({
    getInitialState: function() {
      return {room: ''};
    },
    handleChange(event) {
      this.props.leaveRoom(this.state.room);
      this.props.joinRoom(event.target.value);
      this.setState({room: event.target.value});
    },
    move(...args) {
      return this.game.move(...args);
    },
    cheat() {
      return this.game.cheat();
    },
    render: function() {
      return <div>
        <form>
          <label>
            {"Internet multiplayer room: "}
            <input type="text" value={this.state.room} onChange={this.handleChange} />
          </label>
        </form>
        <UI.Game
          sendState={this.props.sendState}
          sendSound={this.props.sendSound}
          listenForState={this.props.listenForState}
          listenForSound={this.props.listenForSound}
          ref={game => this.game = game}
          />
        </div>;
    }
  })
};

document.addEventListener("DOMContentLoaded", () => {
  var socket = io("https://rogueish-server.glitch.me");
  var connected = false;
  function sendState(state) {
    if (connected && !state.inbound) {
      socket.emit('state', state);
    }
  }
  function sendSound(sound) {
    if (connected) {
      socket.emit('sound', sound);
    }
  }
  function listenForState(cb) {
    socket.on('state', cb);
  }
  function listenForSound(cb) {
    socket.on('sound', cb);
  }
  function leaveRoom(room) {
    socket.emit('leaving', room);
    connected = false;
  }
  function joinRoom(room) {
    if (room !== '') {
      socket.emit('joining', room);
      connected = true;
    }
  }
  var game = ReactDOM.render(<UI.NetworkLayer
                               sendState={sendState}
                               sendSound={sendSound}
                               listenForState={listenForState}
                               listenForSound={listenForSound}
                               leaveRoom={leaveRoom}
                               joinRoom={joinRoom}
                               />, document.getElementById("game-wrapper"));
  Mousetrap.bind('up', game.move(0, 0, -1));
  Mousetrap.bind('down', game.move(0, 0, 1));
  Mousetrap.bind('left', game.move(0, -1, 0));
  Mousetrap.bind('right', game.move(0, 1, 0));
  Mousetrap.bind('w', game.move(1, 0, -1));
  Mousetrap.bind('s', game.move(1, 0, 1));
  Mousetrap.bind('a', game.move(1, -1, 0));
  Mousetrap.bind('d', game.move(1, 1, 0));
  Mousetrap.bind('enter up up down down left right left right b a enter', game.cheat);
});

              
            
!
999px

Console