Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
soapdog committed Jun 6, 2014
1 parent 3a70b50 commit c3c56e7
Show file tree
Hide file tree
Showing 27 changed files with 212,694 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

What is this file for? Mortar templates-or the goal we're aiming for- is they are self-contained and need no build step. In this case you've checked in the js files that bower pulls, which is great! But you should describe the purpose of .bowerrc in the README :-)

Also if this template's phaser installation can be updated with bower, aren't we missing bower.json?
I understand if both bower.json and .bowerrc are present, you should be able to update the phaser code. Otherwise we should probably delete all the bower stuff? :)

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

The objective is to be able to easily update phaser or install different versions. I've added the bower.json file and a note in the README.

"directory":"js"
}
57 changes: 54 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
mortar-phaser-game-stub
=======================
# Phaser Game Stub

This is a small game template to build games using the [Phaser Framework](http://phaser.io).

This is part of the [mortar](https://github.com/mozilla/mortar/) template collection for building [Open Web Apps](https://developer.mozilla.org/en-US/Apps).


## Downloading

There are a few ways to get this template:

If you use [Git](http://www.git-scm.com/):

````bash
git clone https://github.com/soapdog/mortar-phaser-game-stub.git

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

not the right url!

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

Oops, left the original url there. Sorry. Fixed.

````

Or download the latest version in this [ZIP file](https://github.com/soapdog/mortar-phaser-game-stub/archive/master.zip).


## Usage

Start a local server to simulate accessing the hosted app from the browser, and trying the *Install* button flow.

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

there's no need for local server so far, and there's no install button either ;) - unless you plan to add them?

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

Removed the bit about usage.

Do we need a task runner with stuff such as launching servers, packaging apps?

This comment has been minimized.

Copy link
@sole

sole Jun 17, 2014

Are you using that now? :-)
Always try to think of how you felt when you started programming. The more things you throw at the face of a new developer, the more confused they'll be.


For example:

````bash
python -m SimpleHTTPServer 8000
````

then access `localhost:8000` or `your.computer.ip:8000` (for example, `192.168.0.25`) using Firefox (Desktop or Mobile), or the Browser app in a Firefox OS simulator (or device).

You'll need to use the IP address when using a physical device. Change the port accordingly, if you're running a webserver in this port already.

## Phaser Framework

[Phaser](http://phaser.io) is a proven open source framework for creating HTML5 games. It has many features such as input control, game loops, camera control, tilemaps and more all baked in. Its a great tool to help you create your game and not focus on trivia stuff that is not related to your project or vision.


## Code walkthrough
The code is contained in three files &mdash boot.js, preloader.js, game.js &mdash each serves a different purpose and is explained below:

### boot.js
This file register our listener for the *DOMContentLoaded* event. This event will fire once all the html/css/js files are loaded.

In this file we setup the Phaser framework and register our game states. A game can have multiple game states. This stub has two states each defined in their own file.

### preload.js
This is the first game state. Its responsible for loading all the assets needed for this stub (in this case the graphic files). Once all the files are loaded, it moves to the next state.

### game.js
This another game state. It is our **gameloop** in a real game, it would be where you process user input and update your display.

Basically the **preload** state loads the resources to be used in the **game** state.

A web game template using [phaser](phaser.io)
Binary file added assets/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
html, body {
background: #000;
display: block;
margin: 0;
padding: 0;
}
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

needs indentation >>

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

strange... my editor is set to ident automagically but it is failing... fixed.

<meta charset="UTF-8" />
<title>Phaser Game Stub</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<script src="js/phaser/phaser.js"></script>

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

probably all the scripts here could use the defer attribute too

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

added defer entries...

<script src="js/game/boot.js"></script>

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

given that boot.js is the one that sets everything up, I'd suggest you put it at the end of all scripts :-)

Also I'd suggest you rename this file to main.js

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

Sounds good to me. Fixed.

<script src="js/game/preloader.js"></script>
<script src="js/game/game.js"></script>
</head>

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

indent >>

<body>

<div id="game"></div>

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

More indentation in general, you need to be tidy in templates because people need to see things easily at a quick glance :)


</body>
</html>
19 changes: 19 additions & 0 deletions js/game/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

document.addEventListener("DOMContentLoaded", function(event) {

// Create your Phaser game and inject it into the game div.

// Portrait game orientation. Invert the values if your game is in landscape orientation
var width = 320;

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

Maybe it's a bit early days, but can you detect the width and height of the viewport and use them instead of just hardcoding them here?

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

I added the detection with

 var width = window.innerWidth;
 var height = window.innerHeight;

But I now have a different problem which is my scenario image doesn't fit.... Think I will change that for a solid color as a background to the canvas and be happy.

var height = 480;

var game = new Phaser.Game(width, height, Phaser.AUTO, "game");

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

maybe you should explain what Phaser.AUTO and game are. As a new user going through the code I've got no idea what these things are doing.

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

this is all part of phaser. I've added some bits there and links to more docs.


// Add the States the game has.

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

What are the states? how are them supposed to work? Maybe add a very quick explanation!

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

Added link to documentation about states and some explanation

game.state.add('Preloader', BasicGame.Preloader); // <-- Loads the assets
game.state.add('Game', BasicGame.Game); // <-- Game loop.

// Now start the Boot state.

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

if it's boot, it's boot, if it's Preloader make it Preloader. Don't say it's boot but then call Preloader, it's confusing. Be consistent!

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

This was a typo from a previous version. Fixed.

game.state.start('Preloader');

});
83 changes: 83 additions & 0 deletions js/game/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
BasicGame.Game = function(game) {};

/**
* This is the Game game state. Its our game loop responsible for the game itself.
*
* In the create() function we setup the display.
* In the update() function we do the gameloop.
*
* To learn more about states refer to:
* Refer to: http://docs.phaser.io/Phaser.State.html
*/

BasicGame.Game.prototype = {
create: function() {

// Add the background
this.add.sprite(0, 0, 'background');

// Add the player
this.player = this.add.sprite(300, 240, 'player');
this.physics.arcade.enable(this.player);
this.player.enableBody = true;
this.player.body.collideWorldBounds = true;

// Add cursor input.
this.cursors = this.input.keyboard.createCursorKeys()
},

update: function() {
/*
This function is called in a loop using requestAnimationFrame(). It is our game loop, our heartbeat. Every time

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

line too long? the other comments are line-wrapped, why not this one?

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

I use soft wraps, this passed me by. Fixed.

it is called we process the user input and update the display.
*/


/*
Movement of the player using touch.
in this case we're just moving him
on one axis.
The keyboard example shows two axis movement.
To learn more about handling input refer to:
http://docs.phaser.io/Phaser.Input.html
*/

if (this.input.pointer1.isDown) {
if (this.input.pointer1.worldX > 240) {
this.player.body.velocity.x = 150;
}

if (this.input.pointer1.worldX <= 240) {
this.player.body.velocity.x = -150;
}
}

/*
Moving the player using the keyboard keys.
The move here is continuous because we apply
a velocity to the axis.
*/

if (this.cursors.right.isDown) {

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

I'm curious whether this is the best/recommended phaser approach. I'm worried this can be triggered too often if the function is called more often in quicker devices, and the player would end up being too fast.

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

I am following the recommended approach and what they do in the demos. This doesn't lead to it being called too fast.

This comment has been minimized.

Copy link
@sole

sole Jun 17, 2014

Cool :D - I'll trust you, will let you know if I see something weird when trying on diff devices :)

this.player.body.velocity.x = 150;
}

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

if (this.cursors.down.isDown) {
this.player.body.velocity.y = 150;
}

if (this.cursors.up.isDown) {
this.player.body.velocity.y = -150;
}

}

};
36 changes: 36 additions & 0 deletions js/game/preloader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var BasicGame = {}; // <-- Object to hold all our game states.

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

If someone is looking for the definition of BasicGame, where would they look for it? Definitely not in /js/game/preloader.js :-)

I suggest you create a file called js/game/BasicGame.js and put that there.

Also rename the existing:

  • js/game/preloader.js to js/game/BasicGame.Preloader.js
  • js/game/game.js to js/game/BasicGame.Game.js

still I'm not too sure about so many 'game' in these paths, we should discuss/brainstorm this!

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

There is no definition for basic game. That is just an empty umbrella object to hold the game states so they do go floating around like astray globals. BasicGame variable is declared on the top of preload.js since that is the first script to run.

I would not like to move this single var declaration to its own file. It sounds overkill to me. Also I am thinking about changing the name of the variable... yes, I am changing it now, you will think it is better. I am sure. :-) I will change it to GameStates.

This comment has been minimized.

Copy link
@sole

sole Jun 17, 2014

I created a bug regarding this. It might be overkill but it is obvious. You might want to add more things to the variable in the future. Definitely at least it's not inside another file ;)
#1


/**
* This is the Preloader game state. It is the first to run. It loads all the assets used by the game.
*/

BasicGame.Preloader = function (game) {
// placeholder, you can set some game state options in here if needed.
// Refer to: http://docs.phaser.io/Phaser.State.html
};

BasicGame.Preloader.prototype = {

/*
Game states have many functions that will be called at different stages of its lifecycle.
You can check these methods at:
- http://docs.phaser.io/Phaser.State.html#toc21
In this case here:
- preload(): will be called to load our game assets. Once loaded they are cached and any
other game state may use them.
- update(): this is the gameloop function. I will be called using requestAnimationFrame() in a loop

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

gameloop -> game loop
I -> It

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

Damn typos... fixed.

This comment has been minimized.

Copy link
@sole

sole Jun 17, 2014

:-D

until we do something to exit. In this case we switch to the Game state.
*/

preload: function() {

This comment has been minimized.

Copy link
@sole

sole Jun 9, 2014

I wonder if you could add some note saying hey these are placeholders, replace the images with your own. Otherwise it looks like you're using images when you could be using just CSS styles.

This comment has been minimized.

Copy link
@soapdog

soapdog Jun 13, 2014

Author Member

Note added.

this.load.image('background', 'assets/background.png');
this.load.image('player', 'assets/player.png');
},
update: function() {
this.state.start('Game');
}


};
40 changes: 40 additions & 0 deletions js/phaser/.bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "phaser",
"version": "2.0.4",
"main": "./phaser.js",
"homepage": "http://phaser.io",
"authors": [
"photonstorm <[email protected]>"
],
"description": "A fun, free and fast 2D game framework for making HTML5 games for desktop and mobile, supporting Canvas and WebGL.",
"keywords": [
"html5",
"game",
"games",
"framework",
"canvas",
"WebGL",
"tilemaps",
"physics",
"sprites",
"fonts",
"images",
"audio",
"Web",
"Audio",
"touch",
"input",
"mobile"
],
"license": "MIT",
"_release": "2.0.4",
"_resolution": {
"type": "version",
"tag": "2.0.4",
"commit": "312d41912763d901490a6e00d461f95bf0bf475f"
},
"_source": "git://github.com/nDmitry/bower-phaser.git",
"_target": "~2.0.4",
"_originalSource": "phaser",
"_direct": true
}
17 changes: 17 additions & 0 deletions js/phaser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# bower-phaser

The source for this package is in the [main Phaser repo](https://github.com/photonstorm/phaser).

## Install

Install with `bower`:

```shell
bower install phaser
```

Add a `<script>` to your HTML:

```html
<script src="/bower_components/phaser/phaser.js"></script>
```
30 changes: 30 additions & 0 deletions js/phaser/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "phaser",
"version": "2.0.4",
"main": "./phaser.js",
"homepage": "http://phaser.io",
"authors": [
"photonstorm <[email protected]>"
],
"description": "A fun, free and fast 2D game framework for making HTML5 games for desktop and mobile, supporting Canvas and WebGL.",
"keywords": [
"html5",
"game",
"games",
"framework",
"canvas",
"WebGL",
"tilemaps",
"physics",
"sprites",
"fonts",
"images",
"audio",
"Web",
"Audio",
"touch",
"input",
"mobile"
],
"license": "MIT"
}
Loading

0 comments on commit c3c56e7

Please sign in to comment.