Skip to content

Commit

Permalink
initial proof of concept - super sloppy, but it works
Browse files Browse the repository at this point in the history
  • Loading branch information
joelongstreet committed Oct 6, 2014
1 parent 2255254 commit 3a49cd5
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 1 deletion.
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var port = process.env.PORT || 3000;
var connect = require('connect');
var serveStatic = require('serve-static');
var server = connect().use(serveStatic(__dirname)).listen(port);


// Connect to arduino
var arduinoReady = false;
var firmata = require('firmata');
var board = new firmata.Board('/dev/tty.usbmodem1411', function(){
isReady = true;
board.pinMode(9,board.MODES.SERVO);
});


// Handle socket events
var io = require('socket.io')(server);

io.on('connection', function(socket){
socket.on('hide-platform', function(){
if(arduinoReady)
board.servoWrite(9,50);
});

socket.on('show-platform', function(){
if(arduinoReady)
board.servoWrite(9,25);
});
});
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"author": "Joe Longstreet",
"license": "MIT",
"dependencies": {
"duino": "0.0.9"
"connect": "^3.2.0",
"serve-static": "^1.6.3",
"socket.io": "^1.1.0",
"firmata": "^0.3.3"
}
}
Binary file added public/assets/dude.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions public/assets/outie.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{ "height":5,
"layers":[
{
"data":[0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 8, 0, 0, 0, 0, 0, 0],
"height":5,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"orientation":"orthogonal",
"properties":
{

},
"tileheight":50,
"tilesets":[
{
"firstgid":1,
"image":"..\/..\/..\/..\/Desktop\/starstruck 2\/tiles-1.png",
"imageheight":64,
"imagewidth":272,
"margin":0,
"name":"tiles-1",
"properties":
{

},
"spacing":0,
"tileheight":16,
"tilewidth":16
}],
"tilewidth":50,
"version":1,
"width":5
}
39 changes: 39 additions & 0 deletions public/assets/platforms.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{ "height":5,
"layers":[
{
"data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 8, 0, 0, 0, 0, 0, 0],
"height":5,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"orientation":"orthogonal",
"properties":
{

},
"tileheight":50,
"tilesets":[
{
"firstgid":1,
"image":"..\/..\/..\/..\/Desktop\/starstruck 2\/tiles-1.png",
"imageheight":64,
"imagewidth":272,
"margin":0,
"name":"tiles-1",
"properties":
{

},
"spacing":0,
"tileheight":16,
"tilewidth":16
}],
"tilewidth":50,
"version":1,
"width":5
}
Binary file added public/assets/tiles-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>hello phaser!</title>
<script src='vendor/phaser/build/phaser.min.js'></script>
<script src="/socket.io/socket.io.js"></script>
<script src='index.js'></script>
</head>
<body style='padding:150px; background-color:black; margin: 150px;'>
</body>
</html>
126 changes: 126 additions & 0 deletions public/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
var socket = io();

var map;
var layer;
var player;
var facing = 'left';
var jumpTimer = 0;
var platformLayer;
var outieLayer;
var cursors;
var jumpButton;
var outieIsHidden = true;

var preload = function(){
game.load.tilemap('platforms', 'assets/platforms.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tilemap('outie', 'assets/outie.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('tiles-1', 'assets/tiles-1.png');
game.load.spritesheet('dude', 'assets/dude.png', 32, 48);
};


var hidePlatform = function(){
socket.emit('hide-platform');
outieLayer.visible = false;
};

var showPlatform = function(){
socket.emit('show-platform');
outieLayer.visible = true;
};


var create = function(){
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = '#000000';

game.physics.arcade.gravity.y = 250;

player = game.add.sprite(32, 32, 'dude');
game.physics.enable(player, Phaser.Physics.ARCADE);

player.body.bounce.y = 0.2;
player.body.collideWorldBounds = true;
player.body.setSize(20, 32, 5, 16);

player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('turn', [4], 20, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);

var platformMap = game.add.tilemap('platforms');
var outieMap = game.add.tilemap('outie');
platformMap.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
outieMap.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
platformMap.addTilesetImage('tiles-1');
outieMap.addTilesetImage('tiles-1');

platformLayer = platformMap.createLayer('Tile Layer 1');
outieLayer = outieMap.createLayer('Tile Layer 1');
platformLayer.debug=true;
outieLayer.debug=true;
outieLayer.visible = false;

game.camera.follow(player);

cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);


setInterval(function(){
outieIsHidden = !outieIsHidden;

if(outieIsHidden)
hidePlatform();
else
showPlatform();

}, 5000);
};


var update = function() {
if(outieIsHidden)
game.physics.arcade.collide(player, platformLayer);
else
game.physics.arcade.collide(player, outieLayer);

player.body.velocity.x = 0;

if (cursors.left.isDown){
player.body.velocity.x = -150;

if (facing != 'left'){
player.animations.play('left');
facing = 'left';
}
} else if (cursors.right.isDown){
player.body.velocity.x = 150;

if (facing != 'right'){
player.animations.play('right');
facing = 'right';
}
} else {
if (facing != 'idle') {
player.animations.stop();

if (facing == 'left')
player.frame = 0;
else
player.frame = 5;

facing = 'idle';
}
}

if (jumpButton.isDown && player.body.onFloor() && game.time.now > jumpTimer){
player.body.velocity.y = -250;
jumpTimer = game.time.now + 750;
}
};


var game = new Phaser.Game(
800, 600, Phaser.CANVAS, 'bizarrio',
{ preload: preload, create: create, update: update }
);

0 comments on commit 3a49cd5

Please sign in to comment.