Group now extends PIXI.DisplayObjectContainer, rather than owning a _container property, which makes life a whole lot easier re: nesting.

This commit is contained in:
photonstorm
2014-02-06 02:31:36 +00:00
parent 9737710200
commit 4cfce8b4d2
89 changed files with 1176 additions and 1521 deletions
+422
View File
@@ -0,0 +1,422 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('chunk', 'assets/sprites/chunk.png');
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('ball', 'assets/sprites/shinyball.png');
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
}
var sprite;
var sprite2;
var sprite3;
var group;
var flag = false;
var bmd;
function create() {
game.stage.backgroundColor = '#124184';
bmd = game.add.bitmapData(800, 600);
bmd.fillStyle('#ffffff');
var bg = game.add.sprite(0, 0, bmd);
bg.body.moves = false;
test12();
}
function test12() {
// game.physics.gravity.y = 150;
sprite = game.add.sprite(200, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.right = false;
// sprite.body.checkCollision.up = false;
// sprite.body.immovable = true;
sprite2 = game.add.sprite(400, 358, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
game.input.onDown.add(launch12, this);
}
function launch12() {
sprite2.body.velocity.x = -200;
// sprite2.body.velocity.y = -60;
}
function test11() {
// game.physics.gravity.y = 150;
sprite = game.add.sprite(300, 200, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.checkCollision.down = false;
// sprite.body.checkCollision.up = false;
sprite.body.checkCollision.right = false;
// sprite.body.immovable = true;
sprite2 = game.add.sprite(290, 400, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
game.input.onDown.add(launch11, this);
}
function launch11() {
sprite2.body.velocity.y = -200;
}
function test10() {
// game.physics.gravity.y = 150;
sprite = game.add.sprite(300, 200, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.down = false;
sprite2 = game.add.sprite(330, 400, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
game.input.onDown.add(launch10, this);
}
function launch10() {
sprite2.body.velocity.y = -100;
}
function test9() {
// game.physics.gravity.y = 150;
sprite = game.add.sprite(300, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.up = false;
// sprite.body.checkCollision.right = false;
sprite2 = game.add.sprite(330, 100, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
game.input.onDown.add(launch9, this);
}
function launch9() {
sprite2.body.velocity.y = 100;
}
function test8() {
game.physics.gravity.y = 150;
sprite = game.add.sprite(300, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.left = false;
sprite.body.checkCollision.right = false;
sprite2 = game.add.sprite(500, 400, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
game.input.onDown.add(launch8, this);
}
function launch8() {
sprite.body.velocity.x = -50;
sprite2.body.velocity.x = -200;
}
function test7() {
game.physics.gravity.y = 200;
sprite = game.add.sprite(0, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
// sprite.body.velocity.y = 100;
// sprite.body.gravity.y = 200;
// sprite.body.linearDamping = 0.2;
game.input.onDown.add(launch7, this);
}
function launch7() {
sprite.body.velocity.x = -200;
sprite.body.velocity.y = 200;
}
function test6() {
game.physics.gravity.y = 100;
sprite = game.add.sprite(300, 200, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.5, 0.5);
game.input.onDown.add(launch6, this);
}
function launch6() {
sprite.body.velocity.x = 200;
sprite.body.velocity.y = -200;
}
function test5() {
sprite = game.add.sprite(0, 600, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.bounce.setTo(0.9, 0.9);
game.input.onDown.add(launch5, this);
}
function launch5() {
sprite.body.velocity.x = 100;
sprite.body.velocity.y = -100;
game.time.events.add(Phaser.Timer.SECOND * 4, stop5, this);
}
function stop5() {
sprite.body.velocity.x = -100;
// sprite.body.velocity.y = 100;
console.log(sprite.x, sprite.body.x);
}
function test4() {
game.physics.gravity.y = 150;
sprite = game.add.sprite(300, 0, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.9, 0.9);
sprite2 = game.add.sprite(310, 500, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
// sprite2.body.bounce.setTo(0.9, 0.9);
game.input.onDown.add(launch4, this);
}
function launch4() {
sprite.body.velocity.y = 200;
}
function test3() {
game.physics.gravity.y = 50;
// group = game.add.group();
sprite = game.add.sprite(500, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.9, 0.9);
sprite2 = game.add.sprite(100, 400, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
// sprite2.body.immovable = true;
// sprite2.body.bounce.setTo(0.5, 0.5);
sprite3 = game.add.sprite(700, 400, 'gameboy', 3);
sprite3.name = 'yellow';
sprite3.body.collideWorldBounds = true;
sprite3.body.bounce.setTo(0.5, 0.5);
// sprite.body.velocity.x = -300;
// sprite.body.velocity.y = -200;
// group.add(sprite);
// group.add(sprite2);
// group.add(sprite3);
game.input.onDown.add(launch3, this);
}
function launch3() {
sprite.body.velocity.x = -200;
sprite.body.velocity.y = -200;
}
function test2() {
// game.physics.gravity.y = 100;
sprite = game.add.sprite(700, 400, 'ball');
sprite.name = 'sprite1';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
sprite2 = game.add.sprite(100, 400, 'ball');
sprite2.name = 'sprite2';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
sprite2.body.immovable = true;
// sprite.body.velocity.x = -225;
// sprite2.body.velocity.x = 225;
game.input.onDown.addOnce(launch2, this);
}
function launch2() {
sprite.body.velocity.x = -225;
sprite2.body.velocity.x = 225;
}
function test1() {
// game.physics.gravity.y = 100;
sprite = game.add.sprite(100, 400, 'ball');
sprite.name = 'sprite1';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
sprite2 = game.add.sprite(700, 400, 'ball');
sprite2.name = 'sprite2';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
game.input.onDown.add(launch1, this);
}
function launch1() {
sprite.body.velocity.x = 225;
// sprite2.body.velocity.x = -225;
}
function update() {
// game.physics.collide(group, group);
if (sprite3)
{
game.physics.collide(sprite, [sprite2, sprite3]);
game.physics.collide(sprite2, sprite3);
}
else
{
game.physics.collide(sprite, sprite2);
}
if (sprite)
{
bmd.fillStyle('#ffff00');
bmd.fillRect(sprite.body.center.x, sprite.body.center.y, 2, 2);
}
if (sprite2)
{
bmd.fillStyle('#ff00ff');
bmd.fillRect(sprite2.body.center.x, sprite2.body.center.y, 2, 2);
}
if (sprite3)
{
bmd.fillStyle('#0000ff');
bmd.fillRect(sprite3.body.center.x, sprite3.body.center.y, 2, 2);
}
}
function render() {
if (sprite)
{
game.debug.renderBodyInfo(sprite2, 16, 24);
// game.debug.renderText(sprite.name + ' x: ' + sprite.x.toFixed(2) + ' dx: ' + sprite.body._dx.toFixed(2), 16, 500);
// game.debug.renderText(sprite.name + ' y: ' + sprite.y.toFixed(2) + ' dy: ' + sprite.body._dy.toFixed(2), 16, 520);
}
if (sprite2)
{
// game.debug.renderBodyInfo(sprite2, 16, 190);
// game.debug.renderText(sprite2.name + ' x: ' + sprite2.x, 400, 500);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 196 B

+862
View File
@@ -0,0 +1,862 @@
// Version 0.2 - Copyright 2013 - Jim Riecken <jimr@jimr.ca>
//
// Released under the MIT License - https://github.com/jriecken/sat-js
//
// A simple library for determining intersections of circles and
// polygons using the Separating Axis Theorem.
/** @preserve SAT.js - Version 0.2 - Copyright 2013 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */
/*global define: false, module: false*/
/*jshint shadow:true, sub:true, forin:true, noarg:true, noempty:true,
eqeqeq:true, bitwise:true, strict:true, undef:true,
curly:true, browser:true */
// Create a UMD wrapper for SAT. Works in:
//
// - Plain browser via global SAT variable
// - AMD loader (like require.js)
// - Node.js
//
// The quoted properties all over the place are used so that the Closure Compiler
// does not mangle the exposed API in advanced mode.
/**
* @param {*} root - The global scope
* @param {Function} factory - Factory that creates SAT module
*/
(function (root, factory) {
"use strict";
if (typeof define === 'function' && define['amd']) {
define(factory);
} else if (typeof exports === 'object') {
module['exports'] = factory();
} else {
root['SAT'] = factory();
}
}(this, function () {
"use strict";
var SAT = {};
//
// ## Vector
//
// Represents a vector in two dimensions with `x` and `y` properties.
// Create a new Vector, optionally passing in the `x` and `y` coordinates. If
// a coordinate is not specified, it will be set to `0`
/**
* @param {?number=} x The x position.
* @param {?number=} y The y position.
* @constructor
*/
function Vector(x, y) {
this['x'] = x || 0;
this['y'] = y || 0;
}
SAT['Vector'] = Vector;
// Alias `Vector` as `V`
SAT['V'] = Vector;
// Copy the values of another Vector into this one.
/**
* @param {Vector} other The other Vector.
* @return {Vector} This for chaining.
*/
Vector.prototype['copy'] = Vector.prototype.copy = function(other) {
this['x'] = other['x'];
this['y'] = other['y'];
return this;
};
// Change this vector to be perpendicular to what it was before. (Effectively
// roatates it 90 degrees in a clockwise direction)
/**
* @return {Vector} This for chaining.
*/
Vector.prototype['perp'] = Vector.prototype.perp = function() {
var x = this['x'];
this['x'] = this['y'];
this['y'] = -x;
return this;
};
// Rotate this vector (counter-clockwise) by the specified angle (in radians).
/**
* @param {number} angle The angle to rotate (in radians)
* @return {Vector} This for chaining.
*/
Vector.prototype['rotate'] = Vector.prototype.rotate = function (angle) {
var x = this['x'];
var y = this['y'];
this['x'] = x * Math.cos(angle) - y * Math.sin(angle);
this['y'] = x * Math.sin(angle) + y * Math.cos(angle);
return this;
};
// Rotate this vector (counter-clockwise) by the specified angle (in radians) which has already been calculated into sin and cos.
/**
* @param {number} sin - The Math.sin(angle)
* @param {number} cos - The Math.cos(angle)
* @return {Vector} This for chaining.
*/
Vector.prototype['rotatePrecalc'] = Vector.prototype.rotatePrecalc = function (sin, cos) {
var x = this['x'];
var y = this['y'];
this['x'] = x * cos - y * sin;
this['y'] = x * sin + y * cos;
return this;
};
// Reverse this vector.
/**
* @return {Vector} This for chaining.
*/
Vector.prototype['reverse'] = Vector.prototype.reverse = function() {
this['x'] = -this['x'];
this['y'] = -this['y'];
return this;
};
// Normalize this vector. (make it have length of `1`)
/**
* @return {Vector} This for chaining.
*/
Vector.prototype['normalize'] = Vector.prototype.normalize = function() {
var d = this.len();
if(d > 0) {
this['x'] = this['x'] / d;
this['y'] = this['y'] / d;
}
return this;
};
// Add another vector to this one.
/**
* @param {Vector} other The other Vector.
* @return {Vector} This for chaining.
*/
Vector.prototype['add'] = Vector.prototype.add = function(other) {
this['x'] += other['x'];
this['y'] += other['y'];
return this;
};
// Subtract another vector from this one.
/**
* @param {Vector} other The other Vector.
* @return {Vector} This for chaiing.
*/
Vector.prototype['sub'] = Vector.prototype.sub = function(other) {
this['x'] -= other['x'];
this['y'] -= other['y'];
return this;
};
// Scale this vector. An independant scaling factor can be provided
// for each axis, or a single scaling factor that will scale both `x` and `y`.
/**
* @param {number} x The scaling factor in the x direction.
* @param {?number=} y The scaling factor in the y direction. If this
* is not specified, the x scaling factor will be used.
* @return {Vector} This for chaining.
*/
Vector.prototype['scale'] = Vector.prototype.scale = function(x,y) {
this['x'] *= x;
this['y'] *= y || x;
return this;
};
// Project this vector on to another vector.
/**
* @param {Vector} other The vector to project onto.
* @return {Vector} This for chaining.
*/
Vector.prototype['project'] = Vector.prototype.project = function(other) {
var amt = this.dot(other) / other.len2();
this['x'] = amt * other['x'];
this['y'] = amt * other['y'];
return this;
};
// Project this vector onto a vector of unit length. This is slightly more efficient
// than `project` when dealing with unit vectors.
/**
* @param {Vector} other The unit vector to project onto.
* @return {Vector} This for chaining.
*/
Vector.prototype['projectN'] = Vector.prototype.projectN = function(other) {
var amt = this.dot(other);
this['x'] = amt * other['x'];
this['y'] = amt * other['y'];
return this;
};
// Reflect this vector on an arbitrary axis.
/**
* @param {Vector} axis The vector representing the axis.
* @return {Vector} This for chaining.
*/
Vector.prototype['reflect'] = Vector.prototype.reflect = function(axis) {
var x = this['x'];
var y = this['y'];
this.project(axis).scale(2);
this['x'] -= x;
this['y'] -= y;
return this;
};
// Reflect this vector on an arbitrary axis (represented by a unit vector). This is
// slightly more efficient than `reflect` when dealing with an axis that is a unit vector.
/**
* @param {Vector} axis The unit vector representing the axis.
* @return {Vector} This for chaining.
*/
Vector.prototype['reflectN'] = Vector.prototype.reflectN = function(axis) {
var x = this['x'];
var y = this['y'];
this.projectN(axis).scale(2);
this['x'] -= x;
this['y'] -= y;
return this;
};
// Get the dot product of this vector and another.
/**
* @param {Vector} other The vector to dot this one against.
* @return {number} The dot product.
*/
Vector.prototype['dot'] = Vector.prototype.dot = function(other) {
return this['x'] * other['x'] + this['y'] * other['y'];
};
// Get the squared length of this vector.
/**
* @return {number} The length^2 of this vector.
*/
Vector.prototype['len2'] = Vector.prototype.len2 = function() {
return this.dot(this);
};
// Get the length of this vector.
/**
* @return {number} The length of this vector.
*/
Vector.prototype['len'] = Vector.prototype.len = function() {
return Math.sqrt(this.len2());
};
// ## Circle
//
// Represents a circle with a position and a radius.
// Create a new circle, optionally passing in a position and/or radius. If no position
// is given, the circle will be at `(0,0)`. If no radius is provided, the circle will
// have a radius of `0`.
/**
* @param {Vector=} pos A vector representing the position of the center of the circle
* @param {?number=} r The radius of the circle
* @constructor
*/
function Circle(pos, r) {
this['pos'] = pos || new Vector();
this['r'] = r || 0;
}
SAT['Circle'] = Circle;
// ## Polygon
//
// Represents a *convex* polygon with any number of points (specified in counter-clockwise order)
//
// The edges/normals of the polygon will be calculated on creation and stored in the
// `edges` and `normals` properties. If you change the polygon's points, you will need
// to call `recalc` to recalculate the edges/normals.
// Create a new polygon, passing in a position vector, and an array of points (represented
// by vectors relative to the position vector). If no position is passed in, the position
// of the polygon will be `(0,0)`.
/**
* @param {Vector=} pos A vector representing the origin of the polygon. (all other
* points are relative to this one)
* @param {Array.<Vector>=} points An array of vectors representing the points in the polygon,
* in counter-clockwise order.
* @constructor
*/
function Polygon(pos, points) {
this['pos'] = pos || new Vector();
this['points'] = points || [];
this.recalc();
}
SAT['Polygon'] = Polygon;
// Recalculates the edges and normals of the polygon. This **must** be called
// if the `points` array is modified at all and the edges or normals are to be
// accessed.
/**
* @return {Polygon} This for chaining.
*/
Polygon.prototype['recalc'] = Polygon.prototype.recalc = function() {
// The edges here are the direction of the `n`th edge of the polygon, relative to
// the `n`th point. If you want to draw a given edge from the edge value, you must
// first translate to the position of the starting point.
this['edges'] = [];
// The normals here are the direction of the normal for the `n`th edge of the polygon, relative
// to the position of the `n`th point. If you want to draw an edge normal, you must first
// translate to the position of the starting point.
this['normals'] = [];
var points = this['points'];
var len = points.length;
for (var i = 0; i < len; i++) {
var p1 = points[i];
var p2 = i < len - 1 ? points[i + 1] : points[0];
var e = new Vector().copy(p2).sub(p1);
var n = new Vector().copy(e).perp().normalize();
this['edges'].push(e);
this['normals'].push(n);
}
return this;
};
// Rotates this polygon counter-clockwise around the origin of *its local coordinate system* (i.e. `pos`).
//
// Note: You do **not** need to call `recalc` after rotation.
/**
* @param {number} angle The angle to rotate (in radians)
* @return {Polygon} This for chaining.
*/
Polygon.prototype['rotate'] = Polygon.prototype.rotate = function(angle) {
var i;
var points = this['points'];
var edges = this['edges'];
var normals = this['normals'];
var len = points.length;
// Calc it just the once, rather than 4 times per array element
var cos = Math.cos(angle);
var sin = Math.sin(angle);
for (i = 0; i < len; i++) {
points[i].rotatePrecalc(sin, cos);
edges[i].rotatePrecalc(sin, cos);
normals[i].rotatePrecalc(sin, cos);
}
return this;
};
// Rotates this polygon counter-clockwise around the origin of *its local coordinate system* (i.e. `pos`).
//
// Note: You do **not** need to call `recalc` after rotation.
/**
* @param {number} angle The angle to rotate (in radians)
* @return {Polygon} This for chaining.
*/
Polygon.prototype['scale'] = Polygon.prototype.scale = function(x, y) {
var i;
var points = this['points'];
var edges = this['edges'];
var normals = this['normals'];
var len = points.length;
for (i = 0; i < len; i++) {
points[i].scale(x,y);
edges[i].scale(x,y);
normals[i].scale(x,y);
}
return this;
};
// Translates the points of this polygon by a specified amount relative to the origin of *its own coordinate
// system* (i.e. `pos`).
//
// This is most useful to change the "center point" of a polygon.
//
// Note: You do **not** need to call `recalc` after translation.
/**
* @param {number} x The horizontal amount to translate.
* @param {number} y The vertical amount to translate.
* @return {Polygon} This for chaining.
*/
Polygon.prototype['translate'] = Polygon.prototype.translate = function (x, y) {
var i;
var points = this['points'];
var len = points.length;
for (i = 0; i < len; i++) {
points[i].x += x;
points[i].y += y;
}
return this;
};
// ## Box
//
// Represents an axis-aligned box, with a width and height.
// Create a new box, with the specified position, width, and height. If no position
// is given, the position will be `(0,0)`. If no width or height are given, they will
// be set to `0`.
/**
* @param {Vector=} pos A vector representing the top-left of the box.
* @param {?number=} w The width of the box.
* @param {?number=} h The height of the box.
* @constructor
*/
function Box(pos, w, h) {
this['pos'] = pos || new Vector();
this['w'] = w || 0;
this['h'] = h || 0;
}
SAT['Box'] = Box;
// Returns a polygon whose edges are the same as this box.
/**
* @return {Polygon} A new Polygon that represents this box.
*/
Box.prototype['toPolygon'] = Box.prototype.toPolygon = function() {
var pos = this['pos'];
var w = this['w'];
var h = this['h'];
return new Polygon(new Vector(pos['x'], pos['y']), [
new Vector(), new Vector(w, 0),
new Vector(w,h), new Vector(0,h)
]);
};
// ## Response
//
// An object representing the result of an intersection. Contains:
// - The two objects participating in the intersection
// - The vector representing the minimum change necessary to extract the first object
// from the second one (as well as a unit vector in that direction and the magnitude
// of the overlap)
// - Whether the first object is entirely inside the second, and vice versa.
/**
* @constructor
*/
function Response() {
this['a'] = null;
this['b'] = null;
this['overlapN'] = new Vector();
this['overlapV'] = new Vector();
this.clear();
}
SAT['Response'] = Response;
// Set some values of the response back to their defaults. Call this between tests if
// you are going to reuse a single Response object for multiple intersection tests (recommented
// as it will avoid allcating extra memory)
/**
* @return {Response} This for chaining
*/
Response.prototype['clear'] = Response.prototype.clear = function() {
this['aInB'] = true;
this['bInA'] = true;
this['overlap'] = Number.MAX_VALUE;
return this;
};
// ## Object Pools
// A pool of `Vector` objects that are used in calculations to avoid
// allocating memory.
/**
* @type {Array.<Vector>}
*/
var T_VECTORS = [];
for (var i = 0; i < 10; i++) { T_VECTORS.push(new Vector()); }
// A pool of arrays of numbers used in calculations to avoid allocating
// memory.
/**
* @type {Array.<Array.<number>>}
*/
var T_ARRAYS = [];
for (var i = 0; i < 5; i++) { T_ARRAYS.push([]); }
// ## Helper Functions
// Flattens the specified array of points onto a unit vector axis,
// resulting in a one dimensional range of the minimum and
// maximum value on that axis.
/**
* @param {Array.<Vector>} points The points to flatten.
* @param {Vector} normal The unit vector axis to flatten on.
* @param {Array.<number>} result An array. After calling this function,
* result[0] will be the minimum value,
* result[1] will be the maximum value.
*/
function flattenPointsOn(points, normal, result) {
var min = Number.MAX_VALUE;
var max = -Number.MAX_VALUE;
var len = points.length;
for (var i = 0; i < len; i++ ) {
// The magnitude of the projection of the point onto the normal
var dot = points[i].dot(normal);
if (dot < min) { min = dot; }
if (dot > max) { max = dot; }
}
result[0] = min; result[1] = max;
}
// Check whether two convex polygons are separated by the specified
// axis (must be a unit vector).
/**
* @param {Vector} aPos The position of the first polygon.
* @param {Vector} bPos The position of the second polygon.
* @param {Array.<Vector>} aPoints The points in the first polygon.
* @param {Array.<Vector>} bPoints The points in the second polygon.
* @param {Vector} axis The axis (unit sized) to test against. The points of both polygons
* will be projected onto this axis.
* @param {Response=} response A Response object (optional) which will be populated
* if the axis is not a separating axis.
* @return {boolean} true if it is a separating axis, false otherwise. If false,
* and a response is passed in, information about how much overlap and
* the direction of the overlap will be populated.
*/
function isSeparatingAxis(aPos, bPos, aPoints, bPoints, axis, response) {
var rangeA = T_ARRAYS.pop();
var rangeB = T_ARRAYS.pop();
// The magnitude of the offset between the two polygons
var offsetV = T_VECTORS.pop().copy(bPos).sub(aPos);
var projectedOffset = offsetV.dot(axis);
// Project the polygons onto the axis.
flattenPointsOn(aPoints, axis, rangeA);
flattenPointsOn(bPoints, axis, rangeB);
// Move B's range to its position relative to A.
rangeB[0] += projectedOffset;
rangeB[1] += projectedOffset;
// Check if there is a gap. If there is, this is a separating axis and we can stop
if (rangeA[0] > rangeB[1] || rangeB[0] > rangeA[1]) {
T_VECTORS.push(offsetV);
T_ARRAYS.push(rangeA);
T_ARRAYS.push(rangeB);
return true;
}
// This is not a separating axis. If we're calculating a response, calculate the overlap.
if (response) {
var overlap = 0;
// A starts further left than B
if (rangeA[0] < rangeB[0]) {
response['aInB'] = false;
// A ends before B does. We have to pull A out of B
if (rangeA[1] < rangeB[1]) {
overlap = rangeA[1] - rangeB[0];
response['bInA'] = false;
// B is fully inside A. Pick the shortest way out.
} else {
var option1 = rangeA[1] - rangeB[0];
var option2 = rangeB[1] - rangeA[0];
overlap = option1 < option2 ? option1 : -option2;
}
// B starts further left than A
} else {
response['bInA'] = false;
// B ends before A ends. We have to push A out of B
if (rangeA[1] > rangeB[1]) {
overlap = rangeA[0] - rangeB[1];
response['aInB'] = false;
// A is fully inside B. Pick the shortest way out.
} else {
var option1 = rangeA[1] - rangeB[0];
var option2 = rangeB[1] - rangeA[0];
overlap = option1 < option2 ? option1 : -option2;
}
}
// If this is the smallest amount of overlap we've seen so far, set it as the minimum overlap.
var absOverlap = Math.abs(overlap);
if (absOverlap < response['overlap']) {
response['overlap'] = absOverlap;
response['overlapN'].copy(axis);
if (overlap < 0) {
response['overlapN'].reverse();
}
}
}
T_VECTORS.push(offsetV);
T_ARRAYS.push(rangeA);
T_ARRAYS.push(rangeB);
return false;
}
// Calculates which Vornoi region a point is on a line segment.
// It is assumed that both the line and the point are relative to `(0,0)`
//
// | (0) |
// (-1) [S]--------------[E] (1)
// | (0) |
/**
* @param {Vector} line The line segment.
* @param {Vector} point The point.
* @return {number} LEFT_VORNOI_REGION (-1) if it is the left region,
* MIDDLE_VORNOI_REGION (0) if it is the middle region,
* RIGHT_VORNOI_REGION (1) if it is the right region.
*/
function vornoiRegion(line, point) {
var len2 = line.len2();
var dp = point.dot(line);
// If the point is beyond the start of the line, it is in the
// left vornoi region.
if (dp < 0) { return LEFT_VORNOI_REGION; }
// If the point is beyond the end of the line, it is in the
// right vornoi region.
else if (dp > len2) { return RIGHT_VORNOI_REGION; }
// Otherwise, it's in the middle one.
else { return MIDDLE_VORNOI_REGION; }
}
// Constants for Vornoi regions
/**
* @const
*/
var LEFT_VORNOI_REGION = -1;
/**
* @const
*/
var MIDDLE_VORNOI_REGION = 0;
/**
* @const
*/
var RIGHT_VORNOI_REGION = 1;
// ## Collision Tests
// Check if two circles collide.
/**
* @param {Circle} a The first circle.
* @param {Circle} b The second circle.
* @param {Response=} response Response object (optional) that will be populated if
* the circles intersect.
* @return {boolean} true if the circles intersect, false if they don't.
*/
function testCircleCircle(a, b, response) {
// Check if the distance between the centers of the two
// circles is greater than their combined radius.
var differenceV = T_VECTORS.pop().copy(b['pos']).sub(a['pos']);
var totalRadius = a['r'] + b['r'];
var totalRadiusSq = totalRadius * totalRadius;
var distanceSq = differenceV.len2();
// If the distance is bigger than the combined radius, they don't intersect.
if (distanceSq > totalRadiusSq) {
T_VECTORS.push(differenceV);
return false;
}
// They intersect. If we're calculating a response, calculate the overlap.
if (response) {
var dist = Math.sqrt(distanceSq);
response['a'] = a;
response['b'] = b;
response['overlap'] = totalRadius - dist;
response['overlapN'].copy(differenceV.normalize());
response['overlapV'].copy(differenceV).scale(response['overlap']);
response['aInB']= a['r'] <= b['r'] && dist <= b['r'] - a['r'];
response['bInA'] = b['r'] <= a['r'] && dist <= a['r'] - b['r'];
}
T_VECTORS.push(differenceV);
return true;
}
SAT['testCircleCircle'] = testCircleCircle;
// Check if a polygon and a circle collide.
/**
* @param {Polygon} polygon The polygon.
* @param {Circle} circle The circle.
* @param {Response=} response Response object (optional) that will be populated if
* they interset.
* @return {boolean} true if they intersect, false if they don't.
*/
function testPolygonCircle(polygon, circle, response) {
// Get the position of the circle relative to the polygon.
var circlePos = T_VECTORS.pop().copy(circle['pos']).sub(polygon['pos']);
var radius = circle['r'];
var radius2 = radius * radius;
var points = polygon['points'];
var len = points.length;
var edge = T_VECTORS.pop();
var point = T_VECTORS.pop();
// For each edge in the polygon:
for (var i = 0; i < len; i++) {
var next = i === len - 1 ? 0 : i + 1;
var prev = i === 0 ? len - 1 : i - 1;
var overlap = 0;
var overlapN = null;
// Get the edge.
edge.copy(polygon['edges'][i]);
// Calculate the center of the circle relative to the starting point of the edge.
point.copy(circlePos).sub(points[i]);
// If the distance between the center of the circle and the point
// is bigger than the radius, the polygon is definitely not fully in
// the circle.
if (response && point.len2() > radius2) {
response['aInB'] = false;
}
// Calculate which Vornoi region the center of the circle is in.
var region = vornoiRegion(edge, point);
// If it's the left region:
if (region === LEFT_VORNOI_REGION) {
// We need to make sure we're in the RIGHT_VORNOI_REGION of the previous edge.
edge.copy(polygon['edges'][prev]);
// Calculate the center of the circle relative the starting point of the previous edge
var point2 = T_VECTORS.pop().copy(circlePos).sub(points[prev]);
region = vornoiRegion(edge, point2);
if (region === RIGHT_VORNOI_REGION) {
// It's in the region we want. Check if the circle intersects the point.
var dist = point.len();
if (dist > radius) {
// No intersection
T_VECTORS.push(circlePos);
T_VECTORS.push(edge);
T_VECTORS.push(point);
T_VECTORS.push(point2);
return false;
} else if (response) {
// It intersects, calculate the overlap.
response['bInA'] = false;
overlapN = point.normalize();
overlap = radius - dist;
}
}
T_VECTORS.push(point2);
// If it's the right region:
} else if (region === RIGHT_VORNOI_REGION) {
// We need to make sure we're in the left region on the next edge
edge.copy(polygon['edges'][next]);
// Calculate the center of the circle relative to the starting point of the next edge.
point.copy(circlePos).sub(points[next]);
region = vornoiRegion(edge, point);
if (region === LEFT_VORNOI_REGION) {
// It's in the region we want. Check if the circle intersects the point.
var dist = point.len();
if (dist > radius) {
// No intersection
T_VECTORS.push(circlePos);
T_VECTORS.push(edge);
T_VECTORS.push(point);
return false;
} else if (response) {
// It intersects, calculate the overlap.
response['bInA'] = false;
overlapN = point.normalize();
overlap = radius - dist;
}
}
// Otherwise, it's the middle region:
} else {
// Need to check if the circle is intersecting the edge,
// Change the edge into its "edge normal".
var normal = edge.perp().normalize();
// Find the perpendicular distance between the center of the
// circle and the edge.
var dist = point.dot(normal);
var distAbs = Math.abs(dist);
// If the circle is on the outside of the edge, there is no intersection.
if (dist > 0 && distAbs > radius) {
// No intersection
T_VECTORS.push(circlePos);
T_VECTORS.push(normal);
T_VECTORS.push(point);
return false;
} else if (response) {
// It intersects, calculate the overlap.
overlapN = normal;
overlap = radius - dist;
// If the center of the circle is on the outside of the edge, or part of the
// circle is on the outside, the circle is not fully inside the polygon.
if (dist >= 0 || overlap < 2 * radius) {
response['bInA'] = false;
}
}
}
// If this is the smallest overlap we've seen, keep it.
// (overlapN may be null if the circle was in the wrong Vornoi region).
if (overlapN && response && Math.abs(overlap) < Math.abs(response['overlap'])) {
response['overlap'] = overlap;
response['overlapN'].copy(overlapN);
}
}
// Calculate the final overlap vector - based on the smallest overlap.
if (response) {
response['a'] = polygon;
response['b'] = circle;
response['overlapV'].copy(response['overlapN']).scale(response['overlap']);
}
T_VECTORS.push(circlePos);
T_VECTORS.push(edge);
T_VECTORS.push(point);
return true;
}
SAT['testPolygonCircle'] = testPolygonCircle;
// Check if a circle and a polygon collide.
//
// **NOTE:** This is slightly less efficient than polygonCircle as it just
// runs polygonCircle and reverses everything at the end.
/**
* @param {Circle} circle The circle.
* @param {Polygon} polygon The polygon.
* @param {Response=} response Response object (optional) that will be populated if
* they interset.
* @return {boolean} true if they intersect, false if they don't.
*/
function testCirclePolygon(circle, polygon, response) {
// Test the polygon against the circle.
var result = testPolygonCircle(polygon, circle, response);
if (result && response) {
// Swap A and B in the response.
var a = response['a'];
var aInB = response['aInB'];
response['overlapN'].reverse();
response['overlapV'].reverse();
response['a'] = response['b'];
response['b'] = a;
response['aInB'] = response['bInA'];
response['bInA'] = aInB;
}
return result;
}
SAT['testCirclePolygon'] = testCirclePolygon;
// Checks whether polygons collide.
/**
* @param {Polygon} a The first polygon.
* @param {Polygon} b The second polygon.
* @param {Response=} response Response object (optional) that will be populated if
* they interset.
* @return {boolean} true if they intersect, false if they don't.
*/
function testPolygonPolygon(a, b, response) {
var aPoints = a['points'];
var aLen = aPoints.length;
var bPoints = b['points'];
var bLen = bPoints.length;
// If any of the edge normals of A is a separating axis, no intersection.
for (var i = 0; i < aLen; i++) {
if (isSeparatingAxis(a['pos'], b['pos'], aPoints, bPoints, a['normals'][i], response)) {
return false;
}
}
// If any of the edge normals of B is a separating axis, no intersection.
for (var i = 0;i < bLen; i++) {
if (isSeparatingAxis(a['pos'], b['pos'], aPoints, bPoints, b['normals'][i], response)) {
return false;
}
}
// Since none of the edge normals of A or B are a separating axis, there is an intersection
// and we've already calculated the smallest overlap (in isSeparatingAxis). Calculate the
// final overlap vector.
if (response) {
response['a'] = a;
response['b'] = b;
response['overlapV'].copy(response['overlapN']).scale(response['overlap']);
}
return true;
}
SAT['testPolygonPolygon'] = testPolygonPolygon;
return SAT;
}));
+100
View File
@@ -0,0 +1,100 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('car', 'assets/sprites/car90.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
}
var car;
var aliens;
function create() {
// aliens = game.add.group();
// for (var i = 0; i < 50; i++)
// {
// var s = aliens.create(game.world.randomX, game.world.randomY, 'baddie');
// s.name = 'alien' + s;
// s.body.collideWorldBounds = true;
// s.body.bounce.setTo(0.8, 0.8);
// s.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40);
// }
car = game.add.sprite(0, 300, 'car');
car.anchor.setTo(0.5, 0.5);
car.body.collideWorldBounds = true;
car.body.bounce.setTo(0.8, 0.8);
car.body.allowRotation = true;
car.body.linearDamping = 0.4;
// car.body.acceleration.x = 10;
game.input.onDown.add(start, this);
}
var s = 0;
function start() {
// car.body.velocity.x = 200;
car.body.acceleration.x = 100;
s = game.time.now;
}
function update() {
// if (car.x >= 400 && car.body.velocity.x > 0)
// {
// car.body.velocity.x = 0;
// car.body.acceleration.x = 0;
// var total = game.time.now - s;
// console.log(game.time.physicsElapsed);
// console.log('total ms', total, 'px/sec', car.x/(total/1000));
// }
// car.body.velocity.x = 0;
// car.body.velocity.y = 0;
car.body.angularVelocity = 0;
car.body.acceleration.x = 0;
car.body.acceleration.y = 0;
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
car.body.angularVelocity = -200;
// car.body.acceleration.x = -100;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
car.body.angularVelocity = 200;
// car.body.acceleration.x = 100;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
game.physics.accelerationFromRotation(car.rotation, 400, car.body.acceleration);
// car.body.acceleration.x = 10;
// car.body.acceleration.copyFrom(game.physics.velocityFromAngle(car.angle, 30));
// car.body.velocity.copyFrom(game.physics.velocityFromAngle(car.angle, 300));
}
else
{
// game.physics.accelerationFromRotation(car.rotation, 10, car.body.acceleration);
}
// game.physics.collide(car, aliens);
}
function render() {
// game.debug.renderSpriteInfo(car, 32, 32);
game.debug.renderBodyInfo(car, 16, 24);
}
+46
View File
@@ -0,0 +1,46 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.audio('music', 'assets/audio/CatAstroPhi_shmup_normal.wav');
}
var music;
function create() {
music = game.add.audio('music');
// play: function (marker, position, volume, loop, forceRestart) {
music.play('', 0, 1, true);
game.input.onDown.add(toggleAudio, this);
}
function toggleAudio() {
if (music.isPlaying)
{
music.pause();
}
else
{
music.resume();
}
}
function update() {
}
function render() {
game.debug.renderSoundInfo(music, 32, 32);
}
+281
View File
@@ -0,0 +1,281 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('chunk', 'assets/sprites/chunk.png');
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
game.load.image('p', 'assets/sprites/mushroom2.png');
game.load.image('ball', 'assets/sprites/shinyball.png');
}
var sprite;
var sprite2;
var track;
var bmd;
function create() {
game.stage.backgroundColor = '#124184';
bmd = game.add.bitmapData(800, 600);
bmd.fillStyle('#ffffff');
var bg = game.add.sprite(0, 0, bmd);
bg.body.moves = false;
test9();
}
// stacked
function test9() {
game.physics.gravity.y = 500;
sprite = game.add.sprite(400, 100, 'ball');
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
sprite.body.minBounceVelocity = 0.8;
sprite2 = game.add.sprite(400, 300, 'ball');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.5, 0.5);
sprite2.body.minBounceVelocity = 0.8;
// game.input.onDown.add(launch8, this);
}
// gravity into floor jiggle
function test8() {
game.physics.gravity.y = 1000;
sprite = game.add.sprite(400, 100, 'ball');
sprite.body.collideWorldBounds = true;
// it's all about tweaking these values
sprite.body.bounce.setTo(0.8, 0.8);
sprite.body.minBounceVelocity = 1.2;
sprite.body.velocity.x = -400;
sprite.body.linearDamping = 1.2;
sprite2 = game.add.sprite(500, 100, 'ball');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.5, 0.5);
sprite2.body.minBounceVelocity = 0.8;
sprite2.body.linearDamping = 0.5;
game.input.onDown.add(launch8, this);
}
function launch8() {
// sprite.body.velocity.x = -200;
sprite2.body.velocity.x = 200;
}
// both sprites are shot into each other vertically
function test7() {
// game.physics.gravity.y = 50;
sprite = game.add.sprite(400, 600, 'ball');
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
sprite2 = game.add.sprite(400, 100, 'ball');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
track = sprite2;
game.input.onDown.add(launch7, this);
}
function launch7() {
sprite.body.velocity.y = -200;
// sprite2.body.velocity.y = 200;
}
// sprite is on the bottom, blocked by the world bounds, sprite2 falls onto it via gravity only
function test6() {
game.physics.gravity.y = 100;
sprite = game.add.sprite(380, 600, 'p');
sprite.body.collideWorldBounds = true;
sprite2 = game.add.sprite(400, 100, 'ball');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
track = sprite2;
}
// sprite is on the bottom, blocked by the world bounds, sprite2 falls onto it
function test5() {
game.physics.gravity.y = 50;
sprite = game.add.sprite(400, 600, 'ball');
sprite.body.collideWorldBounds = true;
sprite2 = game.add.sprite(400, 100, 'ball');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
track = sprite2;
game.input.onDown.add(launch5, this);
}
function launch5() {
sprite2.body.velocity.y = 200;
}
// bounce both sprites into each other, one with lots less velocity - checking newtons craddle approach
function test4() {
game.physics.gravity.y = 100;
sprite = game.add.sprite(780, 400, 'ball');
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
sprite2 = game.add.sprite(0, 400, 'ball');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
track = sprite2;
game.input.onDown.add(launch4, this);
}
function launch4() {
sprite.body.velocity.x = 70;
sprite2.body.velocity.x = -250;
}
// bounce both sprites into each other, identical bounce + velocity
function test3() {
game.physics.gravity.y = 100;
sprite = game.add.sprite(780, 400, 'p');
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
sprite2 = game.add.sprite(0, 400, 'p');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
track = sprite2;
game.input.onDown.add(launch3, this);
}
function launch3() {
sprite.body.velocity.x = 225;
sprite2.body.velocity.x = -225;
}
// sprite is on the right, blocked by the world bounds, sprite2 bounces into it
function test2() {
game.physics.gravity.y = 100;
sprite = game.add.sprite(780, 400, 'p');
sprite.body.collideWorldBounds = true;
sprite2 = game.add.sprite(0, 400, 'p');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
track = sprite2;
game.input.onDown.add(launch2, this);
}
function launch2() {
sprite2.body.velocity.x = -225;
}
// sprite is on the left, blocked by the world bounds, sprite2 bounces into it
function test1() {
game.physics.gravity.y = 100;
sprite = game.add.sprite(0, 400, 'p');
sprite.body.collideWorldBounds = true;
sprite2 = game.add.sprite(700, 400, 'p');
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
track = sprite2;
game.input.onDown.add(launch1, this);
}
function launch1() {
sprite2.body.velocity.x = -225;
}
function update() {
game.physics.collide(sprite, sprite2);
// sprite.rotation = sprite.body.angle;
if (sprite)
{
bmd.fillStyle('#ffff00');
bmd.fillRect(sprite.body.center.x, sprite.body.center.y, 2, 2);
}
if (sprite2)
{
bmd.fillStyle('#ff00ff');
bmd.fillRect(sprite2.body.center.x, sprite2.body.center.y, 2, 2);
}
}
function render() {
if (sprite)
{
game.debug.renderBodyInfo(sprite, 16, 24);
}
if (sprite2)
{
game.debug.renderBodyInfo(sprite2, 16, 190);
}
}
+82
View File
@@ -0,0 +1,82 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('coke', 'assets/sprites/cokecan.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('ball', 'assets/sprites/shinyball.png');
}
var bmd;
function create() {
bmd = game.add.bitmapData(32, 64);
console.log(bmd);
// And apply it to 100 randomly positioned sprites
for (var i = 0; i < 100; i++)
{
game.add.sprite(game.world.randomX - 32, game.world.randomY - 64, bmd);
}
// Populate the wave with some data
waveData = game.math.sinCosGenerator(32, 8, 8, 2);
}
function update() {
bmd.clear();
updateWobblyBall();
}
// This creates a simple sine-wave effect running through our DynamicTexture.
// This is then duplicated across all sprites using it, meaning we only have to calculate it once.
var waveSize = 8;
var wavePixelChunk = 2;
var waveData;
var waveDataCounter;
function updateWobblyBall()
{
var s = 0;
var copyRect = { x: 0, y: 0, w: wavePixelChunk, h: 32 };
var copyPoint = { x: 0, y: 0 };
for (var x = 0; x < 32; x += wavePixelChunk)
{
copyPoint.x = x;
copyPoint.y = waveSize + (waveSize / 2) + waveData.sin[s];
bmd.context.drawImage(game.cache.getImage('ball'), copyRect.x, copyRect.y, copyRect.w, copyRect.h, copyPoint.x, copyPoint.y, copyRect.w, copyRect.h);
copyRect.x += wavePixelChunk;
s++;
}
// Cycle through the wave data - this is what causes the image to "undulate"
game.math.shift(waveData.sin);
waveDataCounter++;
if (waveDataCounter == waveData.length)
{
waveDataCounter = 0;
}
}
function render() {
bmd.render();
}
+51
View File
@@ -0,0 +1,51 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('coke', 'assets/sprites/cokecan.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('ball', 'assets/sprites/shinyball.png');
}
var bmd;
function create() {
bmd = game.add.bitmapData(800, 600);
console.log('isLittleEndian', bmd.isLittleEndian);
bmd.isLittleEndian = false;
bmd.beginPath();
bmd.beginLinearGradientFill(["#ff0000", "#ffff00"], [0, 1], 0, 0, 0, 600);
bmd.rect(0, 0, 800, 600);
bmd.closePath();
bmd.fill();
bmd.refreshBuffer();
for (var i = 0; i < 100; i++)
{
bmd.setPixel(game.world.randomX, game.world.randomY, 0, 0, 0);
}
var d = game.add.sprite(0, 0, bmd);
}
function update() {
bmd.refreshBuffer();
bmd.setPixel(game.input.x, game.input.y, 0, 0, 0);
}
function render() {
bmd.render();
}
+74
View File
@@ -0,0 +1,74 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
}
var bmd;
var img;
function create() {
var canvas = Phaser.Canvas.create(800, 600);
var context = canvas.getContext('2d');
context.drawImage(game.cache.getImage('atari1'), 0, 0);
img = context.getImageData(0, 0, 800, 600);
var data8 = new Uint8ClampedArray(img.data.buffer);
var data32 = new Uint32Array(img.data.buffer);
context.drawImage(game.cache.getImage('atari1'), 32, 50);
img = context.getImageData(0, 0, 800, 600);
// console.log(data32[y * 800 + x]);
var alpha = 255;
var blue = 50;
var red = 50;
var green = 100;
for (var y = 0; y < 100; y++)
{
for (var x = 0; x < 250; x++)
{
// var value = x * y & 0xff;
// console.log(data32[y * 800 + x]);
// data32[((y + 100) * 800) + x] = 0;
// data32[y * 800 + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;;
// data32[y * 800 + x] = (red << 24) | (green << 16) | (blue << 8) | alpha;
data32[y * 800 + (x + 300)] = data32[y * 800 + x];
}
}
// if (this.isLittleEndian)
// {
// this.data32[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
// }
// else
// {
// this.data32[y * this.width + x] = (red << 24) | (green << 16) | (blue << 8) | alpha;
// }
img.data.set(data8);
context.putImageData(img, 0, 0);
document.getElementById('phaser-example').appendChild(canvas);
}
function update() {
}
function render() {
}
+50
View File
@@ -0,0 +1,50 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('arrow', 'assets/sprites/longarrow.png');
}
var sprite;
var cursors;
function create() {
game.stage.backgroundColor = '#124184';
sprite = game.add.sprite(400, 300, 'arrow');
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
sprite.body.angularVelocity = 0;
if (cursors.left.isDown)
{
sprite.body.angularVelocity = -100;
}
else if (cursors.right.isDown)
{
sprite.body.angularVelocity = 100;
}
if (cursors.up.isDown)
{
game.physics.velocityFromAngle(sprite.angle, 300, sprite.body.velocity);
}
}
function render() {
game.debug.renderBodyInfo(sprite, 16, 24);
}
+336
View File
@@ -0,0 +1,336 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('bullet', 'assets/games/invaders/bullet.png');
game.load.image('enemyBullet', 'assets/games/invaders/enemy-bullet.png');
game.load.spritesheet('invader', 'assets/games/invaders/invader32x32x4.png', 32, 32);
game.load.image('ship', 'assets/games/invaders/player.png');
game.load.spritesheet('kaboom', 'assets/games/invaders/explode.png', 128, 128);
game.load.image('starfield', 'assets/games/invaders/starfield.png');
game.load.image('background', 'assets/games/starstruck/background2.png');
}
var player;
var aliens;
var bullets;
var bulletTime = 0;
var cursors;
var fireButton;
var explosions;
var starfield;
var score = 0;
var scoreString = '';
var scoreText;
var lives;
var enemyBullet;
var firingTimer = 0;
var stateText;
var livingEnemies = [];
var bob;
function create() {
$('#step').click(function(){
console.log('---- STEP', game.stepCount, '-------------------------------');
game.step();
});
game.enableStep();
// The scrolling starfield background
// starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');
// Our bullet group
// bullets = game.add.group();
// bullets.createMultiple(30, 'bullet');
// bullets.setAll('anchor.x', 0.5);
// bullets.setAll('anchor.y', 1);
// bullets.setAll('outOfBoundsKill', true);
// The enemy's bullets
// enemyBullets = game.add.group();
// enemyBullets.createMultiple(30, 'enemyBullet');
// enemyBullets.setAll('anchor.x', 0.5);
// enemyBullets.setAll('anchor.y', 1);
// enemyBullets.setAll('outOfBoundsKill', true);
// The hero!
// player = game.add.sprite(400, 500, 'ship');
// player.anchor.setTo(0.5, 0.5);
// The baddies!
aliens = game.add.group();
createAliens();
// The score
// scoreString = 'Score : ';
// scoreText = game.add.text(10, 10, scoreString + score, { fontSize: '34px', fill: '#fff' });
// Lives
// lives = game.add.group();
// game.add.text(game.world.width - 100, 10, 'Lives : ', { fontSize: '34px', fill: '#fff' });
// Text
// stateText = game.add.text(game.world.centerX,game.world.centerY,'', { fontSize: '84px', fill: '#fff' });
// stateText.anchor.setTo(0.5, 0.5);
// stateText.visible = false;
// for (var i = 0; i < 3; i++)
// {
// var ship = lives.create(game.world.width - 100 + (30 * i), 60, 'ship');
// ship.anchor.setTo(0.5, 0.5);
// ship.angle = 90;
// ship.alpha = 0.4;
// }
// An explosion pool
// explosions = game.add.group();
// explosions.createMultiple(30, 'kaboom');
// explosions.forEach(setupInvader, this);
// And some controls to play the game with
cursors = game.input.keyboard.createCursorKeys();
// fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
}
function createAliens () {
/*
for (var y = 0; y < 4; y++)
{
for (var x = 0; x < 10; x++)
{
var alien = aliens.create(x * 48, y * 50, 'invader');
alien.anchor.setTo(0.5, 0.5);
alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true);
alien.play('fly');
}
}
*/
bob = game.add.sprite(32, 32, 'invader');
// bob = aliens.create(48, 50, 'invader');
bob.debug = true;
aliens.x = 100;
aliens.y = 50;
// All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly.
// var tween = game.add.tween(aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
// When the tween completes it calls descend, before looping again
// tween.onComplete.add(descend, this);
}
function setupInvader (invader) {
invader.anchor.x = 0.5;
invader.anchor.y = 0.5;
invader.animations.add('kaboom');
}
function descend() {
console.log('descend');
aliens.y += 10;
}
function update() {
// aliens.x += 1;
bob.body.velocity.x = 100;
// bob.body.velocity.y = 100;
// Scroll the background
// starfield.tilePosition.y += 2;
// Reset the player, then check for movement keys
/*
player.body.velocity.setTo(0, 0);
if (cursors.left.isDown)
{
player.body.velocity.x = -200;
}
else if (cursors.right.isDown)
{
player.body.velocity.x = 200;
}
// Firing?
if (fireButton.isDown)
{
fireBullet();
}
if (game.time.now > firingTimer)
{
enemyFires();
}
// Run collision
game.physics.collide(bullets, aliens, collisionHandler, null, this);
game.physics.collide(enemyBullets, player, enemyHitsPlayer, null, this);
*/
}
/*
function collisionHandler (bullet, alien) {
// When a bullet hits an alien we kill them both
bullet.kill();
alien.kill();
// Increase the score
score += 20;
scoreText.content = scoreString + score;
// And create an explosion :)
var explosion = explosions.getFirstDead();
explosion.reset(alien.body.x, alien.body.y);
explosion.play('kaboom', 30, false, true);
if (aliens.countLiving() == 0)
{
score += 1000;
scoreText.content = scoreString + score;
enemyBullets.callAll('kill',this);
stateText.content = " You Won, \n Click to restart";
stateText.visible = true;
//the "click to restart" handler
game.input.onTap.addOnce(restart,this);
}
}
function enemyHitsPlayer (player,bullet) {
bullet.kill();
live = lives.getFirstAlive();
if (live)
{
live.kill();
}
// And create an explosion :)
var explosion = explosions.getFirstDead();
explosion.reset(player.body.x, player.body.y);
explosion.play('kaboom', 30, false, true);
// When the player dies
if (lives.countLiving() < 1)
{
player.kill();
enemyBullets.callAll('kill');
stateText.content=" GAME OVER \n Click to restart";
stateText.visible = true;
//the "click to restart" handler
game.input.onTap.addOnce(restart,this);
}
}
function enemyFires () {
// Grab the first bullet we can from the pool
enemyBullet = enemyBullets.getFirstExists(false);
livingEnemies.length=0;
aliens.forEachAlive(function(alien){
// put every living enemy in an array
livingEnemies.push(alien);
});
if (enemyBullet && livingEnemies.length > 0)
{
var random=game.rnd.integerInRange(0,livingEnemies.length);
// randomly select one of them
var shooter=livingEnemies[random];
// And fire the bullet from this enemy
enemyBullet.reset(shooter.body.x, shooter.body.y);
game.physics.moveToObject(enemyBullet,player,120);
firingTimer = game.time.now + 2000;
}
}
function fireBullet () {
// To avoid them being allowed to fire too fast we set a time limit
if (game.time.now > bulletTime)
{
// Grab the first bullet we can from the pool
bullet = bullets.getFirstExists(false);
if (bullet)
{
// And fire it
bullet.reset(player.x, player.y + 8);
bullet.body.velocity.y = -400;
bulletTime = game.time.now + 200;
}
}
}
function resetBullet (bullet) {
// Called if the bullet goes out of the screen
bullet.kill();
}
function restart () {
// A new level starts
//resets the life count
lives.callAll('revive');
// And brings the aliens back from the dead :)
aliens.removeAll();
createAliens();
//revives the player
player.revive();
//hides the text
stateText.visible = false;
}
*/
function render() {
for (var i = 0; i < aliens._container.children.length; i++)
{
// game.debug.renderPhysicsBody(aliens._container.children[i].body);
}
game.debug.renderPhysicsBody(bob.body);
}
+48
View File
@@ -0,0 +1,48 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
}
var sprite;
var sprite2;
function create() {
game.stage.backgroundColor = '#124184';
// Here we're tweening the scale of the sprite, which translates to the scale of the Body as well
// The collision will carry on working even against the scaled body.
sprite = game.add.sprite(200, 300, 'gameboy', 2);
sprite.name = 'green';
// sprite.anchor.setTo(0.5, 0.5);
sprite.body.immovable = true;
sprite2 = game.add.sprite(600, 300, 'gameboy', 3);
sprite2.name = 'yellow';
sprite2.body.rebound = false;
// sprite2.body.velocity.x = -200;
game.add.tween(sprite2).to( { x: 0 }, 3000, Phaser.Easing.Linear.None, true);
// game.add.tween(sprite.scale).to( { x: 3, y: 3 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
}
function update() {
game.physics.collide(sprite, sprite2);
}
function render() {
game.debug.renderPolygon(sprite.body.polygons, 'rgb(255,0,0)');
game.debug.renderPolygon(sprite2.body.polygons, 'rgb(255,0,0)');
}
+77
View File
@@ -0,0 +1,77 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('coke', 'assets/sprites/cokecan.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var group;
var group2;
var atari;
var coke;
var dropper;
function create() {
// make our world wider
game.world.setBounds(0, 0, 2000, 600);
// shift the camera (still works)
// game.camera.x = 400;
game.world.angle = 10;
// base group
group = game.add.group();
// group.angle = 10;
group.y = 200;
atari = group.create(0, 0, 'atari1');
coke = group.create(340, 0, 'coke');
// dropper group
group2 = game.add.group();
dropper = group2.create(400, 0, 'mushroom');
dropper.inputEnabled = true;
dropper.input.enableDrag();
dropper.events.onDragStop.add(checkDrop, this);
group2.y = 400;
}
function checkDrop() {
if (Phaser.Rectangle.intersects(dropper.body, atari.body))
{
atari.alpha = 0.3;
}
else if (Phaser.Rectangle.intersects(dropper.body, coke.body))
{
coke.alpha = 0.3;
}
}
function update() {
group.x += 0.5;
}
function render() {
game.debug.renderWorldTransformInfo(dropper, 32, 32);
game.debug.renderText(dropper.position.x, 200, 32);
game.debug.renderText(dropper.position.y, 260, 32);
game.debug.renderRectangle(atari.body, 'rgba(255,0,0,0.3)');
game.debug.renderRectangle(coke.body, 'rgba(255,0,0,0.3)');
game.debug.renderRectangle(dropper.body, 'rgba(0,255,0,0.3)');
}
+55
View File
@@ -0,0 +1,55 @@
GameState = function (game) {
this.game = game;
};
GameState.prototype = Object.create(Phaser.State.prototype);
GameState.prototype.constructor = GameState;
GameState.prototype.preload = function preload() {
this.game.load.image('bunny', 'assets/sprites/bunny.png');
this.game.load.image('carrot', 'assets/sprites/carrot.png');
this.game.load.image('melon', 'assets/sprites/melon.png');
}
GameState.prototype.create = function () {
this.game.world.setBounds(0, 0, 1600, 600);
this.bunny = this.game.add.sprite(200, 200, 'bunny');
this.bunny.scale.setTo(0.2, 0.2);
var melon = this.game.add.sprite(0, 0, 'melon');
this.melonGroup = this.game.add.group(null, 'melonGroup');
this.melonGroup.add(melon);
melon.x = this.bunny.x;
melon.y = this.bunny.y - 40;
this.carrot = this.game.add.sprite(0, 0, 'carrot');
this.game.camera.follow(this.bunny);
}
GameState.prototype.update = function () {
this.bunny.body.velocity.x = 0;
if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
this.bunny.body.velocity.x = 500;
}
if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
this.bunny.body.velocity.x = -500;
}
var melon = this.melonGroup.getFirstExists(true);
melon.x = this.bunny.x;
melon.y = this.bunny.y - 40;
this.carrot.x = this.bunny.x;
this.carrot.y = this.bunny.y - 20;
};
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example');
game.state.add('state', GameState, true);
})();
+115
View File
@@ -0,0 +1,115 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
function preload() {
// Enable scaling
// game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL;
// game.stage.scale.maxWidth = 1500;
// game.stage.scale.maxHeight = 1000;
// game.stage.scale.startFullScreen();
// game.stage.scale.refresh();
game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);
game.load.image('sky0','assets/skies/space2.png');
game.load.image('sky1','assets/skies/cavern1.png');
game.load.image('sky2','assets/skies/chrome.png');
game.load.image('sky3','assets/skies/fire.png');
game.load.image('sky4','assets/skies/fog.png');
game.load.image('sky5','assets/skies/sky1.png');
game.load.image('sky6','assets/skies/toxic.png');
}
var background;
var button1;
var button2;
var button3;
var button4;
var button5;
var button6;
function create() {
console.log('create!');
background = game.add.sprite(0, 0, 'sky0');
background.name = 'background';
// Standard button (also used as our pointer tracker)
button1 = game.add.button(100, 100, 'button', changeSky, this, 2, 1, 0);
button1.name = 'sky1';
button1.anchor.setTo(0.5, 0.5);
// Rotated button
button2 = game.add.button(330, 200, 'button', changeSky, this, 2, 1, 0);
button2.name = 'sky2';
button2.angle = 24;
button2.anchor.setTo(0.5, 0.5);
// Width scaled button
button3 = game.add.button(100, 300, 'button', changeSky, this, 2, 1, 0);
button3.name = 'sky3';
button3.width = 300;
button3.anchor.setTo(0, 0.5);
// button3.angle = 0.1;
// Scaled button
button4 = game.add.button(300, 450, 'button', changeSky, this, 2, 1, 0);
button4.name = 'sky4';
button4.scale.setTo(2, 2);
// Shrunk button
button5 = game.add.button(100, 450, 'button', changeSky, this, 2, 1, 0);
button5.name = 'sky5';
button5.scale.setTo(0.5, 0.5);
// Scaled and Rotated button
button6 = game.add.button(570, 200, 'button', changeSky, this, 2, 1, 0); // anchor 0.5
button6.name = 'sky6';
button6.angle = 32;
button6.scale.setTo(2, 2);
button6.anchor.setTo(0.5, 0.5);
// works regardless of world angle, parent angle or camera position
// game.world.setBounds(0, 0, 2000, 2000);
// game.world.angle = 10;
// game.camera.x = 300;
// Phaser.Canvas.setSmoothingEnabled(false);
//game.input.onDown.add(gofull, this);
}
function gofull() {
game.stage.scale.startFullScreen(false);
}
function changeSky (button) {
background.loadTexture(button.name);
}
function render () {
game.debug.renderSpriteCorners(button1, false, true);
game.debug.renderSpriteCorners(button2, false, true);
game.debug.renderSpriteCorners(button3, false, true);
game.debug.renderSpriteCorners(button4, false, true);
game.debug.renderSpriteCorners(button5, false, true);
game.debug.renderSpriteCorners(button6, false, true);
// game.debug.renderWorldTransformInfo(button1, 32, 132);
// game.debug.renderText('sx: ' + button3.scale.x + ' sy: ' + button3.scale.y + ' w: ' + button3.width + ' cw: ' + button3._cache.width, 32, 20);
// game.debug.renderText('ox: ' + game.stage.offset.x + ' oy: ' + game.stage.offset.y, 32, 20);
// game.debug.renderPoint(button3.input._tempPoint);
// game.debug.renderPoint(button6.input._tempPoint);
game.debug.renderInputInfo(32, 32);
}
+42
View File
@@ -0,0 +1,42 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
function preload() {
game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);
game.load.image('background','assets/misc/starfield.jpg');
}
var button;
var background;
function create() {
game.stage.backgroundColor = '#182d3b';
background = game.add.tileSprite(0, 0, 800, 600, 'background');
button = game.add.button(game.world.centerX - 95, 200, 'button', actionOnClick, this, 2, 1, 0);
button.anchor.setTo(0.5, 0.5);
button.scale.setTo(2, 2);
// button.width = 100;
// button.height = 300;
button.angle = 10;
}
function actionOnClick () {
background.visible =! background.visible;
}
function render () {
game.debug.renderSpriteCorners(button);
game.debug.renderPoint(button.input._tempPoint);
}
+70
View File
@@ -0,0 +1,70 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('wizball', 'assets/sprites/wizball.png');
game.load.image('platform', 'assets/sprites/interference_tunnel.png');
}
var ball;
var circle;
var platform;
function create() {
// Our ball sprite
ball = game.add.sprite(440, 100, 'wizball');
ball.anchor.setTo(0.5, 0.5);
ball.body.customSeparateX = true;
ball.body.customSeparateY = true;
ball.body.velocity.y = 150;
ball.body.bounce.y = 0.9;
// Our collision circle
circle = new Phaser.Circle(200, 100, 188);
// Our platform
platform = game.add.sprite(200, 450, 'platform');
platform.body.immovable = true;
}
function update() {
// ball.x = game.input.x;
// ball.y = game.input.y;
circle.x = ball.x;
circle.y = ball.y;
// This is a rect vs. rect collision. The callback will check the circle.
game.physics.overlap(ball, platform, collisionCallback, processCallback, this);
}
function collisionCallback(a, b) {
ball.body.y -= 10;
ball.body.velocity.y *= -1 * ball.body.bounce.y;
}
function processCallback(a, b) {
return (Phaser.Circle.intersectsRectangle(circle, platform.body));
}
function render() {
game.debug.renderText(Phaser.Circle.intersectsRectangle(circle, platform.body), 32, 32);
game.debug.renderText(ball.body.velocity.y, 32, 64);
// game.debug.renderText(ball.body.overlapY, 64, 64);
game.debug.renderCircle(circle);
game.debug.renderRectangle(platform.body);
}
+137
View File
@@ -0,0 +1,137 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('map', 'assets/maps/commando.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('tiles', 'assets/maps/commando.png');
game.load.image('phaser', 'assets/sprites/arrow.png');
}
var cursors;
var map;
var layer;
var sprite;
function create() {
map = game.add.tilemap('map');
map.addTilesetImage('CommandoMap1-1BG_bank.png', 'tiles');
layer = map.createLayer('ShoeBox Tile Grab');
// layer.debug = true;
layer.resizeWorld();
sprite = game.add.sprite(260, 100, 'phaser');
sprite.anchor.setTo(0.5, 0.5);
sprite.body.setSize(16, 16, 8, 8);
// We'll set a lower max angular velocity here to keep it from going totally nuts
sprite.body.maxAngular = 500;
// Apply a drag otherwise the sprite will just spin and never slow down
sprite.body.angularDrag = 50;
game.camera.follow(sprite);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
/*
if (cursors.left.isDown)
{
game.camera.x -= 1;
}
else if (cursors.right.isDown)
{
game.camera.x += 1;
}
if (cursors.up.isDown)
{
layer.scrollY -= 4;
}
else if (cursors.down.isDown)
{
layer.scrollY += 4;
}
*/
game.physics.collide(sprite, layer);
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
sprite.body.angularVelocity = 0;
// sprite.body.acceleration.x = 0;
// sprite.body.angularAcceleration = 0;
if (cursors.left.isDown)
{
// sprite.body.acceleration.x = -200;
sprite.body.angularVelocity = -300;
// sprite.body.angularAcceleration -= 200;
}
else if (cursors.right.isDown)
{
// sprite.body.acceleration.x = 200;
sprite.body.angularVelocity = 300;
// sprite.body.angularAcceleration += 200;
}
if (cursors.up.isDown)
{
game.physics.velocityFromAngle(sprite.angle, 300, sprite.body.velocity);
}
else
{
// game.physics.velocityFromAngle(sprite.angle, sprite.body.velocity, sprite.body.velocity);
}
/*
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
sprite.angle = sprite.angle + 1;
if (cursors.up.isDown)
{
sprite.body.velocity.y = -900;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 900;
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -900;
// sprite.scale.x = -1;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 900;
// sprite.scale.x = 1;
}
*/
}
function render() {
// game.debug.renderSpriteBody(sprite);
// game.debug.renderSpriteBounds(sprite);
// game.debug.renderText(sprite.x, 32, 32);
// game.debug.renderText(sprite.y, 32, 48);
// game.debug.renderText(layer.scrollX, 32, 32);
// game.debug.renderText(layer.scrollY, 32, 48);
}
+234
View File
@@ -0,0 +1,234 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
// game.load.tilemap('map', 'assets/tilemaps/maps/platform.json', null, Phaser.Tilemap.TILED_JSON);
// game.load.image('platformer_tiles', 'assets/tilemaps/tiles/platformer_tiles.png');
game.load.tilemap('map', 'assets/tilemaps/maps/features_test.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
game.load.image('walls_1x2', 'assets/tilemaps/tiles/walls_1x2.png');
game.load.image('tiles2', 'assets/tilemaps/tiles/tiles2.png');
}
var map;
var layer;
var mapLayer;
var polys = [];
var tiles = [];
var idx = 0;
var group = [];
function create() {
game.stage.backgroundColor = '#124184';
// map = game.add.tilemap('map');
// map.addTilesetImage('platformer_tiles');
// map.setCollisionBetween(21, 53);
// layer = map.createLayer('Tile Layer 1');
// layer.debug = true;
map = game.add.tilemap('map');
map.addTilesetImage('ground_1x1');
map.addTilesetImage('walls_1x2');
map.addTilesetImage('tiles2');
map.setCollisionBetween(1, 12);
layer = map.createLayer('Tile Layer 1');
mapLayer = map.layers[0];
tiles = getInterestingTiles();
console.log('found',tiles.length,'interesting tiles');
// group = [];
// var tile = getFirstUnscanned();
// console.log('starting from', tile);
// floodFill(tile);
// console.log('group collected, size', group.length);
// now scan the group
// scanEdges();
// works great :)
while (getScannedLeft() > 0) {
// group = [];
var tile = getFirstUnscanned();
console.log('starting from', tile);
floodFill(tile);
console.log('group collected, size', group.length);
console.log('remaining: ', getScannedLeft());
}
}
function getFirstUnscanned() {
for (var i = 0; i < tiles.length; i++)
{
if (!tiles[i].scanned)
{
return tiles[i];
}
}
}
function getScannedLeft() {
var total = tiles.length;
for (var i = 0; i < tiles.length; i++)
{
if (tiles[i].scanned)
{
total--;
}
}
return total;
}
var current;
function scanEdges() {
var points = [];
var start = group[0];
// special case for when group.length = 1 should go here
}
function scanTop(origin) {
}
function floodFill(origin) {
if (origin.scanned === true)
{
return;
}
origin.scanned = true;
group.push(origin);
// console.log('ff origin', origin.x, origin.y);
var west = map.getTileLeft(0, origin.x, origin.y);
if (west && (west.faceTop || west.faceBottom || west.faceLeft || west.faceRight))
{
// console.log('west found', west);
floodFill(west);
}
var east = map.getTileRight(0, origin.x, origin.y);
if (east && (east.faceTop || east.faceBottom || east.faceLeft || east.faceRight))
{
// console.log('east found', east);
floodFill(east);
}
var north = map.getTileAbove(0, origin.x, origin.y);
if (north && (north.faceTop || north.faceBottom || north.faceLeft || north.faceRight))
{
// console.log('north found', north);
floodFill(north);
}
var south = map.getTileBelow(0, origin.x, origin.y);
if (south && (south.faceTop || south.faceBottom || south.faceLeft || south.faceRight))
{
// console.log('south found', south);
floodFill(south);
}
}
function getInterestingTiles() {
var tiles = [];
for (var y = 0, h = mapLayer.height; y < h; y++)
{
for (var x = 0, w = mapLayer.width; x < w; x++)
{
var tile = mapLayer.data[y][x];
if (tile && (tile.faceTop || tile.faceBottom || tile.faceLeft || tile.faceRight))
{
// reset the scanned status
tile.scanned = false;
tiles.push(tile);
}
}
}
return tiles;
}
function getTile() {
for (var y = 0, h = mapLayer.height; y < h; y++)
{
for (var x = 0, w = mapLayer.width; x < w; x++)
{
var tile = mapLayer.data[y][x];
if (tile && (tile.faceTop || tile.faceBottom || tile.faceLeft || tile.faceRight))
{
return tile;
}
}
}
}
function update() {
}
function render() {
if (group)
{
game.context.fillStyle = 'rgba(255,0,0,0.7)';
for (var i = 0; i < group.length; i++)
{
game.context.fillRect(group[i].x * group[i].width, group[i].y * group[i].height, group[i].width, group[i].height);
}
}
}
+24
View File
@@ -0,0 +1,24 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
}
var atari;
function create() {
atari = game.add.sprite(300, 300, 'atari1');
// atari.crop = new Phaser.Rectangle(0, 0, 10, atari.height);
}
function update() {
atari.crop.width -= 1;
}
+55
View File
@@ -0,0 +1,55 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('atari2', 'assets/sprites/atari800xl.png');
game.load.image('atari4', 'assets/sprites/atari800.png');
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
game.load.image('duck', 'assets/sprites/darkwing_crazy.png');
game.load.image('firstaid', 'assets/sprites/firstaid.png');
game.load.image('diamond', 'assets/sprites/diamond.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var group;
function create() {
group = game.add.group();
var sprite1 = group.create(0, 200, 'atari1');
sprite1.name = 'Atari 1';
var sprite2 = group.create(300, 200, 'atari2');
sprite2.name = 'Atari 2';
var sprite3 = group.create(600, 200, 'atari4');
sprite3.name = 'Atari 3';
game.input.onDown.add(moveCursor, this);
group.dump(false);
}
function moveCursor () {
group.next();
group.dump(false);
}
function update() {
}
function render() {
game.debug.renderText(group.cursor.name, 32, 32);
// game.debug.renderInputInfo(32, 32);
}
+79
View File
@@ -0,0 +1,79 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('ball', 'assets/sprites/shinyball.png');
}
var sprite;
var particles = [];
var bmd;
var u = 0;
var n = 0;
var oldn = 0;
var ad = 0;
function create() {
bmd = game.add.bitmapData(800, 600);
for (var i = 0; i < 60; i++)
{
particles.push(new Phaser.Point(0, 0));
}
sprite = game.add.sprite(0, 0, bmd);
}
function mycircle(context, x, y, R, color) {
//R = 64;
context.fillStyle = color;
context.beginPath();
context.arc(x, y, R, 0, Math.PI * 2, true);
context.closePath();
context.fill();
}
function update() {
bmd.clear();
oldn = n;
for (var t = 0; t < particles.length; t++)
{
var p = particles[t];
// p.x = Math.sin(n) * 50 + Math.cos(n * 1.5) * 300;
// p.y = Math.sin(n / 2) * 20 + Math.sin(n * 2) * 250;
p.x = Math.cos(n) * 50 + Math.sin(n * 1.5) * 300;
p.y = Math.cos(n / 2) * 20 + Math.cos(n * 2) * 250;
var tx = p.x;
var ty = p.y;
bmd.context.globalCompositeOperation = 'xor';
//mycircle(bmd.context, p.x + 400, p.y + 300, Math.sin(t * 360 / particles.length / 2 * Math.PI / 180) * 50, 'rgba(255, 255, 0, 1)');
mycircle(bmd.context, p.x + 400, p.y + 300, Math.sin(t * 360 / particles.length / 2 * Math.PI / 180) * 50, 'rgba(255, 255, 0, 1)');
n += 0.05;
//bmd.context.globalCompositeOperation = 'source-over';
}
n = oldn + 0.02;
}
function render() {
}
+53
View File
@@ -0,0 +1,53 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
var background;
var filter;
function preload() {
game.load.spritesheet('dragon', 'assets/sprites/stormlord-dragon96x64.png', 96, 64);
game.load.image('sky', 'assets/skies/sky2.png');
game.load.image('land', 'assets/wip/karamoon.png');
game.load.script('fire', '../filters/Fire.js');
}
var sky;
var background;
var fire;
var dragon;
function create() {
sky = game.add.sprite(0, 600, 'sky');
sky.scale.y = -1;
sky.alpha = 0.5;
fire = game.add.sprite(0, 200);
fire.width = 800;
fire.height = 400;
background = game.add.sprite(0, 0, 'land');
dragon = game.add.sprite(100, 300, 'dragon');
dragon.animations.add('flyright', [0,1,2,3,4,5], 10, true);
dragon.play('flyright');
filter = game.add.filter('Fire', 800, 400);
filter.alpha = 0.0;
fire.filters = [filter];
}
function update() {
filter.update();
}
function render() {
// filter.update();
}
+29
View File
@@ -0,0 +1,29 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.image('ball', 'assets/sprites/yellow_ball.png');
}
var ball;
function create() {
ball = game.add.sprite(300, 0, 'ball');
TweenLite.to(ball, 10, { y: 400 });
}
function startBounceTween() {
// ball.y = 0;
// var bounce=game.add.tween(ball);
// bounce.to({ y: game.world.height-ball.height }, 1000 + Math.random() * 3000, Phaser.Easing.Bounce.In);
// bounce.onComplete.add(startBounceTween, this);
// bounce.start();
}
+110
View File
@@ -0,0 +1,110 @@
<?php
function getTabs() {
global $tabs;
$s = "";
for ($t = 0; $t < $tabs; $t++)
{
$s = $s . "\t";
}
return $s;
}
$pixi = Array();
$raw = 'Paste ShaderToy code in here';
$output = "this.fragmentSrc = [\n";
$output .= "\t\"precision mediump float;\",\n";
$output .= "\t\"uniform vec3 iResolution;\",\n";
$output .= "\t\"uniform float iGlobalTime;\",\n";
$output .= "\t\"uniform float iChannelTime[4];\",\n";
$output .= "\t\"uniform vec4 iMouse;\",\n";
$output .= "\t\"uniform vec4 iDate;\",\n";
$output .= "\t\"uniform vec3 iChannelResolution[4];\",\n";
$output .= "\t\"uniform sampler2D iChannel0;\",\n";
$output .= "\t\"// add any extra uniforms here\",\n";
$output .= "\n";
$tabs = 1;
if (isset($_POST['shader']))
{
$shader = explode("\n", $_POST['shader']);
$raw = urldecode($_POST['shader']);
for ($i = 0; $i < count($shader); $i++)
{
$pixi[] = trim($shader[$i]);
}
// Tabbing
for ($i = 0; $i < count($pixi); $i++)
{
if ($pixi[$i] == '{')
{
$output = $output . getTabs() . '"' . $pixi[$i] . "\",\n";
$tabs++;
continue;
}
else if ($pixi[$i] == '}')
{
$tabs--;
}
if ($pixi[$i] == '')
{
$output = $output . "\n";
}
else
{
$output = $output . getTabs() . '"' . $pixi[$i] . "\",\n";
}
}
$output = rtrim($output);
if (substr($output, -1) == ',')
{
$output = substr($output, 0, -1);
}
}
$output .= "];";
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>phaser - ShaderToy Convertor</title>
<style>
body {
font-family: Arial;
font-size: 14px;
}
</style>
</head>
<body>
<h1>ShaderToy to PIXI Convertor</h1>
<form action="filterconv.php" method="post">
<h2>Input</h2>
<textarea name="shader" style="width: 800px; height: 200px"><?php echo $raw ?></textarea>
<h2>Output</h2>
<textarea style="width: 800px; height: 400px"><?php echo $output ?></textarea>
<br />
<input type="submit" value="Convert" />
</form>
</body>
</html>
+72
View File
@@ -0,0 +1,72 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.stage.backgroundColor = '#007236';
game.load.image('ball', 'assets/sprites/shinyball.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('phaser', 'assets/sprites/sonic_havok_sanity.png');
}
var sprite;
var cursors;
function create() {
game.world.setBounds(0, 0, 1000, 1000);
for (var i = 0; i < 100; i++)
{
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
}
sprite = game.add.sprite(200, 200, 'ball');
sprite.fixedToCamera = true;
// sprite.cameraOffset.setTo(200, 200);
sprite.inputEnabled = true;
sprite.input.enableDrag();
sprite.events.onInputOver.add(wibble, this);
cursors = game.input.keyboard.createCursorKeys();
}
function wibble() {
console.log('over');
}
function update() {
if (cursors.up.isDown)
{
game.camera.y -= 4;
}
else if (cursors.down.isDown)
{
game.camera.y += 4;
}
if (cursors.left.isDown)
{
game.camera.x -= 4;
}
else if (cursors.right.isDown)
{
game.camera.x += 4;
}
}
function render() {
game.debug.renderText(sprite.cameraOffset.x, 32, 32);
game.debug.renderText(sprite.cameraOffset.y, 32, 64);
game.debug.renderPointer(game.input.activePointer);
}
+47
View File
@@ -0,0 +1,47 @@
var mainMenu = {
preload: function () {
// game.load.image('bg', 'assets/bg.png');
// game.load.image('mainstart', 'assets/mainstart.png');
game.load.image('bg', 'assets/pics/louie-inga.png');
game.load.image('mainstart', 'assets/pics/contra2.png');
},
create: function () {
game.add.sprite(0, 0, 'bg');
var mainstart = game.add.sprite(0, 0, 'mainstart');
mainstart.name = "mainstart";
mainstart.inputEnabled = true;
mainstart.events.onInputDown.add(this.listener, this);
},
listener: function ()
{
game.state.start('levelMenu', true, true);
}
}
var levelSelect = {
preload: function () {
},
create: function () {
game.add.sprite(0, 0, 'bg');
}
}
var game = new Phaser.Game(640, 480);
game.state.add('menu', mainMenu, true);
game.state.add('levelMenu', levelSelect);
+14
View File
@@ -0,0 +1,14 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
}
function create() {
}
function update() {
}
function render() {
}
+39
View File
@@ -0,0 +1,39 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('arrow', 'assets/sprites/arrow.png');
}
var sprite;
var setGravityToPointXY = function(game,sprite,power, radiusLow,radiusHigh,pointX,pointY){
var pointXY = new Phaser.Point(pointX,pointY);
var tempDistance = Phaser.Point.distance(sprite,pointXY);
if(tempDistance>radiusLow&&tempDistance<radiusHigh){
var rotation = game.physics.angleBetween(sprite,pointXY);
//console.log(" ROTATION " + rotation);
sprite.body.gravity.y = Math.round(Math.sin(rotation)*1000)/1000 * (power);
sprite.body.gravity.x = Math.round(Math.cos(rotation)*1000)/1000 * (power);
}
};
function create() {
game.stage.backgroundColor = '#0072bc';
sprite = game.add.sprite(100, 100, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
sprite.body.canSleep = false;
}
function update() {
setGravityToPointXY(game,sprite,20,-0,8000,100,300);
}
function render() {
game.debug.renderBodyInfo(sprite,30,32);
}
+31
View File
@@ -0,0 +1,31 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.atlas('seacreatures', 'assets/sprites/seacreatures_json.png', 'assets/sprites/seacreatures_json.json');
game.load.image('undersea', 'assets/pics/undersea.jpg');
game.load.image('coral', 'assets/pics/seabed.png');
}
function create() {
game.add.sprite(0, 0, 'undersea');
var group = game.add.group();
for (var i = 0; i < 6; i++)
{
sprite = group.create(120 * i, game.rnd.integerInRange(100, 400), 'seacreatures', 'octopus0000');
}
var frameNames = Phaser.Animation.generateFrameNames('octopus', 0, 24, '', 4);
group.callAll('animations.add', 'animations', 'swim', frameNames, 30, true, false);
group.callAll('play', null, 'swim');
game.add.sprite(0, 466, 'coral');
}
+54
View File
@@ -0,0 +1,54 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('atari2', 'assets/sprites/atari800xl.png');
game.load.image('atari4', 'assets/sprites/atari800.png');
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
game.load.image('duck', 'assets/sprites/darkwing_crazy.png');
game.load.image('firstaid', 'assets/sprites/firstaid.png');
game.load.image('diamond', 'assets/sprites/diamond.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var group;
var sprite;
function create() {
var images = game.cache.getImageKeys();
group = game.add.group();
for (var i = 0; i < 20; i++)
{
sprite = group.create(game.world.randomX, game.world.randomY, game.rnd.pick(images));
}
sprite.x = 100;
sprite.y = 100;
game.add.tween(sprite).to( { y: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
game.input.onDown.addOnce(nuke, this);
}
function nuke() {
// The optional parameter here will destroy the Sprites as well as the Group.
// The default is 'false' which means destroy the Group, but none of the children.
group.destroy(true);
console.log(group);
console.log(sprite);
}
function render() {
game.debug.renderText('Click to nuke', 32, 32);
}
+47
View File
@@ -0,0 +1,47 @@
// Here is a custom group with a property 'key'
// This isn't required, it's just an example of a custom group level property. Only 'game' is required.
MonsterGroup = function (game, key) {
Phaser.Group.call(this, game);
this.key = key;
};
MonsterGroup.prototype = Object.create(Phaser.Group.prototype);
MonsterGroup.prototype.constructor = MonsterGroup;
/**
* Generate some monsters on request, all spaced out evenly
*/
MonsterGroup.prototype.make = function(qty, x, y) {
for (var i = 0; i < qty; i++)
{
this.create(x, y, this.key);
x += 64;
}
}
// Boilerplate code below
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.image('bunny', 'assets/sprites/bunny.png');
game.load.image('ball', 'assets/sprites/shinyball.png');
}
function create() {
var monsters = new MonsterGroup(game, 'bunny');
var balls = new MonsterGroup(game, 'ball');
monsters.make(6, 100, 100);
balls.make(10, 64, 500);
}
+38
View File
@@ -0,0 +1,38 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
function preload() {
// game.load.image('atari1', 'assets/sprites/atari130xe.png');
// game.load.image('atari2', 'assets/sprites/atari800xl.png');
// game.load.image('atari4', 'assets/sprites/atari800.png');
// game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
// game.load.image('duck', 'assets/sprites/darkwing_crazy.png');
// game.load.image('firstaid', 'assets/sprites/firstaid.png');
// game.load.image('diamond', 'assets/sprites/diamond.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var group;
var sprite;
function create() {
var images = game.cache.getImageKeys();
group = game.add.group();
// for (var i = 0; i < 20; i++)
// {
sprite = group.create(200, game.world.randomY, game.rnd.pick(images));
// }
group.x = 200;
}
function render() {
game.debug.renderText('sx: ' + sprite.x + ' wx: ' + sprite.world.x, 32, 32);
}
+39
View File
@@ -0,0 +1,39 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('coke', 'assets/sprites/cokecan.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var dropper;
function create() {
dropper = game.add.sprite(200, 400, 'mushroom');
dropper.inputEnabled = true;
dropper.input.enableDrag();
dropper.events.onDragStart.add(prepareToKill, this);
}
function prepareToKill() {
console.log('3 sec warning');
var t = { time: 0 };
var tween = game.add.tween(t).to({time: 1}, 3000, Phaser.Easing.Linear.None, true);
tween.onComplete.add(nukeIt, this);
}
function nukeIt() {
console.log('nuked');
dropper.destroy();
}
+340
View File
@@ -0,0 +1,340 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
game.load.image('atari', 'assets/sprites/atari130xe.png');
}
var sprite;
var sprite2;
var sprite3;
var reverse = false;
function onBeginContact(a, b) {
console.log('Begin Contact between', a.name, 'and', b.name);
}
function onEndContact(a, b) {
console.log('End Contact between', a.name, 'and', b.name);
}
function create() {
game.stage.backgroundColor = '#124184';
test8();
}
function test8() {
$('#step').click(function(){
console.log('---- STEP', game.stepCount, '-------------------------------');
game.step();
});
$('#start').click(function(){
console.log('---- START DEBUGGING -------------------------------');
game.enableStep();
sprite2.debug = true;
});
game.physics.gravity.y = 200;
sprite = game.add.sprite(300, 300, 'atari');
sprite.name = 'atari';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.2, 0.2);
sprite.body.minVelocity.setTo(5, 5);
sprite2 = game.add.sprite(350, 150, 'gameboy', 2);
sprite2.name = 'gameboy';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.8, 0.8);
sprite2.body.minVelocity.setTo(10, 10);
// sprite.debug = true;
// game.enableStep();
sprite2.events.onBeginContact.add(onBeginContact, this);
sprite2.events.onEndContact.add(onEndContact, this);
reverse = true;
// game.input.onDown.add(launch8, this);
}
function launch8() {
sprite2.body.velocity.x = -100;
sprite2.body.velocity.y = -200;
}
function test7() {
// A down into B
sprite = game.add.sprite(300, 400, 'atari');
sprite.name = 'atari';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(1, 1);
sprite.body.checkCollision.up = false;
sprite2 = game.add.sprite(350, 100, 'gameboy', 2);
sprite2.name = 'gameboy';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(1, 1);
// reverse = true;
game.input.onDown.add(launch7, this);
}
function launch7() {
sprite2.body.velocity.y = 100;
}
function test6() {
// Offset Down Collision false
sprite = game.add.sprite(100, 300, 'atari');
sprite.name = 'atari';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(1, 1);
sprite.body.checkCollision.left = false;
sprite.body.checkCollision.right = false;
sprite2 = game.add.sprite(500, 330, 'gameboy', 2);
// sprite2 = game.add.sprite(500, 530, 'gameboy', 2);
sprite2.name = 'gameboy';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(1, 1);
sprite3 = game.add.sprite(400, 100, 'gameboy', 0);
sprite3.name = 'gameboy2';
sprite3.body.collideWorldBounds = true;
sprite3.body.bounce.setTo(1, 1);
game.input.onDown.add(launch6, this);
}
function launch6() {
sprite.body.velocity.x = 100;
sprite2.body.velocity.x = -100;
sprite3.body.velocity.y = 100;
}
function test5() {
// Offset Down Collision false
sprite = game.add.sprite(360, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.bounce.setTo(0.9, 0.9);
sprite.body.checkCollision.up = false;
sprite2 = game.add.sprite(400, 200, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
// sprite2.body.checkCollision.down = false;
// sprite2.body.mass = 1;
sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch5, this);
}
function launch5() {
// sprite.body.velocity.x = 200;
// sprite.body.velocity.y = -300;
// sprite2.body.velocity.x = -200;
sprite2.body.velocity.y = 200;
}
function test4() {
// Down Collision false
sprite = game.add.sprite(400, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.9, 0.9);
sprite2 = game.add.sprite(400, 200, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.checkCollision.down = false;
// sprite2.body.mass = 1;
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch4, this);
}
function launch4() {
// sprite.body.velocity.x = 200;
sprite.body.velocity.y = -300;
// sprite2.body.velocity.x = -200;
// sprite2.body.velocity.y = -200;
}
function test3() {
// Top Collision false
sprite = game.add.sprite(400, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.up = false;
sprite2 = game.add.sprite(400, 200, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
// sprite2.body.checkCollision.left = false;
sprite2.body.bounce.setTo(0.9, 0.9);
// sprite2.body.mass = 1;
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch3, this);
}
function launch3() {
// sprite.body.velocity.x = 200;
// sprite.body.velocity.y = -300;
// sprite2.body.velocity.x = -200;
sprite2.body.velocity.y = 200;
}
function test2() {
// Left Collision false
sprite = game.add.sprite(200, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.checkCollision.right = false;
sprite.body.bounce.setTo(0.9, 0.9);
// sprite.body.linearDamping = 0;
// sprite.scale.setTo(2, 2);
// sprite.body.mass = 2;
sprite2 = game.add.sprite(500, 300, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.checkCollision.left = false;
// sprite2.body.bounce.setTo(0.9, 0.9);
// sprite2.body.mass = 1;
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch2, this);
}
function launch2() {
sprite.body.velocity.x = 200;
// sprite.body.velocity.y = -300;
// sprite2.body.velocity.x = -200;
// sprite2.body.velocity.y = -200;
}
function test1() {
// Right Collision false
sprite = game.add.sprite(200, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.right = false;
// sprite.body.bounce.setTo(0.9, 0.9);
// sprite.body.bounce.setTo(1, 1);
// sprite.body.linearDamping = 0;
// sprite.scale.setTo(2, 2);
// sprite.body.mass = 2;
sprite2 = game.add.sprite(500, 300, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
// sprite2.body.mass = 1;
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch1, this);
}
function launch1() {
sprite2.body.velocity.x = -200;
}
function update() {
if (reverse)
{
game.physics.collide(sprite2, sprite);
}
else
{
game.physics.collide(sprite, sprite2);
}
if (sprite3)
{
game.physics.collide(sprite, sprite3);
}
}
function render() {
if (sprite)
{
game.debug.renderBodyInfo(sprite, 16, 24);
game.debug.renderBodyInfo(sprite2, 16, 240);
}
if (sprite)
{
game.debug.renderPhysicsBody(sprite.body);
}
if (sprite2)
{
game.debug.renderPhysicsBody(sprite2.body);
}
if (sprite3)
{
game.debug.renderPhysicsBody(sprite3.body);
}
}
+107
View File
@@ -0,0 +1,107 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
}
var sprite;
var sprite2;
var land;
var cursors;
function create() {
game.stage.backgroundColor = '#124184';
game.physics.gravity.y = 100;
sprite = game.add.sprite(200, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.bounce.setTo(0.9, 0.9);
// sprite2 = game.add.sprite(500, 200, 'gameboy', 2);
// sprite2.name = 'green';
// sprite2.body.collideWorldBounds = true;
// sprite2.body.bounce.setTo(0.9, 0.9);
land = game.add.sprite(10, 490);
land.name = 'land';
land.body.immovable = true;
land.body.allowGravity = false;
land.body.setSize(780, 100, 0, 0);
land.body.polygons = new SAT.Polygon(new SAT.Vector(10, 490), [
new SAT.Vector(0,50),
new SAT.Vector(300,0),
new SAT.Vector(780,50),
new SAT.Vector(780,100),
new SAT.Vector(0,100),
]);
console.log(land);
// sprite.body.velocity.x = 150;
cursors = game.input.keyboard.createCursorKeys();
// game.input.onDown.add(launch, this);
}
function launch() {
sprite.body.velocity.x = 100;
sprite.body.velocity.y = -300;
sprite2.body.velocity.x = 200;
sprite2.body.velocity.y = -200;
}
function update() {
game.physics.collide(sprite, land);
sprite.body.velocity.x = 0;
// sprite.body.velocity.y = 0;
if (cursors.left.isDown)
{
sprite.body.velocity.x = -100;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 100;
}
if (cursors.up.isDown)
{
sprite.body.velocity.y = -200;
}
}
function render() {
game.debug.renderPolygon(land.body.polygons);
if (sprite)
{
game.debug.renderBodyInfo(sprite, 16, 24);
}
if (sprite)
{
game.debug.renderPolygon(sprite.body.polygons);
}
if (sprite2)
{
game.debug.renderPolygon(sprite2.body.polygons);
}
// game.debug.renderRectangle(land.body);
}
+153
View File
@@ -0,0 +1,153 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17);
}
var handle1;
var handle2;
var handle3;
var handle4;
var line1;
var line2;
function create() {
game.stage.backgroundColor = '#124184';
handle1 = game.add.sprite(100, 200, 'balls', 0);
handle1.inputEnabled = true;
handle1.input.enableDrag(true);
handle2 = game.add.sprite(400, 300, 'balls', 0);
handle2.inputEnabled = true;
handle2.input.enableDrag(true);
handle3 = game.add.sprite(200, 400, 'balls', 1);
handle3.inputEnabled = true;
handle3.input.enableDrag(true);
handle4 = game.add.sprite(500, 500, 'balls', 1);
handle4.inputEnabled = true;
handle4.input.enableDrag(true);
line1 = new Phaser.Line(handle1.x, handle1.y, handle2.x, handle2.y);
line2 = new Phaser.Line(handle3.x, handle3.y, handle4.x, handle4.y);
}
/*---------------------------------------------------------------------------
Returns an Object with the following properties:
intersects -Boolean indicating if an intersection exists.
start_inside -Boolean indicating if Point A is inside of the polygon.
end_inside -Boolean indicating if Point B is inside of the polygon.
intersections -Array of intersection Points along the polygon.
centroid -A Point indicating "center of mass" of the polygon.
"pArry" is an Array of Points.
----------------------------------------------------------------------------*/
// a = point
// b = point
// pArry = polygons array
/*
function lineIntersectPoly(A, B, pArry) {
var An =1;
var Bn =1;
var C = {x: 0, y: 0};
var D = {x: 0, y: 0};
var i = {x: 0, y: 0};
var cx=0;
var cy=0;
var result = {};
var pa = pArry.slice(); //Copy to prevent growing points when connecting ends.
pa.push(pa[0]); //Create line from last Point to beginning Point
result.intersects = false;
result.intersections=[];
result.start_inside=false;
result.end_inside=false;
var n = pa.length-1;
while(n > -1){
C.x = pa[n].x;
C.y = pa[n].y;
if(n > 0){
cx+=C.x;
cy+=C.y;
D=Point(pa[n-1])||Point(pa[0]);
i=lineIntersectLine(A,B,C,D);
if(i != null){
result.intersections.push(i);
}
if(lineIntersectLine(A,new Point(C.x+D.x,A.y),C,D) != null){
An++;
}
if(lineIntersectLine(B,new Point(C.x+D.x,B.y),C,D) != null){
Bn++;
}
}
n--;
}
if(An % 2 == 0){
result.start_inside=true;
}
if(Bn % 2 == 0){
result.end_inside=true;
}
result.centroid=new Point(cx/(pa.length-1),cy/(pa.length-1));
result.intersects = result.intersections.length > 0;
return result;
}
*/
var c = 'rgb(255,255,255)';
var p = new Phaser.Point();
function update() {
line1.fromSprite(handle1, handle2, true);
line2.fromSprite(handle3, handle4, true);
p = line1.intersects(line2, true);
if (p)
{
c = 'rgb(0,255,0)';
}
else
{
c = 'rgb(255,255,255)';
}
}
function render() {
game.debug.renderLine(line1, c);
game.debug.renderLine(line2, c);
game.debug.renderLineInfo(line1, 32, 32);
game.debug.renderLineInfo(line2, 32, 100);
if (p)
{
game.context.fillStyle = 'rgb(255,0,255)';
game.context.fillRect(p.x - 2, p.y - 2, 5, 5);
}
}
+163
View File
@@ -0,0 +1,163 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17);
}
var handle1;
var handle2;
var line;
var polygon;
function create() {
game.stage.backgroundColor = '#124184';
handle1 = game.add.sprite(100, 150, 'balls', 0);
handle1.inputEnabled = true;
handle1.input.enableDrag(true);
handle2 = game.add.sprite(400, 200, 'balls', 0);
handle2.inputEnabled = true;
handle2.input.enableDrag(true);
line = new Phaser.Line(handle1.x, handle1.y, handle2.x, handle2.y);
polygon = new SAT.Polygon(new SAT.Vector(300, 300), [new SAT.Vector(0, 50), new SAT.Vector(100, 50), new SAT.Vector(100, 0), new SAT.Vector(150, 0), new SAT.Vector(150, 150), new SAT.Vector(100, 150), new SAT.Vector(100, 100), new SAT.Vector(0, 100), new SAT.Vector(0, 50)]);
}
/*---------------------------------------------------------------------------
Returns an Object with the following properties:
intersects -Boolean indicating if an intersection exists.
start_inside -Boolean indicating if Point A is inside of the polygon.
end_inside -Boolean indicating if Point B is inside of the polygon.
intersections -Array of intersection Points along the polygon.
centroid -A Point indicating "center of mass" of the polygon.
"pArry" is an Array of Points.
----------------------------------------------------------------------------*/
function lineIntersectPoly(A, B, polygon) {
var An =1;
var Bn =1;
var C = {x: 0, y: 0};
var D = {x: 0, y: 0};
var i = {x: 0, y: 0};
var cx=0;
var cy=0;
var result = {};
var pa = [];
for (var pi = 0; pi < polygon.points.length; pi++)
{
pa.push({ x: polygon.points[pi].x + polygon.pos.x, y: polygon.points[pi].y + polygon.pos.y })
}
pa.push(pa[0]); //Create line from last Point to beginning Point
result.intersects = false;
result.intersections=[];
result.start_inside=false;
result.end_inside=false;
var n = pa.length-1;
while(n > -1){
C.x = pa[n].x;
C.y = pa[n].y;
if(n > 0){
cx+=C.x;
cy+=C.y;
D.x = pa[n-1].x || pa[0].x;
D.y = pa[n-1].y || pa[0].y;
i=Phaser.Line.intersectsPoints(A,B,C,D);
if(i != null){
result.intersections.push(i);
}
if(Phaser.Line.intersectsPoints(A,new Phaser.Point(C.x+D.x,A.y),C,D) != null){
An++;
}
if(Phaser.Line.intersectsPoints(B,new Phaser.Point(C.x+D.x,B.y),C,D) != null){
Bn++;
}
}
n--;
}
if(An % 2 == 0){
result.start_inside=true;
}
if(Bn % 2 == 0){
result.end_inside=true;
}
result.centroid=new Phaser.Point(cx/(pa.length-1),cy/(pa.length-1));
result.intersects = result.intersections.length > 0;
return result;
}
var c = 'rgb(255,255,255)';
var p = new Phaser.Point();
var result = {};
function update() {
line.fromSprite(handle1, handle2, true);
result = lineIntersectPoly(line.start, line.end, polygon);
handle1.frame = 0;
handle2.frame = 0;
if (result.intersects)
{
c = 'rgb(0,255,0)';
if (result.start_inside)
{
handle1.frame = 1;
}
if (result.end_inside)
{
handle2.frame = 1;
}
}
else
{
c = 'rgb(255,255,255)';
}
}
function render() {
game.debug.renderLine(line, c);
game.debug.renderLineInfo(line, 32, 32);
game.debug.renderPolygon(polygon);
if (result.intersects)
{
game.debug.renderText(result.intersects, 32, 100);
game.context.fillStyle = 'rgb(255,0,255)';
for (var i = 0; i < result.intersections.length; i++)
{
game.context.fillRect(result.intersections[i].x - 2, result.intersections[i].y - 2, 5, 5);
}
}
}
+46
View File
@@ -0,0 +1,46 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.tilemap('nes', 'assets/maps/mario1.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tilesNes', 'assets/maps/mario1.png',16,16);
game.load.tilemap('snes', 'assets/maps/smb_level1.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tilesSnes', 'assets/maps/smb_tiles.png',16,16);
}
function create() {
game.stage.backgroundColor = '#5c94fc';
mapNes = game.add.tilemap('nes');
tilesetNes = game.add.tileset('tilesNes');
layerNes = game.add.tilemapLayer(0, 0, mapNes.layers[0].width*tilesetNes.tileWidth, 600, tilesetNes, mapNes, 0);
layerNes.fixedToCamera=false;
layerNes.resizeWorld();
mapSnes = game.add.tilemap('snes');
tilesetSnes = game.add.tileset('tilesSnes');
layerSnes = game.add.tilemapLayer(0, 168, mapSnes.layers[0].width*tilesetSnes.tileWidth, 600, tilesetSnes, mapSnes, 0);
layerSnes.fixedToCamera=false;
game.add.tween(game.camera).to( { x: 5120-800 }, 30000, Phaser.Easing.Linear.None, true, 0, 1000, true);
game.input.onDown.add(goFull, this);
}
function goFull() {
game.stage.scale.startFullScreen();
}
+174
View File
@@ -0,0 +1,174 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.script('pt', 'wip/pt.js');
game.load.binary('impulse', 'assets/audio/protracker/act_of_impulse.mod', modLoaded, this);
game.load.binary('macrocosm', 'assets/audio/protracker/macrocosm.mod', modLoaded, this);
game.load.binary('enigma', 'assets/audio/protracker/enigma.mod', modLoaded, this);
game.load.binary('elysium', 'assets/audio/protracker/elysium.mod', modLoaded, this);
game.load.binary('stardust', 'assets/audio/protracker/sd-ingame1.mod', modLoaded, this);
game.load.binary('globaltrash', 'assets/audio/protracker/global_trash_3_v2.mod', modLoaded, this);
game.load.image('vu1', 'assets/sprites/flectrum.png');
game.load.image('vu2', 'assets/sprites/flectrum2.png');
game.load.image('vu3', 'assets/sprites/healthbar.png');
}
function modLoaded(key, data) {
console.log('module loaded:', key);
var buffer = new Uint8Array(data);
return buffer;
}
var vu1;
var vu2;
var vu3;
var vu4;
var modsample = new Array();
var module;
var sample1;
var sample2;
var sample3;
var sample4;
var sampleName1;
var sampleName2;
var sampleName3;
var sampleName4;
var r1;
var r2;
var r3;
var r4;
function create() {
// vu1 = game.add.sprite(400, 100, 'vu3');
// vu2 = game.add.sprite(400, 150, 'vu3');
// vu3 = game.add.sprite(400, 200, 'vu3');
// vu4 = game.add.sprite(400, 250, 'vu3');
module = new Protracker();
// module.buffer = game.cache.getBinary('globaltrash');
module.buffer = game.cache.getBinary('macrocosm');
module.parse();
module.play();
r1 = new Phaser.Rectangle(400, 100, 100, 32);
r2 = new Phaser.Rectangle(400, 150, 100, 32);
r3 = new Phaser.Rectangle(400, 200, 100, 32);
r4 = new Phaser.Rectangle(400, 250, 100, 32);
// console.log(module.sample);
}
function update() {
sampleName1 = '';
sampleName2 = '';
sampleName3 = '';
sampleName4 = '';
sample1 = module.channel[0].sample;
sample2 = module.channel[1].sample;
sample3 = module.channel[2].sample;
sample4 = module.channel[3].sample;
/*
module.sample = array of Objects containing:
data (Float32Array)
finetime
length (ms? bytes?)
looplength
loopstart
name
volume
arpeggio: 0
command: 0
data: 0
flags: 0
note: 22
noteon: 1
period: 240
sample: 11
samplepos: 314.3411880952386
samplespeed: 0.335118537414966
semitone: 14
slidespeed: 0
slideto: 214
slidetospeed: 0
vibratodepth: 0
vibratopos: 0
vibratospeed: 0
vibratowave: 0
voiceperiod: 240
volume: 64
*/
if (module.sample[sample1])
{
sampleName1 = module.sample[sample1].name;
}
if (module.sample[sample2])
{
sampleName2 = module.sample[sample2].name;
}
if (module.sample[sample3])
{
sampleName3 = module.sample[sample3].name;
}
if (module.sample[sample4])
{
sampleName4 = module.sample[sample4].name;
}
// vu1.width = Math.round(module.vu[0] * 400);
// vu2.width = Math.round(module.vu[1] * 400);
// vu3.width = Math.round(module.vu[2] * 400);
// vu4.width = Math.round(module.vu[3] * 400);
r1.width = Math.round(module.vu[0] * 500);
r2.width = Math.round(module.vu[1] * 500);
r3.width = Math.round(module.vu[2] * 500);
r4.width = Math.round(module.vu[3] * 500);
}
function render() {
game.debug.renderText('Sample ' + sample1 + ' : ' + sampleName1, 16, 32);
game.debug.renderText('Sample ' + sample2 + ' : ' + sampleName2, 16, 64);
game.debug.renderText('Sample ' + sample3 + ' : ' + sampleName3, 16, 96);
game.debug.renderText('Sample ' + sample4 + ' : ' + sampleName4, 16, 128);
game.debug.renderText('Position: ' + module.position, 16, 160);
game.debug.renderText('Pattern: ' + module.row, 16, 192);
game.debug.renderText('BPM: ' + module.bpm, 16, 224);
game.debug.renderText('Speed: ' + module.speed, 16, 256);
game.debug.renderText('Name: ' + module.title, 16, 288);
game.debug.renderText('Author: ' + module.signature, 16, 320);
game.debug.renderText('vu1: ' + module.vu[0], 16, 352);
game.debug.renderText('vu2: ' + module.vu[1], 16, 384);
game.debug.renderText('vu3: ' + module.vu[2], 16, 416);
game.debug.renderText('vu4: ' + module.vu[3], 16, 448);
game.debug.renderRectangle(r1);
game.debug.renderRectangle(r2);
game.debug.renderRectangle(r3);
game.debug.renderRectangle(r4);
}
+68
View File
@@ -0,0 +1,68 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
// Enable scaling
game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL;
game.stage.scale.maxWidth = 1500;
game.stage.scale.maxHeight = 1000;
game.stage.scale.refresh();
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('atari2', 'assets/sprites/atari800xl.png');
game.load.image('atari4', 'assets/sprites/atari800.png');
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
game.load.image('duck', 'assets/sprites/darkwing_crazy.png');
game.load.image('firstaid', 'assets/sprites/firstaid.png');
game.load.image('diamond', 'assets/sprites/diamond.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
function create() {
game.world.setBounds(0, 0, 2000, 2000);
// This returns an array of all the image keys in the cache
var images = game.cache.getImageKeys();
// Now let's create some random sprites and enable them all for drag and 'bring to top'
for (var i = 0; i < 200; i++)
{
var img = game.rnd.pick(images);
var tempSprite = game.add.sprite(game.world.randomX, game.world.randomY, img);
tempSprite.inputEnabled = true;
tempSprite.input.enableDrag(false, true);
}
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
if (cursors.up.isDown)
{
game.camera.y -= 4;
}
else if (cursors.down.isDown)
{
game.camera.y += 4;
}
if (cursors.left.isDown)
{
game.camera.x -= 4;
}
else if (cursors.right.isDown)
{
game.camera.x += 4;
}
}
function render() {
game.debug.renderInputInfo(32, 32);
}
+38
View File
@@ -0,0 +1,38 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.image('arrow', 'assets/sprites/arrow.png');
}
var sprite;
var tween;
function create() {
sprite = game.add.sprite(32, 32, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
game.input.onDown.add(moveSprite, this);
}
function moveSprite (pointer) {
if (tween && tween.isRunning)
{
tween.stop();
}
sprite.rotation = game.physics.angleToPointer(sprite, pointer);
// 300 = 300 pixels per second = the speed the sprite will move at, regardless of the distance it has to travel
var duration = (game.physics.distanceToPointer(sprite, pointer) / 300) * 1000;
tween = game.add.tween(sprite).to({ x: pointer.x, y: pointer.y }, duration, Phaser.Easing.Linear.None, true);
}
+40
View File
@@ -0,0 +1,40 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
function preload() {
game.load.spritesheet('spinner', 'assets/sprites/bluemetal_32x32x4.png', 32, 32);
}
var sprites;
function create() {
// Here we create a group, populate it with sprites, give them all a random velocity
// and then check the group against itself for collision
sprites = game.add.group();
for (var i = 0; i < 30; i++)
{
var s = sprites.create(game.rnd.integerInRange(100, 700), game.rnd.integerInRange(32, 200), 'spinner');
s.animations.add('spin', [0,1,2,3]);
s.play('spin', 20, true);
s.body.velocity.x = game.rnd.integerInRange(-200, 200);
s.body.velocity.y = game.rnd.integerInRange(-200, 200);
}
sprites.setAll('body.collideWorldBounds', true);
sprites.setAll('body.bounce.x', 1);
sprites.setAll('body.bounce.y', 1);
sprites.setAll('body.linearDamping', 0);
sprites.setAll('body.minBounceVelocity', 0);
}
function update() {
game.physics.collide(sprites);
}
+41
View File
@@ -0,0 +1,41 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
function preload() {
game.load.image('ball', 'assets/sprites/pangball.png');
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
}
var ballsGroup;
var shipsGroup;
function create() {
ballsGroup = game.add.group();
shipsGroup = game.add.group();
for (var i = 0; i < 20; i++)
{
// Create some randomly placed sprites in both Groups
ballsGroup.create(game.rnd.integerInRange(0, 128), game.world.randomY, 'ball');
shipsGroup.create(game.rnd.integerInRange(0, 128), game.world.randomY, 'arrow');
}
// Now make the ships group a child of the balls group - so anything that happens to the balls group
// will happen to the ships group too
ballsGroup.add(shipsGroup);
}
function update() {
ballsGroup.x += 0.1;
// Because shipsGroup is a child of ballsGroup it has already been moved 0.1px by the line above
// So the following line of code will make it appear to move twice as fast as ballsGroup
shipsGroup.x += 0.1;
}
+33
View File
@@ -0,0 +1,33 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari', 'assets/sprites/atari130xe.png');
}
var sprite1;
function create() {
game.stage.backgroundColor = '#2d2d2d';
sprite1 = game.add.sprite(350, 300, 'atari');
sprite1.name = 'atari';
sprite1.anchor.setTo(0.5, -2);
}
function update() {
sprite1.angle += 1;
}
function render() {
game.debug.renderSpriteCorners(sprite1);
}
+325
View File
@@ -0,0 +1,325 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
game.load.image('atari', 'assets/sprites/atari130xe.png');
}
var sprite;
var sprite2;
var sprite3;
var reverse = false;
function onBeginContact(a, b) {
console.log('Begin Contact between', a.name, 'and', b.name);
}
function onEndContact(a, b) {
console.log('End Contact between', a.name, 'and', b.name);
}
function create() {
game.stage.backgroundColor = '#124184';
test8();
}
function test8() {
// A up into B
sprite = game.add.sprite(300, 200, 'atari');
sprite.name = 'atari';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(1, 1);
sprite.body.checkCollision.up = false;
sprite.body.checkCollision.down = false;
sprite2 = game.add.sprite(350, 400, 'gameboy', 2);
sprite2.name = 'gameboy';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(1, 1);
sprite2.events.onBeginContact.add(onBeginContact, this);
sprite2.events.onEndContact.add(onEndContact, this);
reverse = true;
game.input.onDown.add(launch8, this);
}
function launch8() {
sprite2.body.velocity.x = -100;
sprite2.body.velocity.y = -200;
}
function test7() {
// A down into B
sprite = game.add.sprite(300, 400, 'atari');
sprite.name = 'atari';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(1, 1);
sprite.body.checkCollision.up = false;
sprite2 = game.add.sprite(350, 100, 'gameboy', 2);
sprite2.name = 'gameboy';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(1, 1);
// reverse = true;
game.input.onDown.add(launch7, this);
}
function launch7() {
sprite2.body.velocity.y = 100;
}
function test6() {
// Offset Down Collision false
sprite = game.add.sprite(100, 300, 'atari');
sprite.name = 'atari';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(1, 1);
sprite.body.checkCollision.left = false;
sprite.body.checkCollision.right = false;
sprite2 = game.add.sprite(500, 330, 'gameboy', 2);
// sprite2 = game.add.sprite(500, 530, 'gameboy', 2);
sprite2.name = 'gameboy';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(1, 1);
sprite3 = game.add.sprite(400, 100, 'gameboy', 0);
sprite3.name = 'gameboy2';
sprite3.body.collideWorldBounds = true;
sprite3.body.bounce.setTo(1, 1);
game.input.onDown.add(launch6, this);
}
function launch6() {
sprite.body.velocity.x = 100;
sprite2.body.velocity.x = -100;
sprite3.body.velocity.y = 100;
}
function test5() {
// Offset Down Collision false
sprite = game.add.sprite(360, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.bounce.setTo(0.9, 0.9);
sprite.body.checkCollision.up = false;
sprite2 = game.add.sprite(400, 200, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
// sprite2.body.checkCollision.down = false;
// sprite2.body.mass = 1;
sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch5, this);
}
function launch5() {
// sprite.body.velocity.x = 200;
// sprite.body.velocity.y = -300;
// sprite2.body.velocity.x = -200;
sprite2.body.velocity.y = 200;
}
function test4() {
// Down Collision false
sprite = game.add.sprite(400, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.9, 0.9);
sprite2 = game.add.sprite(400, 200, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.checkCollision.down = false;
// sprite2.body.mass = 1;
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch4, this);
}
function launch4() {
// sprite.body.velocity.x = 200;
sprite.body.velocity.y = -300;
// sprite2.body.velocity.x = -200;
// sprite2.body.velocity.y = -200;
}
function test3() {
// Top Collision false
sprite = game.add.sprite(400, 400, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.up = false;
sprite2 = game.add.sprite(400, 200, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
// sprite2.body.checkCollision.left = false;
sprite2.body.bounce.setTo(0.9, 0.9);
// sprite2.body.mass = 1;
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch3, this);
}
function launch3() {
// sprite.body.velocity.x = 200;
// sprite.body.velocity.y = -300;
// sprite2.body.velocity.x = -200;
sprite2.body.velocity.y = 200;
}
function test2() {
// Left Collision false
sprite = game.add.sprite(200, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.checkCollision.right = false;
sprite.body.bounce.setTo(0.9, 0.9);
// sprite.body.linearDamping = 0;
// sprite.scale.setTo(2, 2);
// sprite.body.mass = 2;
sprite2 = game.add.sprite(500, 300, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.checkCollision.left = false;
// sprite2.body.bounce.setTo(0.9, 0.9);
// sprite2.body.mass = 1;
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch2, this);
}
function launch2() {
sprite.body.velocity.x = 200;
// sprite.body.velocity.y = -300;
// sprite2.body.velocity.x = -200;
// sprite2.body.velocity.y = -200;
}
function test1() {
// Right Collision false
sprite = game.add.sprite(200, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.checkCollision.right = false;
// sprite.body.bounce.setTo(0.9, 0.9);
// sprite.body.bounce.setTo(1, 1);
// sprite.body.linearDamping = 0;
// sprite.scale.setTo(2, 2);
// sprite.body.mass = 2;
sprite2 = game.add.sprite(500, 300, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
// sprite2.body.mass = 1;
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.linearDamping = 0;
game.input.onDown.add(launch1, this);
}
function launch1() {
sprite2.body.velocity.x = -200;
}
function update() {
if (reverse)
{
game.physics.collide(sprite2, sprite);
}
else
{
game.physics.collide(sprite, sprite2);
}
if (sprite3)
{
game.physics.collide(sprite, sprite3);
}
}
function render() {
if (sprite)
{
game.debug.renderBodyInfo(sprite, 16, 24);
}
if (sprite)
{
game.debug.renderPhysicsBody(sprite.body);
}
if (sprite2)
{
game.debug.renderPhysicsBody(sprite2.body);
}
if (sprite3)
{
game.debug.renderPhysicsBody(sprite3.body);
}
}
+29
View File
@@ -0,0 +1,29 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create, render: render });
function create() {
game.onPause.add(onGamePause, this);
game.onResume.add(onGameResume, this);
}
function onGamePause() {
console.log('game paused at ' + this.time.now);
}
function onGameResume() {
console.log('game un-paused at ' + this.time.now);
console.log('was paused for ' + game.time.pauseDuration);
}
function render() {
game.debug.renderText('now: ' + game.time.now, 32, 32);
game.debug.renderText('paused: ' + game.paused, 32, 64);
}
+63
View File
@@ -0,0 +1,63 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('chunk', 'assets/sprites/chunk.png');
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
}
var sprite;
var bmd;
function create() {
game.stage.backgroundColor = '#124184';
bmd = game.add.bitmapData(800, 600);
bmd.fillStyle('#ffffff');
var bg = game.add.sprite(0, 0, bmd);
bg.body.moves = false;
game.physics.gravity.y = 100;
sprite = game.add.sprite(32, 450, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
sprite.body.linearDamping = 0.1;
game.input.onDown.add(launch, this);
}
function launch() {
if (game.input.x < 400)
{
sprite.body.velocity.setTo(-200, -200);
}
else
{
sprite.body.velocity.setTo(200, -200);
}
}
function update() {
sprite.rotation = sprite.body.angle;
bmd.fillStyle('#ffff00');
bmd.fillRect(sprite.x, sprite.y, 2, 2);
}
function render() {
game.debug.renderBodyInfo(sprite, 16, 24);
}
+46
View File
@@ -0,0 +1,46 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari', 'assets/sprites/atari130xe.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('ball', 'assets/sprites/pangball.png');
}
var sprite1;
// var sprite2;
function create() {
game.stage.backgroundColor = '#2d2d2d';
sprite1 = game.add.sprite(300, 300, 'ball');
sprite1.anchor.setTo(0, 1);
sprite1.pivot.x = 100;
sprite1.pivot.y = 100;
// sprite2 = game.add.sprite(0, 0, 'mushroom');
// sprite2.name = 'mushroom';
}
function update() {
sprite1.angle += 1;
// sprite1.pivot.x = game.input.x;
// sprite1.pivot.y = game.input.y;
}
function render() {
game.debug.renderPixel(sprite1.pivot.x, sprite1.pivot.y);
// game.debug.renderSpriteInfo(sprite1, 100, 400);
// game.debug.renderSpriteBounds(sprite1);
// game.debug.renderSpriteInfo(sprite2, 100, 100);
// game.debug.renderSpriteBounds(sprite2);
}
@@ -0,0 +1,51 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.atlas('atlas', 'assets/sprites/atlas_json_array_trimmed.png', 'assets/sprites/atlas_json_array_trimmed.json');
}
var wizball;
// var chick;
// var mech;
// var robot;
// var cop;
function create() {
game.stage.backgroundColor = '#404040';
// This demonstrates pixel perfect click detection even if using sprites in a texture atlas.
// chick = game.add.sprite(64, 64, 'atlas');
// chick.frameName = 'budbrain_chick.png';
// chick.inputEnabled = true;
// chick.input.pixelPerfect = true;
// chick.input.useHandCursor = true;
// cop = game.add.sprite(600, 64, 'atlas');
// cop.frameName = 'ladycop.png';
// cop.inputEnabled = true;
// cop.input.pixelPerfect = true;
// cop.input.useHandCursor = true;
// robot = game.add.sprite(50, 300, 'atlas');
// robot.frameName = 'robot.png';
// robot.inputEnabled = true;
// robot.input.pixelPerfect = true;
// robot.input.useHandCursor = true;
wizball = game.add.sprite(100, 400, 'atlas', 'wizball');
wizball.inputEnabled = true;
wizball.input.pixelPerfect = true;
wizball.input.useHandCursor = true;
// mech = game.add.sprite(250, 100, 'atlas');
// mech.frameName = 'titan_mech.png';
// mech.inputEnabled = true;
// mech.input.pixelPerfect = true;
// mech.input.useHandCursor = true;
}
+121
View File
@@ -0,0 +1,121 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('map', 'assets/tilemaps/maps/platform.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('platformer_tiles', 'assets/tilemaps/tiles/platformer_tiles.png');
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17);
}
var map;
var layer;
var sprite;
var sprite2;
var balls;
var cursors;
function create() {
game.stage.backgroundColor = '#124184';
map = game.add.tilemap('map');
map.addTilesetImage('platformer_tiles');
map.setCollisionBetween(21, 53);
layer = map.createLayer('Tile Layer 1');
layer.debug = true;
// game.physics.gravity.y = 150;
/*
balls = game.add.group();
for (var i = 0; i < 30; i++)
{
var s = balls.create(game.rnd.integerInRange(100, 700), game.rnd.integerInRange(100, 200), 'balls', game.rnd.integerInRange(0, 6));
s.body.velocity.x = game.rnd.integerInRange(-400, 400);
s.body.velocity.y = game.rnd.integerInRange(-100, -200);
s.name = 'ball' + i;
}
balls.setAll('body.collideWorldBounds', true);
balls.setAll('body.bounce.x', 0.8);
balls.setAll('body.bounce.y', 0.9);
balls.setAll('body.minBounceVelocity', 0.9);
balls.setAll('body.linearDamping', 0.5);
*/
sprite2 = game.add.sprite(340, 250, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
// sprite2.body.bounce.setTo(0.5, 0.5);
sprite = game.add.sprite(270, 100, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.minBounceVelocity = 0.9;
sprite.body.bounce.setTo(0.5, 0.9);
sprite.body.linearDamping = 0.5;
game.input.onDown.add(launch, this);
}
function launch() {
// sprite.body.velocity.x = -200;
// sprite.body.velocity.y = -200;
sprite2.body.velocity.x = -200;
sprite2.body.velocity.y = -200;
}
function hit(face, body1, body2) {
console.log('hit', face);
}
var flag = false;
function update() {
// game.physics.collide(balls, layer);
game.physics.collide(sprite, layer);
game.physics.collide(sprite2, layer);
game.physics.collide(sprite, sprite2);
}
function render() {
game.debug.renderBodyInfo(sprite2, 32, 32);
game.debug.renderPhysicsBody(sprite2.body);
// game.debug.renderText(sprite2.body.left, 32, 30);
// game.debug.renderText(sprite2.body.right, 32, 50);
// game.debug.renderText(sprite2.body.top, 32, 70);
// game.debug.renderText(sprite2.body.bottom, 32, 90);
// for (var i = 0; i < balls._container.length; i++)
// {
// }
// if (sprite)
// {
// // game.debug.renderBodyInfo(sprite, 20, 30);
// game.debug.renderBodyInfo(sprite2, 20, 230);
// }
}
+16
View File
@@ -0,0 +1,16 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.image('test', 'assets/pics/nanoha_taiken_blue.png');
}
function create() {
game.stage.backgroundColor = 'rgba(0,0,0,0.3)';
game.add.sprite(0, 0, 'test');
}
+913
View File
@@ -0,0 +1,913 @@
/*
amiga protracker module player for web audio api
(c) 2012-2013 firehawk/tda (firehawk@haxor.fi)
originally hacked together in a weekend, so please excuse
me for the spaghetti code. :)
AMIGAAAAAAAAH!!
kinda sorta changelog:
(apr 2013)
- changed the logic for pattern break/jump. mod.pattern_skank now
plays correctly.
(feb 2013)
- fixed NaN samples with mod.fractured and mod.multicolour (thanks Aegis!)
(jan 2013)
- fixed vibrato amplitude (was half of what it should be, apparently)
- fixed to work on safari again (thanks Matt Diamond @ stackoverflow.com)
(dec 2012)
- replaced effect switch-statement with jumptables
- fixed clicks (bad loops, empty samples)
- fixed playback bug with sample-only rows
- added amiga 500 lowpass filters (not 100% authentic, though)
- added compressor to output
- latest safari has broken web audio so chrome-only for now
(aug 2012)
- first version written from scratch
todo:
- safari on ipad is broken again, it seems
- fix more playback bugs
* mod.black_queen (pattern loop has glitches)
- properly test EEx delay pattern
- implement the rest of the effects
- optimize for more speed!! SPEEEED!!
* switch to fixed point sample pointers, Math.floor() is _slow_ on iOS
*/
// constructor for protracker player object
function Protracker()
{
var i, t;
this.initialize();
this.clearsong();
this.url="";
this.loading=false;
this.ready=false;
this.playing=false;
this.buffer=0;
this.mixerNode=0;
this.paused=false;
this.repeat=false;
this.separation=true;
this.palclock=true;
this.autostart=false;
this.onReady=function(){};
this.onPlay=function(){};
this.onStop=function(){};
// paula period values
this.baseperiodtable=new Array(
856,808,762,720,678,640,604,570,538,508,480,453,
428,404,381,360,339,320,302,285,269,254,240,226,
214,202,190,180,170,160,151,143,135,127,120,113);
// finetune multipliers
this.finetunetable=new Array();
for(t=0;t<16;t++) this.finetunetable[t]=Math.pow(2, (t-8)/12/8);
// calc tables for vibrato waveforms
this.vibratotable=new Array();
for(t=0;t<4;t++) {
this.vibratotable[t]=new Array();
for(i=0;i<64;i++) {
switch(t) {
case 0:
this.vibratotable[t][i]=127*Math.sin(Math.PI*2*(i/64));
break;
case 1:
this.vibratotable[t][i]=127-4*i;
break;
case 2:
this.vibratotable[t][i]=(i<32)?127:-127;
break;
case 3:
this.vibratotable[t][i]=(1-2*Math.random())*127;
break;
}
}
}
// effect jumptables
this.effects_t0 = new Array(
this.effect_t0_0, this.effect_t0_1, this.effect_t0_2, this.effect_t0_3, this.effect_t0_4, this.effect_t0_5, this.effect_t0_6, this.effect_t0_7,
this.effect_t0_8, this.effect_t0_9, this.effect_t0_a, this.effect_t0_b, this.effect_t0_c, this.effect_t0_d, this.effect_t0_e, this.effect_t0_f);
this.effects_t0_e = new Array(
this.effect_t0_e0, this.effect_t0_e1, this.effect_t0_e2, this.effect_t0_e3, this.effect_t0_e4, this.effect_t0_e5, this.effect_t0_e6, this.effect_t0_e7,
this.effect_t0_e8, this.effect_t0_e9, this.effect_t0_ea, this.effect_t0_eb, this.effect_t0_ec, this.effect_t0_ed, this.effect_t0_ee, this.effect_t0_ef);
this.effects_t1 = new Array(
this.effect_t1_0, this.effect_t1_1, this.effect_t1_2, this.effect_t1_3, this.effect_t1_4, this.effect_t1_5, this.effect_t1_6, this.effect_t1_7,
this.effect_t1_8, this.effect_t1_9, this.effect_t1_a, this.effect_t1_b, this.effect_t1_c, this.effect_t1_d, this.effect_t1_e, this.effect_t1_f);
this.effects_t1_e = new Array(
this.effect_t1_e0, this.effect_t1_e1, this.effect_t1_e2, this.effect_t1_e3, this.effect_t1_e4, this.effect_t1_e5, this.effect_t1_e6, this.effect_t1_e7,
this.effect_t1_e8, this.effect_t1_e9, this.effect_t1_ea, this.effect_t1_eb, this.effect_t1_ec, this.effect_t1_ed, this.effect_t1_ee, this.effect_t1_ef);
this.context = null;
this.samplerate=44100;
this.bufferlen=2048;
}
// create the web audio context
Protracker.prototype.createContext = function()
{
this.context = new webkitAudioContext();
this.samplerate=this.context.sampleRate;
this.bufferlen=(this.samplerate > 44100) ? 4096 : 2048;
// fixed filter at 6kHz
this.filterNode=this.context.createBiquadFilter();
this.filterNode.frequency.value=6000
// "LED filter" at 3.5kHz - off by default
this.lowpassNode=this.context.createBiquadFilter();
this.lowpassNode.frequency.value=28867;
// mixer
this.mixerNode=this.context.createJavaScriptNode(this.bufferlen, 1, 2);
this.mixerNode.module=this;
this.mixerNode.onaudioprocess=Protracker.prototype.mix;
// compressor for a bit of volume boost
this.compressorNode=this.context.createDynamicsCompressor();
// patch up some cables :)
this.mixerNode.connect(this.filterNode);
this.filterNode.connect(this.lowpassNode);
this.lowpassNode.connect(this.compressorNode);
this.compressorNode.connect(this.context.destination);
}
// play loaded and parsed module with webaudio context
Protracker.prototype.play = function()
{
if (this.context==null) this.createContext();
if (!this.ready) return false;
if (this.paused) {
this.paused=false;
return true;
}
this.paused=false;
this.initialize();
this.flags=1+2;
this.playing=true;
this.onPlay();
return true;
}
// pause playback
Protracker.prototype.pause = function()
{
if (!this.paused) {
this.paused=true;
} else {
this.paused=false;
}
}
// stop playback and release webaudio node
Protracker.prototype.stop = function()
{
this.playing=false;
this.onStop();
}
// jump positions forward/back
Protracker.prototype.jump = function(step)
{
this.tick=0;
this.row=0;
this.position+=step;
this.flags=1+2;
if (this.position<0) this.position=0;
if (this.position >= this.songlen) this.stop();
}
// set whether module repeats after songlen
Protracker.prototype.setrepeat = function(rep)
{
this.repeat=rep;
}
// set stereo separation mode (false=paula, true=betterpaula)
Protracker.prototype.setseparation = function(sep)
{
this.separation=sep;
}
// set amiga video standard (false=NTSC, true=PAL)
Protracker.prototype.setamigatype = function(clock)
{
this.palclock=clock;
}
// set autostart to play immediately after loading
Protracker.prototype.setautostart = function(st)
{
this.autostart=st;
}
// clear song data
Protracker.prototype.clearsong = function()
{
this.title="";
this.signature="";
this.songlen=1;
this.repeatpos=0;
this.patterntable=new ArrayBuffer(128);
for(i=0;i<128;i++) this.patterntable[i]=0;
this.channels=4;
this.sample=new Array();
this.samples=31;
for(i=0;i<31;i++) {
this.sample[i]=new Object();
this.sample[i].name="";
this.sample[i].length=0;
this.sample[i].finetune=0;
this.sample[i].volume=64;
this.sample[i].loopstart=0;
this.sample[i].looplength=0;
this.sample[i].data=0;
}
this.patterns=0;
this.pattern=new Array();
this.note=new Array();
this.looprow=0;
this.loopstart=0;
this.loopcount=0;
this.patterndelay=0;
this.patternwait=0;
}
// initialize all player variables
Protracker.prototype.initialize = function()
{
this.tick=0;
this.position=0;
this.row=0;
this.offset=0;
this.flags=0;
this.speed=6;
this.bpm=125;
this.breakrow=0;
this.patternjump=0;
this.patterndelay=0;
this.patternwait=0;
this.channel=new Array();
for(i=0;i<this.channels;i++) {
this.channel[i]=new Object();
this.channel[i].sample=0;
this.channel[i].period=214;
this.channel[i].voiceperiod=214;
this.channel[i].note=24;
this.channel[i].volume=64;
this.channel[i].command=0;
this.channel[i].data=0;
this.channel[i].samplepos=0;
this.channel[i].samplespeed=0;
this.channel[i].flags=0;
this.channel[i].noteon=0;
this.channel[i].slidespeed=0;
this.channel[i].slideto=214;
this.channel[i].slidetospeed=0;
this.channel[i].arpeggio=0;
this.channel[i].semitone=12;
this.channel[i].vibratospeed=0
this.channel[i].vibratodepth=0
this.channel[i].vibratopos=0;
this.channel[i].vibratowave=0;
}
this.vu=new Array();
}
// load module from url into local buffer
Protracker.prototype.load = function(url)
{
this.url=url;
this.clearsong();
var request = new XMLHttpRequest();
request.open("GET", this.url, true);
request.responseType = "arraybuffer";
this.request = request;
this.loading=true;
var asset = this;
request.onload = function() {
asset.buffer=new Uint8Array(request.response);
asset.parse();
if (asset.autostart) asset.play();
}
request.send();
}
// parse the module from local buffer
Protracker.prototype.parse = function()
{
var i,j,c;
if (!this.buffer) return false;
for(i=0;i<4;i++) this.signature+=String.fromCharCode(this.buffer[1080+i]);
switch (this.signature) {
case "M.K.":
case "M!K!":
case "4CHN":
case "FLT4":
break;
case "6CHN":
this.channels=6;
break;
case "8CHN":
case "FLT8":
this.channels=8;
break;
case "28CH":
this.channels=28;
break;
default:
return false;
}
this.vu=new Array();
for(i=0;i<this.channels;i++) this.vu[i]=0.0;
i=0;
while(this.buffer[i] && i<20)
this.title=this.title+String.fromCharCode(this.buffer[i++]);
for(i=0;i<this.samples;i++) {
var st=20+i*30;
j=0;
while(this.buffer[st+j] && j<22) {
this.sample[i].name+=
((this.buffer[st+j]>0x1f) && (this.buffer[st+j]<0x7f)) ?
(String.fromCharCode(this.buffer[st+j])) :
(" ");
j++;
}
this.sample[i].length=2*(this.buffer[st+22]*256 + this.buffer[st+23]);
this.sample[i].finetune=this.buffer[st+24];
if (this.sample[i].finetune > 7) this.sample[i].finetune=this.sample[i].finetune-16;
this.sample[i].volume=this.buffer[st+25];
this.sample[i].loopstart=2*(this.buffer[st+26]*256 + this.buffer[st+27]);
this.sample[i].looplength=2*(this.buffer[st+28]*256 + this.buffer[st+29]);
if (this.sample[i].looplength==2) this.sample[i].looplength=0;
if (this.sample[i].loopstart>this.sample[i].length) {
this.sample[i].loopstart=0;
this.sample[i].looplength=0;
}
}
this.songlen=this.buffer[950];
if (this.buffer[951] != 127) this.repeatpos=this.buffer[951];
for(i=0;i<128;i++) {
this.patterntable[i]=this.buffer[952+i];
if (this.patterntable[i] > this.patterns) this.patterns=this.patterntable[i];
}
this.patterns+=1;
var patlen=4*64*this.channels;
this.pattern=new Array();
this.note=new Array();
for(i=0;i<this.patterns;i++) {
this.pattern[i]=new Uint8Array(patlen);
this.note[i]=new Uint8Array(this.channels*64);
for(j=0;j<patlen;j++) this.pattern[i][j]=this.buffer[1084+i*patlen+j];
for(j=0;j<64;j++) for(c=0;c<this.channels;c++) {
this.note[i][j*this.channels+c]=0;
var n=(this.pattern[i][j*4*this.channels+c*4]&0x0f)<<8 | this.pattern[i][j*4*this.channels+c*4+1];
for(var np=0; np<this.baseperiodtable.length; np++)
if (n==this.baseperiodtable[np]) this.note[i][j*this.channels+c]=np;
}
}
var sst=1084+this.patterns*patlen;
for(i=0;i<this.samples;i++) {
this.sample[i].data=new Float32Array(this.sample[i].length);
for(j=0;j<this.sample[i].length;j++) {
var q=this.buffer[sst+j];
if (q<128) {
q=q/128.0;
} else {
q=((q-128)/128.0)-1.0;
}
this.sample[i].data[j]=q;
}
sst+=this.sample[i].length;
}
this.ready=true;
this.loading=false;
this.buffer=0;
if (this.context) this.lowpassNode.frequency.value=28867;
this.onReady();
return true;
}
// advance player
Protracker.prototype.advance=function(mod) {
var spd=(((mod.samplerate*60)/mod.bpm)/4)/6;
// advance player
if (mod.offset>spd) { mod.tick++; mod.offset=0; mod.flags|=1; }
if (mod.tick>=mod.speed) {
if (mod.patterndelay) { // delay pattern
if (mod.tick < ((mod.patternwait+1)*mod.speed)) {
mod.patternwait++;
} else {
mod.row++; mod.tick=0; mod.flags|=2; mod.patterndelay=0;
}
}
else {
if (mod.flags&(16+32+64)) {
if (mod.flags&64) { // loop pattern?
mod.row=mod.looprow;
mod.flags&=0xa1;
mod.flags|=2;
}
else {
if (mod.flags&16) { // pattern jump/break?
//console.log("break to pattern " + mod.patternjump + " row "+mod.breakrow);
mod.position=mod.patternjump;
mod.row=mod.breakrow;
mod.patternjump=0;
mod.breakrow=0;
mod.flags&=0xe1;
mod.flags|=2;
}
}
mod.tick=0;
} else {
mod.row++; mod.tick=0; mod.flags|=2;
}
}
}
if (mod.row>=64) { mod.position++; mod.row=0; mod.flags|=4; }
if (mod.position>=mod.songlen) {
if (mod.repeat) {
mod.position=0;
} else {
mod.stop();
}
return;
}
}
// mix an audio buffer with data
Protracker.prototype.mix = function(ape) {
var f;
var p, pp, n, nn;
var mod=ape.srcElement.module;
outp=new Array();
var bufs=new Array(ape.outputBuffer.getChannelData(0), ape.outputBuffer.getChannelData(1));
var buflen=ape.outputBuffer.length;
for(var s=0;s<buflen;s++)
{
outp[0]=0.0;
outp[1]=0.0;
if (!mod.paused && mod.playing)
{
mod.advance(mod);
var och=0;
for(var ch=0;ch<mod.channels;ch++)
{
// calculate playback position
p=mod.patterntable[mod.position];
pp=mod.row*4*mod.channels + ch*4;
if (mod.flags&2) { // new row
modrow = mod.row; // here
mod.channel[ch].command=mod.pattern[p][pp+2]&0x0f;
mod.channel[ch].data=mod.pattern[p][pp+3];
if (!(mod.channel[ch].command==0x0e && (mod.channel[ch].data&0xf0)==0xd0)) {
n=(mod.pattern[p][pp]&0x0f)<<8 | mod.pattern[p][pp+1];
if (n) {
// noteon, except if command=3 (porta to note)
if ((mod.channel[ch].command != 0x03) && (mod.channel[ch].command != 0x05)) {
mod.channel[ch].period=n;
mod.channel[ch].samplepos=0;
if (mod.channel[ch].vibratowave>3) mod.channel[ch].vibratopos=0;
mod.channel[ch].flags|=3; // recalc speed
mod.channel[ch].noteon=1;
} else {
mod.channel[ch].slideto=n;
}
}
nn=mod.pattern[p][pp+0]&0xf0 | mod.pattern[p][pp+2]>>4;
if (nn) {
mod.channel[ch].sample=nn-1;
mod.channel[ch].volume=mod.sample[nn-1].volume;
if (!n && (mod.channel[ch].samplepos > mod.sample[nn-1].length)) mod.channel[ch].samplepos=0;
}
}
}
mod.channel[ch].voiceperiod=mod.channel[ch].period;
if (mod.channel[ch].samplepos==0) modsample[ch]=mod.channel[ch].sample;
// kill empty samples
if (!mod.sample[mod.channel[ch].sample].length) mod.channel[ch].noteon=0;
// effects
if (mod.flags&1) {
if (!mod.tick) {
// process only on tick 0
mod.effects_t0[mod.channel[ch].command](mod, ch);
} else {
mod.effects_t1[mod.channel[ch].command](mod, ch);
}
}
// recalc note number from period
if (mod.channel[ch].flags&2) {
for(var np=0; np<mod.baseperiodtable.length; np++)
if (mod.baseperiodtable[np]>=mod.channel[ch].period) mod.channel[ch].note=np;
mod.channel[ch].semitone=7;
if (mod.channel[ch].period>=120)
mod.channel[ch].semitone=mod.baseperiodtable[mod.channel[ch].note]-mod.baseperiodtable[mod.channel[ch].note+1];
}
// recalc sample speed and apply finetune
if ((mod.channel[ch].flags&1 || mod.flags&2) && mod.channel[ch].voiceperiod)
mod.channel[ch].samplespeed=
(mod.palclock ? 7093789.2 : 7159090.5)/(mod.channel[ch].voiceperiod*2) * mod.finetunetable[mod.sample[mod.channel[ch].sample].finetune+8] / mod.samplerate;
// advance vibrato on each new tick
if (mod.flags&1) {
mod.channel[ch].vibratopos+=mod.channel[ch].vibratospeed;
mod.channel[ch].vibratopos&=0x3f;
}
// mix channel to output
och=och^(ch&1);
f=0.0;
if (mod.channel[ch].noteon) {
if (mod.sample[mod.channel[ch].sample].length > mod.channel[ch].samplepos)
f=(1.0/mod.channels) *
(mod.sample[mod.channel[ch].sample].data[Math.floor(mod.channel[ch].samplepos)]*mod.channel[ch].volume)/64.0;
outp[och]+=f;
mod.channel[ch].samplepos+=mod.channel[ch].samplespeed;
}
if (s==0) mod.vu[ch]=Math.abs(f);
// loop or end samples
if (mod.channel[ch].noteon) {
if (mod.sample[mod.channel[ch].sample].loopstart || mod.sample[mod.channel[ch].sample].looplength) {
if (mod.channel[ch].samplepos >= (mod.sample[mod.channel[ch].sample].loopstart+mod.sample[mod.channel[ch].sample].looplength)) {
mod.channel[ch].samplepos=mod.sample[mod.channel[ch].sample].loopstart;
}
} else {
if (mod.channel[ch].samplepos >= mod.sample[mod.channel[ch].sample].length) {
mod.channel[ch].noteon=0;
}
}
}
// clear channel flags
mod.channel[ch].flags=0;
}
mod.offset++;
mod.flags&=0x70;
}
// a more headphone-friendly stereo separation (aka. betterpaula)
if (mod.separation) {
t=outp[0];
outp[0]=outp[0]*0.6 + outp[1]*0.4;
outp[1]=outp[1]*0.6 + t*0.4;
}
bufs[0][s]=outp[0];
bufs[1][s]=outp[1];
}
}
//
// tick 0 effect functions
//
Protracker.prototype.effect_t0_0=function(mod, ch) { // 0 arpeggio
mod.channel[ch].arpeggio=mod.channel[ch].data;
}
Protracker.prototype.effect_t0_1=function(mod, ch) { // 1 slide up
if (mod.channel[ch].data) mod.channel[ch].slidespeed=mod.channel[ch].data;
}
Protracker.prototype.effect_t0_2=function(mod, ch) { // 2 slide down
if (mod.channel[ch].data) mod.channel[ch].slidespeed=mod.channel[ch].data;
}
Protracker.prototype.effect_t0_3=function(mod, ch) { // 3 slide to note
if (mod.channel[ch].data) mod.channel[ch].slidetospeed=mod.channel[ch].data;
}
Protracker.prototype.effect_t0_4=function(mod, ch) { // 4 vibrato
if (mod.channel[ch].data&0x0f && mod.channel[ch].data&0xf0) {
mod.channel[ch].vibratodepth=(mod.channel[ch].data&0x0f);
mod.channel[ch].vibratospeed=(mod.channel[ch].data&0xf0)>>4;
}
mod.channel[ch].voiceperiod+=
(mod.channel[ch].vibratodepth/32)*mod.channel[ch].semitone*(mod.vibratotable[mod.channel[ch].vibratowave&3][mod.channel[ch].vibratopos]/127);
mod.channel[ch].flags|=1;
}
Protracker.prototype.effect_t0_5=function(mod, ch) { // 5
}
Protracker.prototype.effect_t0_6=function(mod, ch) { // 6
}
Protracker.prototype.effect_t0_7=function(mod, ch) { // 7
}
Protracker.prototype.effect_t0_8=function(mod, ch) { // 8
}
Protracker.prototype.effect_t0_9=function(mod, ch) { // 9 set sample offset
mod.channel[ch].samplepos=mod.channel[ch].data*256;
}
Protracker.prototype.effect_t0_a=function(mod, ch) { // a
}
Protracker.prototype.effect_t0_b=function(mod, ch) { // b pattern jump
mod.breakrow=0;
mod.patternjump=mod.channel[ch].data;
mod.flags|=16;
}
Protracker.prototype.effect_t0_c=function(mod, ch) { // c set volume
mod.channel[ch].volume=mod.channel[ch].data;
}
Protracker.prototype.effect_t0_d=function(mod, ch) { // d pattern break
mod.breakrow=((mod.channel[ch].data&0xf0)>>4)*10 + (mod.channel[ch].data&0x0f);
if (!(mod.flags&16)) mod.patternjump=mod.position+1;
mod.flags|=16;
}
Protracker.prototype.effect_t0_e=function(mod, ch) { // e
var i=(mod.channel[ch].data&0xf0)>>4;
mod.effects_t0_e[i](mod, ch);
}
Protracker.prototype.effect_t0_f=function(mod, ch) { // f set speed
if (mod.channel[ch].data > 32) {
mod.bpm=mod.channel[ch].data;
} else {
if (mod.channel[ch].data) mod.speed=mod.channel[ch].data;
}
}
//
// tick 0 effect e functions
//
Protracker.prototype.effect_t0_e0=function(mod, ch) { // e0 filter on/off
if (mod.channel[ch].data&0x0f) {
mod.lowpassNode.frequency.value=4280; //3500;
} else {
mod.lowpassNode.frequency.value=28867;
}
}
Protracker.prototype.effect_t0_e1=function(mod, ch) { // e1 fine slide up
mod.channel[ch].period-=mod.channel[ch].data&0x0f;
if (mod.channel[ch].period < 113) mod.channel[ch].period=113;
}
Protracker.prototype.effect_t0_e2=function(mod, ch) { // e2 fine slide down
mod.channel[ch].period+=mod.channel[ch].data&0x0f;
if (mod.channel[ch].period > 856) mod.channel[ch].period=856;
mod.channel[ch].flags|=1;
}
Protracker.prototype.effect_t0_e3=function(mod, ch) { // e3 set glissando
}
Protracker.prototype.effect_t0_e4=function(mod, ch) { // e4 set vibrato waveform
mod.channel[ch].vibratowave=mod.channel[ch].data&0x07;
}
Protracker.prototype.effect_t0_e5=function(mod, ch) { // e5 set finetune
}
Protracker.prototype.effect_t0_e6=function(mod, ch) { // e6 loop pattern
if (mod.channel[ch].data&0x0f) {
if (mod.loopcount) {
mod.loopcount--;
} else {
mod.loopcount=mod.channel[ch].data&0x0f;
}
if (mod.loopcount) mod.flags|=64;
} else {
mod.looprow=mod.row;
}
}
Protracker.prototype.effect_t0_e7=function(mod, ch) { // e7
}
Protracker.prototype.effect_t0_e8=function(mod, ch) { // e8
}
Protracker.prototype.effect_t0_e9=function(mod, ch) { // e9
}
Protracker.prototype.effect_t0_ea=function(mod, ch) { // ea fine volslide up
mod.channel[ch].volume+=mod.channel[ch].data&0x0f;
if (mod.channel[ch].volume > 64) mod.channel[ch].volume=64;
}
Protracker.prototype.effect_t0_eb=function(mod, ch) { // eb fine volslide down
mod.channel[ch].volume-=mod.channel[ch].data&0x0f;
if (mod.channel[ch].volume < 0) mod.channel[ch].volume=0;
}
Protracker.prototype.effect_t0_ec=function(mod, ch) { // ec
}
Protracker.prototype.effect_t0_ed=function(mod, ch) { // ed delay sample
if (mod.tick==(mod.channel[ch].data&0x0f)) {
// start note
var p=mod.patterntable[mod.position];
var pp=mod.row*4*mod.channels + ch*4;
n=(mod.pattern[p][pp]&0x0f)<<8 | mod.pattern[p][pp+1];
if (n) {
mod.channel[ch].period=n;
mod.channel[ch].voiceperiod=mod.channel[ch].period;
mod.channel[ch].samplepos=0;
if (mod.channel[ch].vibratowave>3) mod.channel[ch].vibratopos=0;
mod.channel[ch].flags|=3; // recalc speed
mod.channel[ch].noteon=1;
}
n=mod.pattern[p][pp+0]&0xf0 | mod.pattern[p][pp+2]>>4;
if (n) {
mod.channel[ch].sample=n-1;
mod.channel[ch].volume=mod.sample[n-1].volume;
}
}
}
Protracker.prototype.effect_t0_ee=function(mod, ch) { // ee delay pattern
mod.patterndelay=mod.channel[ch].data&0x0f;
mod.patternwait=0;
}
Protracker.prototype.effect_t0_ef=function(mod, ch) { // ef
}
//
// tick 1+ effect functions
//
Protracker.prototype.effect_t1_0=function(mod, ch) { // 0 arpeggio
if (mod.channel[ch].data) {
var apn=mod.channel[ch].note;
if ((mod.tick%3)==1) apn+=mod.channel[ch].arpeggio>>4;
if ((mod.tick%3)==2) apn+=mod.channel[ch].arpeggio&0x0f;
if (apn>=0 && apn <= mod.baseperiodtable.length)
mod.channel[ch].voiceperiod = mod.baseperiodtable[apn];
mod.channel[ch].flags|=1;
}
}
Protracker.prototype.effect_t1_1=function(mod, ch) { // 1 slide up
mod.channel[ch].period-=mod.channel[ch].slidespeed;
if (mod.channel[ch].period<113) mod.channel[ch].period=113;
mod.channel[ch].flags|=3; // recalc speed
}
Protracker.prototype.effect_t1_2=function(mod, ch) { // 2 slide down
mod.channel[ch].period+=mod.channel[ch].slidespeed;
if (mod.channel[ch].period>856) mod.channel[ch].period=856;
mod.channel[ch].flags|=3; // recalc speed
}
Protracker.prototype.effect_t1_3=function(mod, ch) { // 3 slide to note
if (mod.channel[ch].period < mod.channel[ch].slideto) {
mod.channel[ch].period+=mod.channel[ch].slidetospeed;
if (mod.channel[ch].period > mod.channel[ch].slideto)
mod.channel[ch].period=mod.channel[ch].slideto;
}
if (mod.channel[ch].period > mod.channel[ch].slideto) {
mod.channel[ch].period-=mod.channel[ch].slidetospeed;
if (mod.channel[ch].period<mod.channel[ch].slideto)
mod.channel[ch].period=mod.channel[ch].slideto;
}
mod.channel[ch].flags|=3; // recalc speed
}
Protracker.prototype.effect_t1_4=function(mod, ch) { // 4 vibrato
mod.channel[ch].voiceperiod+=
(mod.channel[ch].vibratodepth/16)*mod.channel[ch].semitone*(mod.vibratotable[mod.channel[ch].vibratowave&3][mod.channel[ch].vibratopos]/127);
mod.channel[ch].flags|=1;
}
Protracker.prototype.effect_t1_5=function(mod, ch) { // 5 volslide + slide to note
mod.effect_t1_3(mod, ch); // slide to note
mod.effect_t1_a(mod, ch); // volslide
}
Protracker.prototype.effect_t1_6=function(mod, ch) { // 6 volslide + vibrato
mod.effect_t1_4(mod, ch); // vibrato
mod.effect_t1_a(mod, ch); // volslide
}
Protracker.prototype.effect_t1_7=function(mod, ch) { // 7
}
Protracker.prototype.effect_t1_8=function(mod, ch) { // 8
}
Protracker.prototype.effect_t1_9=function(mod, ch) { // 9 set sample offset
}
Protracker.prototype.effect_t1_a=function(mod, ch) { // a volume slide
if (!(mod.channel[ch].data&0x0f)) {
// y is zero, slide up
mod.channel[ch].volume+=(mod.channel[ch].data>>4);
if (mod.channel[ch].volume>64) mod.channel[ch].volume=64;
}
if (!(mod.channel[ch].data&0xf0)) {
// x is zero, slide down
mod.channel[ch].volume-=(mod.channel[ch].data&0x0f);
if (mod.channel[ch].volume<0) mod.channel[ch].volume=0;
}
}
Protracker.prototype.effect_t1_b=function(mod, ch) { // b pattern jump
}
Protracker.prototype.effect_t1_c=function(mod, ch) { // c set volume
}
Protracker.prototype.effect_t1_d=function(mod, ch) { // d pattern break
}
Protracker.prototype.effect_t1_e=function(mod, ch) { // e
var i=(mod.channel[ch].data&0xf0)>>4;
mod.effects_t1_e[i](mod, ch);
}
Protracker.prototype.effect_t1_f=function(mod, ch) { // f
}
//
// tick 1+ effect e functions
//
Protracker.prototype.effect_t1_e0=function(mod, ch) { // e0
}
Protracker.prototype.effect_t1_e1=function(mod, ch) { // e1
}
Protracker.prototype.effect_t1_e2=function(mod, ch) { // e2
}
Protracker.prototype.effect_t1_e3=function(mod, ch) { // e3
}
Protracker.prototype.effect_t1_e4=function(mod, ch) { // e4
}
Protracker.prototype.effect_t1_e5=function(mod, ch) { // e5
}
Protracker.prototype.effect_t1_e6=function(mod, ch) { // e6
}
Protracker.prototype.effect_t1_e7=function(mod, ch) { // e7
}
Protracker.prototype.effect_t1_e8=function(mod, ch) { // e8
}
Protracker.prototype.effect_t1_e9=function(mod, ch) { // e9 retrig sample
if (mod.tick%(mod.channel[ch].data&0x0f)==0)
mod.channel[ch].samplepos=0;
}
Protracker.prototype.effect_t1_ea=function(mod, ch) { // ea
}
Protracker.prototype.effect_t1_eb=function(mod, ch) { // eb
}
Protracker.prototype.effect_t1_ec=function(mod, ch) { // ec cut sample
if (mod.tick==(mod.channel[ch].data&0x0f))
mod.channel[ch].volume=0;
}
Protracker.prototype.effect_t1_ed=function(mod, ch) { // ed delay sample
mod.effect_t0_ed(mod, ch);
}
Protracker.prototype.effect_t1_ee=function(mod, ch) { // ee
}
Protracker.prototype.effect_t1_ef=function(mod, ch) { // ef
}
+251
View File
@@ -0,0 +1,251 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('map', 'assets/tilemaps/maps/features_test.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
game.load.image('walls_1x2', 'assets/tilemaps/tiles/walls_1x2.png');
game.load.image('tiles2', 'assets/tilemaps/tiles/tiles2.png');
game.load.image('ball', 'assets/sprites/shinyball.png');
game.load.image('phaser', 'assets/sprites/firstaid.png');
// game.load.image('phaser', 'assets/sprites/atari130xe.png');
// game.load.image('phaser', 'assets/sprites/mushroom2.png');
}
var cursors;
var map;
var coins;
var layer;
var layer2;
var layer3;
var sprite;
var ball;
var marker;
function create() {
console.log(' --- state create start ---');
$('#step').click(function(){
game.step();
});
game.stage.backgroundColor = '#124184';
marker = new Phaser.Line(256, 0, 256, 600);
map = game.add.tilemap('map');
map.addTilesetImage('ground_1x1');
map.addTilesetImage('walls_1x2');
map.addTilesetImage('tiles2');
map.setCollisionBetween(1, 12);
layer = map.createLayer('Tile Layer 1');
// layer.debug = true;
layer.resizeWorld();
game.physics.gravity.y = 200;
sprite = game.add.sprite(100, 240, 'phaser');
// sprite.anchor.setTo(0.5, 0.5);
// sprite.body.setCircle(20);
// sprite.body.moves = false;
ball = game.add.sprite(200, 180, 'ball');
// game.add.tween(sprite).to({x: 500},5000,Phaser.Easing.Linear.None,true);
// game.add.tween(sprite).to({angle: 360},5000,Phaser.Easing.Linear.None,true);
// sprite = game.add.sprite(200, 240, 'phaser'); // 3-block corner test
// sprite.debug = true;
// game.stepping = true;
// sprite.body.velocity.y = -300;
// sprite.body.velocity.y = 200;
// We'll set a lower max angular velocity here to keep it from going totally nuts
// sprite.body.maxAngular = 500;
// Apply a drag otherwise the sprite will just spin and never slow down
// sprite.body.angularDrag = 50;
// sprite.body.bounce.x = 0.8;
sprite.body.bounce.y = 0.8;
// sprite.angle = 35;
game.camera.follow(sprite);
// game.input.onDown.add(getIt, this);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
// console.log(' --- state update start ---');
/*
if (cursors.left.isDown)
{
game.camera.x -= 1;
}
else if (cursors.right.isDown)
{
game.camera.x += 1;
}
if (cursors.up.isDown)
{
layer.scrollY -= 4;
}
else if (cursors.down.isDown)
{
layer.scrollY += 4;
}
*/
game.physics.collide(sprite, layer);
game.physics.collide(sprite, ball);
// sprite.body.velocity.y = -300;
// sprite.body.angularVelocity = -100;
// sprite.body.angularAcceleration = 200;
// sprite.body.velocity.x = 0;
// sprite.body.velocity.y = 0;
sprite.body.angularVelocity = 0;
// sprite.body.acceleration.x = 0;
// sprite.body.angularAcceleration = 0;
if (cursors.left.isDown)
{
// sprite.body.acceleration.x = -200;
sprite.body.angularVelocity = -300;
// sprite.body.angularAcceleration -= 200;
}
else if (cursors.right.isDown)
{
// sprite.body.acceleration.x = 200;
sprite.body.angularVelocity = 300;
// sprite.body.angularAcceleration += 200;
}
if (cursors.up.isDown)
{
game.physics.velocityFromAngle(sprite.angle, 300, sprite.body.velocity);
}
/*
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
sprite.angle = sprite.angle + 1;
*/
// if (cursors.up.isDown)
// {
// sprite.body.velocity.y = -200;
// }
// else if (cursors.down.isDown)
// {
// sprite.body.velocity.y = 100;
// }
// if (cursors.left.isDown)
// {
// sprite.body.velocity.x = -200;
// }
// else if (cursors.right.isDown)
// {
// sprite.body.velocity.x = 200;
// }
// if (cursors.up.isDown)
// {
// sprite.y -= 2;
// }
// else if (cursors.down.isDown)
// {
// sprite.y += 2;
// }
// if (cursors.left.isDown)
// {
// sprite.x -= 2;
// }
// else if (cursors.right.isDown)
// {
// sprite.x += 2;
// }
// if (cursors.up.isDown)
// {
// sprite.body.y -= 2;
// }
// else if (cursors.down.isDown)
// {
// sprite.body.y += 2;
// }
// if (cursors.left.isDown)
// {
// sprite.body.x -= 2;
// }
// else if (cursors.right.isDown)
// {
// sprite.body.x += 2;
// }
}
function render() {
// game.debug.renderSpriteBody(sprite);
// game.debug.renderSpriteBounds(sprite);
game.debug.renderPhysicsBody(sprite.body);
game.debug.renderBodyInfo(sprite, 32, 32);
// game.debug.renderLine(marker, 'rgba(255,255,255,0.5)');
// game.debug.renderText(sprite.deltaX, 32, 32);
// game.debug.renderText(sprite.deltaY, 32, 48);
// game.debug.renderText(sprite.body.deltaX(), 232, 32);
// game.debug.renderText(sprite.body.deltaY(), 232, 48);
// game.debug.renderText(sprite.body.left, 32, 32);
// game.debug.renderText(sprite.body.top, 32, 48);
// game.debug.renderSpriteCoords(sprite, 32, 320);
}
+83
View File
@@ -0,0 +1,83 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
}
var body;
var tile;
function create() {
body = { left: 386, top: 89, right: 412, bottom: 127 };
tile = { x: 416, y: 96, right: 448, bottom: 128 };
game.input.keyboard.addKeyCapture([Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.UP, Phaser.Keyboard.DOWN]);
}
function tileIntersects (body, tile) {
if (body.width <= 0 || body.height <= 0 || tile.width <= 0 || tile.height <= 0)
{
// return false;
}
var test1 = body.right < tile.x;
var test2 = body.bottom < tile.y;
var test3 = body.left > tile.right;
var test4 = body.top > tile.bottom;
result = !(body.right < tile.x || body.bottom < tile.y || body.left > tile.right || body.top > tile.bottom);
// game.debug.renderText('intersects: ' + result, 32, 32);
// game.debug.renderText('test 1: ' + test1, 320, 60);
// game.debug.renderText('test 2: ' + test2, 320, 80);
// game.debug.renderText('test 3: ' + test3, 320, 100);
// game.debug.renderText('test 4: ' + test4, 320, 120);
return result;
}
function update() {
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
body.left -= 1;
body.right -= 1;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
body.left += 1;
body.right += 1;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
body.top -= 1;
body.bottom -= 1;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
{
body.top += 1;
body.bottom += 1;
}
}
function render() {
var r = tileIntersects(body, tile);
game.context.fillStyle = 'rgb(255,0,0)';
game.context.fillRect(body.left, body.top, body.right-body.left,body.bottom-body.top);
game.context.fillStyle = 'rgb(0,255,0)';
game.context.fillRect(tile.x, tile.y, tile.right-tile.x,tile.bottom-tile.y);
}
+51
View File
@@ -0,0 +1,51 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
var logo;
var background;
var filter;
function preload() {
game.load.image('phaser', 'assets/sprites/phaser2.png');
game.load.script('fire', '../filters/Fire.js');
}
function create() {
logo = game.add.sprite(game.world.centerX, game.world.centerY, 'phaser');
logo.anchor.setTo(0.5, 0.5);
background = game.add.sprite(0, 0);
background.width = 800;
background.height = 600;
filter = game.add.filter('Fire', 800, 600);
filter.alpha = 0.0;
background.filters = [filter];
game.input.onDown.add(removeBackground, this);
}
function update() {
filter.update();
}
function removeBackground() {
console.log('removeBackground');
// background.filters = null;
background.destroy();
// console.log(background.filters);
}
+87
View File
@@ -0,0 +1,87 @@
var config = {
width: "100%",
height: "100%",
renderer: Phaser.WEBGL,
parent: 'phaser-example',
state: { preload: preload, create: create, update: update },
backgroundColor: '#ff0000'
};
var game = new Phaser.Game(config);
function preload() {
game.load.image('spin1', 'assets/sprites/spinObj_01.png');
game.load.image('spin2', 'assets/sprites/spinObj_02.png');
game.load.image('spin3', 'assets/sprites/spinObj_03.png');
game.load.image('spin4', 'assets/sprites/spinObj_04.png');
game.load.image('spin5', 'assets/sprites/spinObj_05.png');
game.load.image('spin6', 'assets/sprites/spinObj_06.png');
game.load.image('spin7', 'assets/sprites/spinObj_07.png');
game.load.image('spin8', 'assets/sprites/spinObj_08.png');
}
var renderTexture;
var renderTexture2;
var currentTexture;
var outputSprite;
var stuffContainer;
var count = 0;
function create() {
// create two render textures.. these dynamic textures will be used to draw the scene into itself
renderTexture = game.add.renderTexture('texture1', game.width, game.height);
renderTexture2 = game.add.renderTexture('textur2e', game.width, game.height);
currentTexture = renderTexture;
// create a new sprite that uses the render texture we created above
outputSprite = game.add.sprite(game.width/2, game.height/2, currentTexture);
// align the sprite
outputSprite.anchor.x = 0.5;
outputSprite.anchor.y = 0.5;
stuffContainer = game.add.group();
stuffContainer.x = game.width/2;
stuffContainer.y = game.height/2;
// now create some items and randomly position them in the stuff container
for (var i = 0; i < 20; i++)
{
var item = stuffContainer.create(Math.random() * 400 - 200, Math.random() * 400 - 200, game.rnd.pick(game.cache.getImageKeys()));
item.anchor.setTo(0.5, 0.5);
}
// used for spinning!
count = 0;
}
function update() {
stuffContainer.addAll('rotation', 0.1);
count += 0.01;
// swap the buffers..
var temp = renderTexture;
renderTexture = renderTexture2;
renderTexture2 = temp;
// set the new texture
outputSprite.setTexture(renderTexture);
// twist this up!
stuffContainer.rotation -= 0.01
outputSprite.scale.x = outputSprite.scale.y = 1 + Math.sin(count) * 0.2;
// render the stage to the texture
// the true clears the texture before content is rendered
renderTexture2.renderXY(game.stage.display, 0, 0, true);
}
+184
View File
@@ -0,0 +1,184 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
}
var sprite;
var sprite2;
var sprite3;
var bmd;
var b1;
var b2;
var b3;
var r;
function create() {
game.stage.backgroundColor = '#124184';
test1();
bmd = game.add.bitmapData(800, 600);
var bg = game.add.sprite(0, 0, bmd);
bg.body.moves = false;
bmd.clear();
bmd.context.lineWidth = 1;
}
function test1() {
sprite = game.add.sprite(500, 400, 'gameboy', 0);
// sprite = game.add.sprite(0, 0, 'gameboy', 0);
sprite.name = 'red';
// sprite.pivot.x = 20;
// sprite.pivot.y = 30;
// sprite.anchor.setTo(0.5, 0.5);
// sprite.body.polygon.translate(20, 20);
// sprite.body.polygon.rotate(game.math.degToRad(45));
// sprite.angle = 45;
// sprite.scale.setTo(2, 2);
// sprite.body.polygon.scale(2, 2);
sprite.body.bounce.x = 0.8;
sprite2 = game.add.sprite(0, 400, 'gameboy', 2);
sprite2.name = 'green';
// sprite2.body = null;
sprite3 = game.add.sprite(700, 400, 'gameboy', 3);
sprite3.name = 'yellow';
// sprite3.body = null;
// b1 = new SAT.Box(new SAT.Vector(sprite.x, sprite.y), sprite.width, sprite.height);
// b2 = new SAT.Box(new SAT.Vector(sprite2.x, sprite2.y), sprite2.width, sprite2.height);
// b3 = new SAT.Box(new SAT.Vector(sprite3.x, sprite3.y), sprite3.width, sprite3.height);
// r = new SAT.Response();
// console.log(b2);
game.input.onDown.add(launch1, this);
}
function launch1() {
// console.log(b1);
sprite.body.velocity.x = -100;
// sprite2.body.velocity.x = -225;
}
function update() {
// sprite.angle += 0.5;
// sprite.body.polygon.rotate(game.math.degToRad(0.5));
/*
b1.pos.x = sprite.x;
b1.pos.y = sprite.y;
b2.pos.x = sprite2.x;
b2.pos.y = sprite2.y;
b3.pos.x = sprite3.x;
b3.pos.y = sprite3.y;
r.clear();
if (SAT.testPolygonPolygon(b1.toPolygon(), b2.toPolygon(), r))
{
console.log(r, b1.pos, b2.pos);
// b1.pos.sub(r.overlapV);
// sprite.body.velocity.x *= -sprite.body.bounce.x;
sprite.body.velocity.x = 0;
sprite.x += Math.abs(r.overlapV.x) + 1;
console.log('sprite moved to', sprite.x);
// sprite.x = b1.pos.x;
// sprite.y = b1.pos.y;
}
*/
sprite.body.collide(sprite2.body);
bmd.clear();
if (sprite)
{
renderPolygon(sprite.body.polygon);
// bmd.fillStyle('rgba(255,0,0,0.8');
// bmd.fillRect(sprite.body.polygon.pos.x, sprite.body.polygon.pos.y, sprite.body.shape.w, sprite.body.shape.h);
}
if (sprite2)
{
renderPolygon(sprite2.body.polygon);
// bmd.fillStyle('rgba(0,255,0,0.8');
// bmd.fillRect(sprite2.body.polygon.pos.x, sprite2.body.polygon.pos.y, sprite2.body.shape.w, sprite2.body.shape.h);
}
if (sprite3)
{
renderPolygon(sprite3.body.polygon);
// bmd.fillStyle('rgba(0,0,255,0.8');
// bmd.fillRect(sprite3.body.polygon.pos.x, sprite3.body.polygon.pos.y, sprite3.body.shape.w, sprite3.body.shape.h);
}
}
function renderPolygon (polygon, color) {
// console.log(polygon);
// this.start(0, 0, color);
bmd.context.beginPath();
bmd.context.moveTo(polygon.pos.x + polygon.points[0].x, polygon.pos.y + polygon.points[0].y);
for (var i = 1; i < polygon.points.length; i++)
{
bmd.context.lineTo(polygon.pos.x + polygon.points[i].x, polygon.pos.y + polygon.points[i].y);
}
bmd.context.closePath();
bmd.context.strokeStyle = 'rgba(255, 255, 255, 1)';
// bmd.context.strokeStyle = color;
bmd.context.stroke();
}
function render() {
if (sprite)
{
// game.debug.renderBodyInfo(sprite, 16, 24);
game.debug.renderText(sprite.name + ' x: ' + sprite.x, 16, 500);
game.debug.renderText(sprite.name + ' world x: ' + sprite.world.x, 16, 520);
}
if (sprite2)
{
// game.debug.renderBodyInfo(sprite2, 16, 190);
game.debug.renderText(sprite2.name + ' x: ' + sprite2.x, 400, 500);
}
}
+93
View File
@@ -0,0 +1,93 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
function preload() {
game.load.tilemap('level3', 'assets/tilemaps/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('tiles', 'assets/tilemaps/tiles/cybernoid.png', 16, 16);
game.load.image('phaser', 'assets/sprites/phaser-ship.png');
game.load.image('chunk', 'assets/sprites/chunk.png');
}
var map;
var layer;
var cursors;
var sprite;
var emitter;
function create() {
// A Tilemap object just holds the data needed to describe the map (i.e. the json exported from Tiled, or the CSV exported from elsewhere).
// You can add your own data or manipulate the data (swap tiles around, etc) but in order to display it you need to create a TilemapLayer.
map = game.add.tilemap('level3');
map.addTilesetImage('CybernoidMap3BG_bank.png', 'tiles');
layer = map.createLayer(0);
// Basically this sets EVERY SINGLE tile to fully collide on all faces
map.setCollisionByExclusion([7, 32, 35, 36, 47]);
// layer.debug = true;
layer.resizeWorld();
cursors = game.input.keyboard.createCursorKeys();
emitter = game.add.emitter(0, 0, 200);
emitter.makeParticles('chunk');
emitter.minRotation = 0;
emitter.maxRotation = 0;
emitter.gravity = 150;
emitter.bounce.setTo(0.5, 0.5);
sprite = game.add.sprite(200, 70, 'phaser');
sprite.anchor.setTo(0.5, 0.5);
game.camera.follow(sprite);
}
function particleBurst() {
emitter.x = sprite.x;
emitter.y = sprite.y;
emitter.start(true, 2000, null, 1);
}
function update() {
game.physics.collide(sprite, layer);
game.physics.collide(emitter, layer);
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
if (cursors.up.isDown)
{
sprite.body.velocity.y = -150;
particleBurst();
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 150;
particleBurst();
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -150;
sprite.scale.x = -1;
particleBurst();
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 150;
sprite.scale.x = 1;
particleBurst();
}
}
+32
View File
@@ -0,0 +1,32 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
}
var snake;
var bmd;
var cursors;
function create() {
bmd = game.add.bitmapData(32, 32);
bmd.context.fillStyle = 'rgb(0, 255, 0)';
bmd.context.fillRect(0, 0, 32, 32);
snake = game.add.group();
sprite = game.add.sprite(game.world.randomX - 32, game.world.randomY - 64, bmd);
}
function update() {
}
function render() {
}
+169
View File
@@ -0,0 +1,169 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('snake','assets/games/snake/sprites.png', 32, 32);
game.load.image('ball', 'assets/sprites/pangball.png');
}
var snake;
var snakeHead; //head of snake sprite
var snakePath = []; //arrary of positions(points) that have to be stored for the path the sections follow
var numSnakeSections = 10; //number of snake body sections
var snakeSpacer = 6; //parameter that sets the spacing between sections
var snakeSpeed = 250;
var snakeLength = 10;
var fruit;
var snakeCircles = [];
var node;
var ball;
function create() {
game.world.setBounds(0, 0, 800, 600);
game.stage.backgroundColor = '#0f4e93';
cursors = game.input.keyboard.createCursorKeys();
snake = game.add.group();
snakeHead = snake.create(400, 300, 'snake', 5);
snakeHead.anchor.setTo(0.5, 0.5);
snakeHead.animations.add('chomp', [5, 6], 10, true);
snakeHead.play('chomp');
snakeHead.body.collideWorldBounds = true;
snakeCircles.push(new Phaser.Circle(snakeHead.x, snakeHead.y, 16));
// Init snakeSection array
for (var i = 1; i <= numSnakeSections-1; i++)
{
var snakeSection = snake.create(400, 300, 'snake', 7);
snakeSection.anchor.setTo(0.5, 0.5);
snakeSection.animations.add('walk', [7, 8, 9, 10], 10, true);
snakeSection.play('walk');
snakeCircles.push(new Phaser.Circle(400, 300, 16));
if (i > snakeLength)
{
snakeSection.visible = false;
}
}
node = new Phaser.Circle(300, 300, 170);
ball = game.add.sprite(node.x, node.y, 'ball');
ball.anchor.setTo(0.5, 0.5);
// fruit = game.add.group();
// for (var i = 0; i < 20; i++)
// {
// fruit.create(game.world.randomX, game.world.randomY, 'snake', 2);
// }
// Init snakePath array
for (var i = 0; i <= numSnakeSections * snakeSpacer; i++)
{
snakePath[i] = new Phaser.Point(400, 300);
}
}
function newNode() {
node.x = game.rnd.integerInRange(100, 700);
node.y = game.rnd.integerInRange(100, 500);
ball.x = node.x;
ball.y = node.y;
ball.alpha = 1;
}
function update() {
// game.physics.collide(snakeHead, fruit, eatFruit);
var enclosed = true;
for (var i = 0; i <= numSnakeSections - 1; i++)
{
if (Phaser.Circle.intersects(snakeCircles[i], node) === false)
{
enclosed = false;
break;
}
}
if (enclosed)
{
ball.alpha -= 0.02;
if (ball.alpha <= 0)
{
newNode();
}
}
snakeHead.body.angularVelocity = 0;
// if (cursors.up.isDown)
// {
snakeHead.body.velocity.copyFrom(game.physics.velocityFromAngle(snakeHead.angle, snakeSpeed));
snakeCircles[0].x = snakeHead.x;
snakeCircles[0].y = snakeHead.y;
var part = snakePath.pop();
part.setTo(snakeHead.x, snakeHead.y);
snakePath.unshift(part);
for (var i = 1; i <= numSnakeSections - 1; i++)
{
var x = (snakePath[i * snakeSpacer]).x;
var y = (snakePath[i * snakeSpacer]).y;
snake.getAt(i).x = x;
snake.getAt(i).y = y;
snakeCircles[i].x = x;
snakeCircles[i].y = y;
}
// }
if (cursors.left.isDown)
{
snakeHead.body.angularVelocity = -220;
}
else if (cursors.right.isDown)
{
snakeHead.body.angularVelocity = 220;
}
}
function eatFruit(snakeHead, fruit) {
fruit.kill();
snakeLength++;
snake.getAt(snakeLength).visible = true;
snakeSpeed += 10;
}
function render() {
//game.debug.renderCircle(node);
for (var i = 0; i <= numSnakeSections - 1; i++)
{
//game.debug.renderCircle(snakeCircles[i], 'rgb(255,0,0)');
}
}
+79
View File
@@ -0,0 +1,79 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
function preload() {
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
game.load.tilemap('desert', 'assets/maps/burd.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/maps/ground_1x1.png', 32, 32);
game.load.spritesheet('trees', 'assets/maps/walls_1x2.png', 32, 64);
}
var map;
var tileset;
var layer;
var sprite;
var group;
var oldY = 0;
var cursors;
function create() {
// Create our tilemap to walk around
map = game.add.tilemap('desert');
tileset = game.add.tileset('tiles');
layer = game.add.tilemapLayer(0, 0, 800, 600, tileset, map, 0);
// This group will hold the main player + all the tree sprites to depth sort against
group = game.add.group();
// The player
sprite = group.create(300, 200, 'phaser');
// Some trees
for (var i = 0; i < 50; i++)
{
var x = game.math.snapTo(game.world.randomX, 32);
var y = game.math.snapTo(game.world.randomY, 32);
group.create(x, y, 'trees', game.rnd.integerInRange(0, 8));
}
// Move it
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
if (cursors.left.isDown)
{
sprite.body.velocity.x = -200;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 200;
}
if (cursors.up.isDown)
{
sprite.body.velocity.y = -200;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 200;
}
if (sprite.y !== oldY)
{
// Group.sort() is an expensive operation
// You really want to minimise how often it is called as much as possible.
// So this little check helps at least, but if you can do it even less than this.
group.sort();
oldY = sprite.y;
}
}
+95
View File
@@ -0,0 +1,95 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari', 'assets/sprites/atari130xe.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('backdrop', 'assets/pics/remember-me.jpg');
}
var sprite1;
var sprite2;
var cursors;
function create() {
game.world.setBounds(0, 0, 1920, 1200);
game.add.sprite(0, 0, 'backdrop');
game.stage.backgroundColor = '#2d2d2d';
// This will check Sprite vs. Sprite collision
sprite1 = game.add.sprite(50, 250, 'atari');
sprite1.name = 'atari';
sprite1.body.immovable = true;
sprite2 = game.add.sprite(32, 32, 'mushroom');
sprite2.name = 'mushroom';
sprite2.body.setCircle(32);
game.camera.follow(sprite1);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
game.physics.collide(sprite1, sprite2);
if (cursors.left.isDown)
{
sprite1.body.x -= 4;
}
else if (cursors.right.isDown)
{
sprite1.body.x += 4;
}
if (cursors.up.isDown)
{
sprite1.body.y -= 4;
}
else if (cursors.down.isDown)
{
sprite1.body.y += 4;
}
/*
sprite1.x = game.input.x;
if (game.input.y > 300)
{
sprite1.y = game.input.y;
}
else
{
sprite1.y = 300;
}
if (sprite2.y > 600)
{
sprite2.body.x = 0;
sprite2.body.y = 0;
}
*/
sprite2.body.velocity.x = 50;
sprite2.body.velocity.y = 100;
}
function render() {
game.debug.renderPhysicsBody(sprite1.body);
game.debug.renderPhysicsBody(sprite2.body);
}
+125
View File
@@ -0,0 +1,125 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level1', 'assets/games/starstruck/level1.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/games/starstruck/tiles-1.png', 16, 16);
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48);
game.load.spritesheet('droid', 'assets/games/starstruck/droid.png', 32, 32);
game.load.image('starSmall', 'assets/games/starstruck/star.png');
game.load.image('starBig', 'assets/games/starstruck/star2.png');
game.load.image('background', 'assets/games/starstruck/background2.png');
}
var map;
var tileset;
var layer;
var player;
var facing = 'left';
var jumpTimer = 0;
var cursors;
var jumpButton;
var bg;
function create() {
game.stage.backgroundColor = '#000000';
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;
map = game.add.tilemap('level1');
tileset = game.add.tileset('tiles');
tileset.setCollisionRange(0, tileset.total - 1, true, true, true, true);
tileset.setCollisionRange(12, 16, false, false, false, false);
tileset.setCollisionRange(46, 50, false, false, false, false);
layer = game.add.tilemapLayer(0, 0, 800, 600, tileset, map, 0);
layer.resizeWorld();
player = game.add.sprite(32, 32, 'dude');
player.body.bounce.y = 0.2;
player.body.collideWorldBounds = true;
player.body.gravity.y = 6;
player.body.setSize(16, 32, 8, 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);
player.angle = 45;
game.camera.follow(player);
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
}
function update() {
game.physics.collide(player, layer);
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.touching.down && game.time.now > jumpTimer)
{
player.body.velocity.y = -250;
jumpTimer = game.time.now + 750;
}
// player.scale.x += 0.001;
// player.scale.y += 0.001;
}
function render () {
game.debug.renderCameraInfo(game.camera, 32, 32);
game.debug.renderSpriteBody(player);
}
+81
View File
@@ -0,0 +1,81 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update});
function preload() {
game.load.tilemap('background', 'assets/maps/smb_bg.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tilesBackground', 'assets/maps/smb_bg.png',16,16);
game.load.tilemap('level1', 'assets/maps/smb_level1.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tilesLevel1', 'assets/maps/smb_tiles.png',16,16);
}
var mapBg;
var tilesetBg;
var layerBg;
var mapLevel1;
var tilesetLevel1;
var layerLevel1;
var cursors;
function create() {
game.stage.backgroundColor = '#787878';
mapBg = game.add.tilemap('background');
tilesetBg = game.add.tileset('tilesBackground');
layerBg = game.add.tilemapLayer(0, 0, mapBg.layers[0].width*tilesetBg.tileWidth, 600, tilesetBg, mapBg, 0);
layerBg.fixedToCamera=false;
layerBg.resizeWorld();
mapLevel1 = game.add.tilemap('level1');
tilesetLevel1 = game.add.tileset('tilesLevel1');
layerLevel1 = game.add.tilemapLayer(0, 0, mapLevel1.layers[0].width*tilesetLevel1.tileWidth, 600, tilesetLevel1, mapLevel1, 0);
layerLevel1.fixedToCamera=false;
cursors=game.input.keyboard.createCursorKeys();
balls = game.add.emitter(300, 50, 500);
balls.bounce = 0.5;
balls.makeParticles('balls', [0,1,2,3,4,5], 500, 1);
balls.minParticleSpeed.setTo(-150, 150);
balls.maxParticleSpeed.setTo(100, 100);
balls.gravity = 8;
balls.start(false, 5000, 50);
game.add.tween(balls).to({ x: 4000 }, 7500, Phaser.Easing.Sinusoidal.InOut, true, 0, 1000, true);
}
function update() {
if (cursors.left.isDown)
{
game.camera.x -= 18;
}
else if (cursors.right.isDown)
{
game.camera.x += 18;
}
if (cursors.up.isDown)
{
game.camera.y -= 18;
}
else if (cursors.down.isDown)
{
game.camera.y += 18;
}
}
+95
View File
@@ -0,0 +1,95 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update});
function preload() {
game.load.tilemap('background', 'assets/maps/smb_bg.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tilesBackground', 'assets/maps/smb_bg.png',16,16);
game.load.tilemap('level1', 'assets/maps/smb_level1.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tilesLevel1', 'assets/maps/smb_tiles.png',16,16);
game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17);
}
var mapBg;
var tilesetBg;
var layerBg;
var mapLevel1;
var tilesetLevel1;
var layerLevel1;
var cursors;
var balls;
function create() {
game.stage.backgroundColor = '#787878';
mapBg = game.add.tilemap('background');
tilesetBg = game.add.tileset('tilesBackground');
layerBg = game.add.tilemapLayer(0, 0, mapBg.layers[0].width*tilesetBg.tileWidth, 600, tilesetBg, mapBg, 0);
layerBg.fixedToCamera=false;
layerBg.resizeWorld();
mapLevel1 = game.add.tilemap('level1');
tilesetLevel1 = game.add.tileset('tilesLevel1');
tilesetLevel1.setCollisionRange(9,11,true,true,true,true);
tilesetLevel1.setCollisionRange(14,19,true,true,true,true);
tilesetLevel1.setCollisionRange(22,24,true,true,true,true);
tilesetLevel1.setCollisionRange(37,38,true,true,true,true);
tilesetLevel1.setCollision(32,true,true,true,true);
layerLevel1 = game.add.tilemapLayer(0, 0, mapLevel1.layers[0].width*tilesetLevel1.tileWidth, 600, tilesetLevel1, mapLevel1, 0);
layerLevel1.fixedToCamera=false;
layerLevel1.resizeWorld();
cursors=game.input.keyboard.createCursorKeys();
balls = game.add.emitter(300, 50, 500);
balls.bounce = 0.5;
balls.makeParticles('balls', [0,1,2,3,4,5], 500, 1);
balls.minParticleSpeed.setTo(-150, 150);
balls.maxParticleSpeed.setTo(100, 100);
balls.gravity = 8;
balls.start(false, 5000, 50);
game.add.tween(balls).to({ x: 4000 }, 7500, Phaser.Easing.Sinusoidal.InOut, true, 0, 1000, true);
}
function update() {
game.physics.collide(balls,layerLevel1);
if (cursors.left.isDown)
{
game.camera.x -= 18;
}
else if (cursors.right.isDown)
{
game.camera.x += 18;
}
if (cursors.up.isDown)
{
game.camera.y -= 18;
}
else if (cursors.down.isDown)
{
game.camera.y += 18;
}
}
+89
View File
@@ -0,0 +1,89 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg32wh37.png', 32, 32);
}
var group;
var start = false;
var swapCount = 0;
var time = 0;
var test = 0;
function create() {
game.stage.backgroundColor = '#2d2d2d';
group = game.add.group();
for (var i = 0; i < 500; i++)
{
var c = group.create(game.world.randomX, game.world.randomY, 'veggies', game.rnd.integerInRange(0, 36));
c.name = 'veg' + i;
}
test = group.length;
game.input.onUp.add(toggleSwap, this);
}
function toggleSwap () {
if (start)
{
start = false;
}
else
{
start = true;
}
}
function update() {
if (start && game.time.now > time)
{
var a = group.getRandom();
var b = group.getRandom();
if (a.name !== b.name)
{
// console.log('************************ NEW ROUND *********************');
// group.dump(true);
// console.log('Group Size: ' + group.length);
group.swap(a, b);
swapCount++;
if (group.length !== test)
{
start = false;
console.log('************************ SHIT *********************');
group.dump(true);
console.log('************************ SHIT *********************');
}
if (group.validate() == false)
{
start = false;
console.log('************************ VALIDATE FAIL *********************');
group.dump(true);
console.log('************************ VALIDATE FAIL *********************');
}
}
// time = game.time.now + 50;
}
}
function render() {
game.debug.renderText('Swap: ' + swapCount, 32, 32);
}
+61
View File
@@ -0,0 +1,61 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render : render });
function preload() {
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var cursors;
var group;
var text;
var timer;
function create() {
game.stage.backgroundColor = '#007236';
for (var i = 0; i < 200; i++)
{
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
}
text = game.add.text(0, 0, "Text Above Sprites", { font: "64px Arial", fill: "#00bff3", align: "center" });
timer = new Phaser.Timer(game);
timer.add(1000);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
timer.update();
if (cursors.up.isDown)
{
text.y -= 4;
}
else if (cursors.down.isDown)
{
text.y += 4;
}
if (cursors.left.isDown)
{
text.x -= 4;
}
else if (cursors.right.isDown)
{
text.x += 4;
}
}
function render() {
// game.debug.renderCameraInfo(game.camera, 32, 32);
}
+105
View File
@@ -0,0 +1,105 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
function preload() {
game.load.image('phaser', 'assets/sprites/phaser1.png');
game.load.spritesheet('arrows', 'assets/sprites/arrows.png', 23, 31);
}
var arrowStart;
var arrowEnd;
var sprite;
function create() {
game.stage.backgroundColor = '#2384e7';
// game.physics.setBoundsToWorld(true, true, false, false);
// arrowStart = game.add.sprite(100, 100, 'arrows', 0);
// arrowEnd = game.add.sprite(400, 100, 'arrows', 1);
// sprite = game.add.sprite(100, 164, 'phaser');
sprite = game.add.sprite(300, 364, 'phaser');
// sprite.anchor.setTo(0.5, 0.5);
sprite.inputEnabled = true;
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(1, 1);
sprite.alpha = 0.2;
// sprite.body.allowRotation = true;
// sprite.body.polygon.translate(100, 0);
// sprite.body.setPolygon([new SAT.Vector(0,0), new SAT.Vector(60, 0), new SAT.Vector(100, 40), new SAT.Vector(60, 80), new SAT.Vector(0, 80)], -50, -40);
sprite.body.setPolygon([new SAT.Vector(0,50), new SAT.Vector(100, 50), new SAT.Vector(100, 0), new SAT.Vector(150, 0), new SAT.Vector(150, 150), new SAT.Vector(100, 150), new SAT.Vector(100, 100), new SAT.Vector(0, 100), new SAT.Vector(0, 50)], -100, -100);
// sprite.body.setCircle(50);
// sprite.body.offset.setTo(50, 50);
// console.log(sprite.body.x, sprite.body.y, sprite.body.shape.radius);
sprite.events.onInputDown.add(move, this);
// sprite.body.polygons.rotate(0.4);
// sprite.rotation = 0.4;
// console.log(sprite.body.polygons);
}
function move() {
console.log('move');
sprite.body.velocity.x = 200;
sprite.body.velocity.y = -200;
// sprite.body.angularVelocity = 2;
// sprite.rotation = 0.4;
// console.log(sprite.body.polygon.points);
// to: function (properties, duration, ease, autoStart, delay, repeat, yoyo) {
// game.add.tween(sprite).to( { angle: 359 }, 8000, Phaser.Easing.Linear.None, true, 0, 1000, false);
// sprite.scale.setTo(2, 2);
if (sprite.x === 100)
{
// Here you'll notice we are using a relative value for the tween.
// You can specify a number as a string with either + or - at the start of it.
// When the tween starts it will take the sprites current X value and add +300 to it.
// game.add.tween(sprite).to( { x: '+300' }, 2000, Phaser.Easing.Linear.None, true);
}
else if (sprite.x === 400)
{
// game.add.tween(sprite).to( { x: '-300' }, 2000, Phaser.Easing.Linear.None, true);
}
}
function render() {
if (sprite.x === 100 || sprite.x === 400)
{
// game.debug.renderText('Click sprite to tween', 32, 32);
}
// game.debug.renderText('x: ' + arrowStart.x, arrowStart.x, arrowStart.y - 4);
// game.debug.renderText('x: ' + arrowEnd.x, arrowEnd.x, arrowEnd.y - 4);
game.debug.renderText('sprite.x: ' + sprite.x + ' deltaX: ' + sprite.deltaX, 32, 32);
game.debug.renderText('sprite.y: ' + sprite.y + ' deltaY: ' + sprite.deltaY, 32, 48);
game.debug.renderPhysicsBody(sprite.body);
game.debug.renderPoint(sprite.center);
}
+217
View File
@@ -0,0 +1,217 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
// game.load.tilemap('map', 'assets/maps/super_mario.json', null, Phaser.Tilemap.TILED_JSON);
// game.load.tileset('tiles', 'assets/maps/super_mario.png', 16, 16);
game.load.tilemap('map', 'assets/tilemaps/maps/features_test.json', null, Phaser.Tilemap.TILED_JSON);
// game.load.image('ground', 'assets/maps/ground_1x1.png');
game.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
game.load.image('walls_1x2', 'assets/tilemaps/tiles/walls_1x2.png');
game.load.image('tiles2', 'assets/tilemaps/tiles/tiles2.png');
// game.load.tileset('tiles', 'assets/maps/ground_1x1.png', 32, 32);
// game.load.image('phaser', 'assets/sprites/phaser-ship.png');
// game.load.image('phaser', 'assets/sprites/mushroom2.png');
game.load.image('phaser', 'assets/sprites/wabbit.png');
// game.load.image('phaser', 'assets/sprites/arrow.png');
game.load.spritesheet('coin', 'assets/sprites/coin.png', 32, 32);
// game.load.image('phaser', 'assets/sprites/darkwing_crazy.png');
}
var cursors;
var map;
var coins;
var layer;
var layer2;
var layer3;
var sprite;
function create() {
map = game.add.tilemap('map');
map.addTilesetImage('ground_1x1');
map.addTilesetImage('walls_1x2');
map.addTilesetImage('tiles2');
map.setCollisionBetween(1, 12);
// mario tiles
// map.setCollisionByIndex(15);
// map.setCollisionByIndex(40);
// map.setCollisionByIndexRange(14, 16);
// map.setCollisionByIndexRange(20, 25);
// map.setCollisionByIndexRange(27, 29);
// layer3 = map.createLayer('Tile Layer 3');
// layer3.scrollFactorX = 0.5;
// layer2 = map.createLayer('Tile Layer 2');
// layer2.alpha = 0.5;
layer = map.createLayer('Tile Layer 1');
layer.debug = true;
layer.resizeWorld();
//coins =
map.createFromObjects('Object Layer 1', 34, 'coin', 0);
// layer2 = game.add.tilemapLayer(0, 0, 400, 600, null, map, 0);
// layer.cameraOffset.x = 400;
// layer.alpha = 0.5;
sprite = game.add.sprite(260, 100, 'phaser');
// sprite.anchor.setTo(0.5, 0.5);
// This adjusts the collision body size.
// 100x50 is the new width/height.
// 50, 25 is the X and Y offset of the newly sized box.
// In this case the box is 50px in and 25px down.
// sprite.body.setSize(16, 16, 8, 8);
// We'll set a lower max angular velocity here to keep it from going totally nuts
sprite.body.maxAngular = 500;
// Apply a drag otherwise the sprite will just spin and never slow down
sprite.body.angularDrag = 50;
// sprite.body.drag.x = 50;
// sprite.body.drag.y = 20;
// sprite.body.velocity.x = 50;
sprite.body.bounce.x = 0.8;
sprite.body.bounce.y = 0.8;
// sprite.angle = 35;
// game.camera.follow(sprite);
// game.input.onDown.add(getIt, this);
cursors = game.input.keyboard.createCursorKeys();
}
function getIt() {
// console.log('cam', game.camera.bounds);
// console.log('w', game.world.bounds);
// console.log(layer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true, true));
}
function update() {
/*
if (cursors.left.isDown)
{
game.camera.x -= 1;
}
else if (cursors.right.isDown)
{
game.camera.x += 1;
}
if (cursors.up.isDown)
{
layer.scrollY -= 4;
}
else if (cursors.down.isDown)
{
layer.scrollY += 4;
}
*/
game.physics.collide(sprite, layer);
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
sprite.body.angularVelocity = 0;
// sprite.body.acceleration.x = 0;
// sprite.body.angularAcceleration = 0;
/*
if (cursors.left.isDown)
{
// sprite.body.acceleration.x = -200;
sprite.body.angularVelocity = -300;
// sprite.body.angularAcceleration -= 200;
}
else if (cursors.right.isDown)
{
// sprite.body.acceleration.x = 200;
sprite.body.angularVelocity = 300;
// sprite.body.angularAcceleration += 200;
}
if (cursors.up.isDown)
{
game.physics.velocityFromAngle(sprite.angle, 300, sprite.body.velocity);
}
else
{
// game.physics.velocityFromAngle(sprite.angle, sprite.body.velocity, sprite.body.velocity);
}
*/
/*
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
sprite.angle = sprite.angle + 1;
*/
if (cursors.up.isDown)
{
sprite.body.velocity.y = -100;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 100;
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -100;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 100;
}
}
function render() {
// game.debug.renderSpriteBody(sprite);
// game.debug.renderSpriteBounds(sprite);
game.debug.renderPhysicsBody(sprite.body);
// game.debug.renderText(sprite.deltaX, 32, 32);
// game.debug.renderText(sprite.deltaY, 32, 48);
// game.debug.renderText(sprite.body.deltaX(), 232, 32);
// game.debug.renderText(sprite.body.deltaY(), 232, 48);
// game.debug.renderText(sprite.body.left, 32, 32);
// game.debug.renderText(sprite.body.right, 32, 48);
game.debug.renderSpriteCoords(sprite, 32, 32);
}
+53
View File
@@ -0,0 +1,53 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
function preload() {
game.load.image('ball', 'assets/sprites/pangball.png');
}
var t;
function create() {
game.stage.backgroundColor = '#6688ee';
t = game.time.create(false);
t.repeat(Phaser.Timer.SECOND * 2, 10, createBall, this);
t.repeat(Phaser.Timer.SECOND * 3, 10, createBall, this);
t.start();
game.input.onDown.add(killThem, this);
}
function createBall() {
// A bouncey ball sprite just to visually see what's going on.
var ball = game.add.sprite(game.world.randomX, 0, 'ball');
ball.body.gravity.y = 200;
ball.body.bounce.y = 0.5;
ball.body.collideWorldBounds = true;
console.log('nuked');
game.time.removeAll();
// t.removeAll();
}
function killThem() {
}
function render() {
game.debug.renderText("Time until event: " + t.duration.toFixed(0), 32, 32);
game.debug.renderText("Next tick: " + t.next.toFixed(0), 32, 64);
}
+55
View File
@@ -0,0 +1,55 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('phaser', 'assets/sprites/phaser1.png');
game.load.spritesheet('arrows', 'assets/sprites/arrows.png', 23, 31);
}
var arrowStart;
var arrowEnd;
var sprite;
function create() {
game.stage.backgroundColor = '#2384e7';
arrowStart = game.add.sprite(100, 100, 'arrows', 0);
arrowEnd = game.add.sprite(400, 100, 'arrows', 1);
sprite = game.add.sprite(100, 164, 'phaser');
sprite.inputEnabled = true;
sprite.events.onInputDown.add(move, this);
}
function move() {
var tween = game.add.tween(sprite).to( { x: '+500' }, 3000, Phaser.Easing.Linear.None, true);
tween.onComplete.add(over, this);
}
function over() {
console.log('done');
sprite.alpha = 0.5;
}
function update() {
if (sprite.x > 200)
{
sprite.x = 200;
}
}
function render() {
}
+102
View File
@@ -0,0 +1,102 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
function preload() {
game.load.image('phaser', 'assets/sprites/phaser1.png');
game.load.spritesheet('arrows', 'assets/sprites/arrows.png', 23, 31);
}
var arrowStart;
var arrowEnd;
var sprite;
function create() {
game.stage.backgroundColor = '#2384e7';
// game.physics.setBoundsToWorld(true, true, false, false);
// arrowStart = game.add.sprite(100, 100, 'arrows', 0);
// arrowEnd = game.add.sprite(400, 100, 'arrows', 1);
// sprite = game.add.sprite(100, 164, 'phaser');
sprite = game.add.sprite(300, 364, 'phaser');
sprite.anchor.setTo(0.5, 0.5);
sprite.inputEnabled = true;
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(1, 1);
// sprite.body.allowRotation = true;
// sprite.body.polygon.translate(100, 0);
sprite.body.setPolygon([new SAT.Vector(0,0), new SAT.Vector(60, 0), new SAT.Vector(100, 40), new SAT.Vector(60, 80), new SAT.Vector(0, 80)], -50, -40);
// sprite.body.setCircle(50);
// sprite.body.offset.setTo(50, 50);
// console.log(sprite.body.x, sprite.body.y, sprite.body.shape.radius);
sprite.events.onInputDown.add(move, this);
// sprite.body.polygons.rotate(0.4);
// sprite.rotation = 0.4;
// console.log(sprite.body.polygons);
}
function move() {
console.log('move');
sprite.body.velocity.x = 200;
sprite.body.velocity.y = -200;
// sprite.body.angularVelocity = 2;
// sprite.rotation = 0.4;
// console.log(sprite.body.polygon.points);
// to: function (properties, duration, ease, autoStart, delay, repeat, yoyo) {
game.add.tween(sprite).to( { angle: 359 }, 4000, Phaser.Easing.Linear.None, true, 0, 1000, false);
// sprite.scale.setTo(2, 2);
if (sprite.x === 100)
{
// Here you'll notice we are using a relative value for the tween.
// You can specify a number as a string with either + or - at the start of it.
// When the tween starts it will take the sprites current X value and add +300 to it.
// game.add.tween(sprite).to( { x: '+300' }, 2000, Phaser.Easing.Linear.None, true);
}
else if (sprite.x === 400)
{
// game.add.tween(sprite).to( { x: '-300' }, 2000, Phaser.Easing.Linear.None, true);
}
}
function render() {
if (sprite.x === 100 || sprite.x === 400)
{
// game.debug.renderText('Click sprite to tween', 32, 32);
}
// game.debug.renderText('x: ' + arrowStart.x, arrowStart.x, arrowStart.y - 4);
// game.debug.renderText('x: ' + arrowEnd.x, arrowEnd.x, arrowEnd.y - 4);
game.debug.renderText('sprite.x: ' + sprite.x + ' deltaX: ' + sprite.deltaX, 32, 32);
game.debug.renderText('sprite.y: ' + sprite.y + ' deltaY: ' + sprite.deltaY, 32, 48);
game.debug.renderPhysicsBody(sprite.body);
game.debug.renderPoint(sprite.center);
}
+163
View File
@@ -0,0 +1,163 @@
// mods by Patrick OReilly
// twitter: @pato_reilly
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('analog', 'assets/tests/fusia.png');
game.load.image('arrow', 'assets/sprites/longarrow2.png');
game.load.image('ball', 'assets/sprites/pangball.png');
game.load.image('hill', 'assets/pics/atari_fujilogo.png');
game.load.image('block', 'assets/sprites/block.png');
game.load.image('wabbit', 'assets/sprites/wabbit.png');
}
var arrow;
var ball;
var catchFlag = false;
var wabbits;
var wab;
var launchVelocity = 0;
var shotCount = 0;
var wabbitCount = 0;
function create() {
// set global gravity
game.physics.gravity.y = 200;
game.stage.backgroundColor = '#0072bc';
var graphics = game.add.graphics(0, 0);
graphics.beginFill(0x049e0c);
graphics.drawRect(195, 450, 10, 150);
wabbits = game.add.group();
for (var i = 0; i < 5; i++)
{
wab = wabbits.create(game.rnd.integerInRange(575, 650), game.rnd.integerInRange(150, 260), 'wabbit');
wab.name = 'wab' + i;
wab.body.collideWorldBounds = true;
wab.body.mass = 0.1;
wab.body.linearDamping = 0.2;
}
hill = game.add.sprite(450, 400, 'hill');
hill.body.collideWorldBounds = true;
hill.body.mass = 10;
hill.body.immovable = true;
block = game.add.sprite(575, 300, 'block');
block.body.collideWorldBounds = true;
block.body.mass = 5;
block.body.linearDamping = 0.4;
analog = game.add.sprite(200, 450, 'analog');
analog.body.allowGravity = false;
analog.width = 8;
analog.rotation = 220;
analog.alpha = 0;
analog.anchor.setTo(0.5, 0.0);
arrow = game.add.sprite(200, 450, 'arrow');
arrow.anchor.setTo(0.1, 0.5);
arrow.body.allowGravity = false;
arrow.alpha = 0;
ball = game.add.sprite(100, 400, 'ball');
ball.anchor.setTo(0.5, 0.5);
ball.inputEnabled = true;
ball.body.collideWorldBounds = true;
ball.body.bounce.setTo(0.9, 0.9);
ball.body.linearDamping = 0.2;
ball.body.immovable = true;
// Enable input.
ball.input.start(0, true);
ball.events.onInputDown.add(set);
ball.events.onInputUp.add(launch);
}
function set(ball, pointer) {
ball.body.velocity.setTo(0, 0);
ball.body.allowGravity = false;
catchFlag = true;
}
function launch() {
catchFlag = false;
shotCount++;
arrow.alpha = 0;
analog.alpha = 0;
Xvector = (arrow.x - ball.x) * 4.1;
Yvector = (arrow.y - ball.y) * 4.1;
ball.body.allowGravity = true;
ball.body.velocity.setTo(Xvector,Yvector);
}
function update() {
arrow.rotation = game.physics.angleBetween(arrow, ball);
if (catchFlag == true)
{
// Track the ball sprite to the mouse
ball.x = game.input.activePointer.worldX;
ball.y = game.input.activePointer.worldY;
arrow.alpha = 1;
analog.alpha = 0.5;
analog.rotation = arrow.rotation - 3.14 / 2;
analog.height = game.physics.distanceToPointer(arrow);
launchVelocity = analog.height;
}
game.physics.collide(ball, wabbits);
game.physics.collide(wabbits, wabbits);
game.physics.collide(wabbits, hill);
game.physics.collide(ball, hill);
game.physics.collide(block, hill);
game.physics.collide(block, ball);
game.physics.collide(block, wabbits);
// check wabbits
var wabs = 0;
for (var i = 0; i < 5; i++)
{
thisWab = wabbits.getAt(i);
if (thisWab.x > 770 && thisWab.y > 400)
{
thisWab.alive = false;
wabs++;
}
}
wabbitCount = 5 - wabs;
// console.log(ball.body.y, ball.body.motionVelocity.y);
}
function render() {
// game.debug.renderSpriteCollision(ball, 32, 32);
// var graphics = game.add.graphics(0, 0);
// game.context.fillStyle = 'rgba(0,0,255,0.5)';
// game.context.fillRect(770, 400, 30, 200);
// game.debug.renderText("Drag the ball to launch", 32, 32);
// game.debug.renderText("Try to get all 5 wabbits in the blue area with the least number of shots", 32, 64);
// game.debug.renderText("Shot Count: " + shotCount, 32, 96);
// game.debug.renderText("Wabbits Left: " + wabbitCount, 32, 128);
}
+158
View File
@@ -0,0 +1,158 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level3', 'assets/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/maps/cybernoid.png', 16, 16);
}
var map;
var tileset;
var layer;
var cursors;
var sprite2;
var overlap;
function create() {
game.stage.backgroundColor = '#3d3d3d';
// A Tilemap object just holds the data needed to describe the map (i.e. the json exported from Tiled, or the CSV exported from elsewhere).
// You can add your own data or manipulate the data (swap tiles around, etc) but in order to display it you need to create a TilemapLayer.
map = new Phaser.Tilemap(game, 'level3');
// A Tileset is a single image containing a strip of tiles. Each tile is broken down into its own Phaser.Tile object on import.
// You can set properties on the Tile objects, such as collision, n-way movement and meta data.
// A Tilemap uses a Tileset to render. The indexes in the map corresponding to the Tileset indexes.
// This way multiple levels can share the same single Tileset without requiring one each.
tileset = game.cache.getTileset('tiles');
// Basically this sets EVERY SINGLE tile to fully collide on all faces
tileset.setCollisionRange(0, tileset.total - 1, true, true, true, true);
// And this turns off collision on the only tile we don't want collision on :)
tileset.setCollision(6, false, false, false, false);
// A TilemapLayer consists of an x,y coordinate (position), a width and height, a Tileset and a Tilemap which it uses for map data.
// The x/y coordinates are in World space and you can place the tilemap layer anywhere in the world.
// The width/height is the rendered size of the layer in pixels, not the size of the map data itself.
// This one gives tileset as a string, the other an object
// layer = new Phaser.TilemapLayer(game, 0, 0, 640, 400, 'tiles', map, 0);
layer = new Phaser.TilemapLayer(game, 0, 0, 640, 400, tileset, map, 0);
// To set tiles for collision you need to modify the Tileset, which is a property of the layer
// Task now
//
// 1) Get tiles that will collide with sprites new position (hullX and hullY)
// 2) Separate x/y
// Collision is based on the layer.x/y value
// layer.sprite.anchor.setTo(0.5, 0.5);
game.world.add(layer.sprite);
// This is a bit nuts, ought to find a way to automate it, but it looks cool :)
map.debugMap = [ '#000000',
'#e40058', '#e40058', '#e40058', '#80d010', '#bcbcbc', '#e40058', '#000000', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc', '#e40058', '#e40058', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#80d010', '#80d010', '#80d010', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#80d010', '#0070ec',
'#0070ec', '#24188c', '#24188c', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#80d010', '#80d010', '#80d010', '#e40058',
'#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#80d010', '#bcbcbc', '#80d010', '#000000', '#80d010',
'#80d010', '#80d010', '#bcbcbc', '#e40058', '#80d010', '#80d010', '#e40058', '#e40058', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc'
];
// map.dump();
// layer.sprite.scale.setTo(2, 2);
// Works a treat :)
// game.add.sprite(320, 0, layer.texture, layer.frame);
// game.add.sprite(0, 200, layer.texture, layer.frame);
// game.add.sprite(320, 200, layer.texture, layer.frame);
// game.world.setBounds(0, 0, 2000, 2000);
// game.camera.x = 400;
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
// layer.sprite.angle += 0.5;
if (cursors.up.isDown)
{
layer.y -= 4;
}
else if (cursors.down.isDown)
{
layer.y += 4;
}
if (cursors.left.isDown)
{
layer.x -= 4;
}
else if (cursors.right.isDown)
{
layer.x += 4;
}
// getTiles: function (x, y, width, height, collides, layer) {
overlap = layer.getTiles(layer.x, layer.y, 128, 128);
}
function render() {
layer.render();
game.context.save();
game.context.setTransform(1, 0, 0, 1, 0, 0);
game.context.fillStyle = 'rgba(255, 0, 0, 0.5)';
if (overlap.length > 1)
{
var x = 0;
var y = 0;
for (var i = 1; i < overlap.length; i++)
{
game.context.drawImage(
overlap[i].tile.tileset.image,
overlap[i].tile.x,
overlap[i].tile.y,
overlap[i].tile.width,
overlap[i].tile.height,
0 + (x * overlap[i].tile.width),
420 + (y * overlap[i].tile.height),
overlap[i].tile.width,
overlap[i].tile.height
);
if (overlap[i].tile.collideNone == false)
{
game.context.fillRect(0 + (x * overlap[i].tile.width), 420 + (y * overlap[i].tile.height), overlap[i].tile.width, overlap[i].tile.height);
}
x++;
if (x == overlap[0].tw)
{
x = 0;
y++;
}
}
}
game.context.restore();
}
+189
View File
@@ -0,0 +1,189 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level3', 'assets/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/maps/cybernoid.png', 16, 16);
// game.load.image('phaser', 'assets/sprites/phaser-dude.png');
game.load.image('phaser', 'assets/sprites/space-baddie.png');
}
var map;
var tileset;
var layer;
var cursors;
var overlap;
var sprite;
function create() {
game.stage.backgroundColor = '#3d3d3d';
// A Tilemap object just holds the data needed to describe the map (i.e. the json exported from Tiled, or the CSV exported from elsewhere).
// You can add your own data or manipulate the data (swap tiles around, etc) but in order to display it you need to create a TilemapLayer.
map = new Phaser.Tilemap(game, 'level3');
// A Tileset is a single image containing a strip of tiles. Each tile is broken down into its own Phaser.Tile object on import.
// You can set properties on the Tile objects, such as collision, n-way movement and meta data.
// A Tilemap uses a Tileset to render. The indexes in the map corresponding to the Tileset indexes.
// This way multiple levels can share the same single Tileset without requiring one each.
tileset = game.cache.getTileset('tiles');
// Basically this sets EVERY SINGLE tile to fully collide on all faces
tileset.setCollisionRange(0, tileset.total - 1, true, true, true, true);
// And this turns off collision on the only tile we don't want collision on :)
tileset.setCollision(6, false, false, false, false);
tileset.setCollision(34, false, false, false, false);
tileset.setCollision(35, false, false, false, false);
// A TilemapLayer consists of an x,y coordinate (position), a width and height, a Tileset and a Tilemap which it uses for map data.
// The x/y coordinates are in World space and you can place the tilemap layer anywhere in the world.
// The width/height is the rendered size of the layer in pixels, not the size of the map data itself.
// This one gives tileset as a string, the other an object
// layer = new Phaser.TilemapLayer(game, 0, 0, 640, 400, 'tiles', map, 0);
layer = new Phaser.TilemapLayer(game, 0, 0, 800, 400, tileset, map, 0);
// To set tiles for collision you need to modify the Tileset, which is a property of the layer
// Task now
//
// 1) Get tiles that will collide with sprites new position (hullX and hullY)
// 2) Separate x/y
// Collision is based on the layer.x/y value
// layer.sprite.anchor.setTo(0.5, 0.5);
game.world.add(layer.sprite);
// This is a bit nuts, ought to find a way to automate it, but it looks cool :)
map.debugMap = [ '#000000',
'#e40058', '#e40058', '#e40058', '#80d010', '#bcbcbc', '#e40058', '#000000', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc', '#e40058', '#e40058', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#80d010', '#80d010', '#80d010', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#80d010', '#0070ec',
'#0070ec', '#24188c', '#24188c', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#80d010', '#80d010', '#80d010', '#e40058',
'#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#80d010', '#bcbcbc', '#80d010', '#000000', '#80d010',
'#80d010', '#80d010', '#bcbcbc', '#e40058', '#80d010', '#80d010', '#e40058', '#e40058', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc'
];
// map.dump();
// layer.sprite.scale.setTo(2, 2);
// Works a treat :)
// game.add.sprite(320, 0, layer.texture, layer.frame);
// game.add.sprite(0, 200, layer.texture, layer.frame);
// game.add.sprite(320, 200, layer.texture, layer.frame);
// game.world.setBounds(0, 0, 2000, 2000);
// game.camera.x = 400;
sprite = game.add.sprite(200, 80, 'phaser');
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
sprite.body.velocity.setTo(0, 0);
// layer.sprite.angle += 0.5;
if (cursors.up.isDown)
{
sprite.body.velocity.y = -100;
// layer.y -= 4;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 100;
// layer.y += 4;
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -100;
// layer.x -= 4;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 100;
// layer.x += 4;
}
// getTiles: function (x, y, width, height, collides, layer) {
overlap = layer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true);
if (overlap.length > 1)
{
// console.log('%c ', 'background: #000000')
for (var i = 1; i < overlap.length; i++)
{
game.physics.separateTile(sprite.body, overlap[i]);
}
}
}
function render() {
layer.render();
game.debug.renderSpriteInfo(sprite, 32, 450);
// game.debug.renderCameraInfo(game.camera, 32, 32);
/*
game.context.save();
game.context.setTransform(1, 0, 0, 1, 0, 0);
game.context.fillStyle = 'rgba(255, 0, 0, 0.5)';
if (overlap.length > 1)
{
var x = 0;
var y = 0;
for (var i = 1; i < overlap.length; i++)
{
game.context.drawImage(
overlap[i].tile.tileset.image,
overlap[i].tile.x,
overlap[i].tile.y,
overlap[i].tile.width,
overlap[i].tile.height,
0 + (x * overlap[i].tile.width),
420 + (y * overlap[i].tile.height),
overlap[i].tile.width,
overlap[i].tile.height
);
if (overlap[i].tile.collideNone == false)
{
game.context.fillRect(0 + (x * overlap[i].tile.width), 420 + (y * overlap[i].tile.height), overlap[i].tile.width, overlap[i].tile.height);
}
x++;
if (x == overlap[0].tw)
{
x = 0;
y++;
}
}
}
game.context.restore();
*/
// game.debug.renderRectangle(sprite.body.hullX);
}
+188
View File
@@ -0,0 +1,188 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level3', 'assets/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/maps/cybernoid.png', 16, 16);
// game.load.image('phaser', 'assets/sprites/phaser-dude.png');
game.load.image('phaser', 'assets/sprites/space-baddie.png');
}
var map;
var tileset;
var layer;
var cursors;
var overlap;
var sprite;
function create() {
game.stage.backgroundColor = '#3d3d3d';
// A Tilemap object just holds the data needed to describe the map (i.e. the json exported from Tiled, or the CSV exported from elsewhere).
// You can add your own data or manipulate the data (swap tiles around, etc) but in order to display it you need to create a TilemapLayer.
map = new Phaser.Tilemap(game, 'level3');
// A Tileset is a single image containing a strip of tiles. Each tile is broken down into its own Phaser.Tile object on import.
// You can set properties on the Tile objects, such as collision, n-way movement and meta data.
// A Tilemap uses a Tileset to render. The indexes in the map corresponding to the Tileset indexes.
// This way multiple levels can share the same single Tileset without requiring one each.
tileset = game.cache.getTileset('tiles');
// Basically this sets EVERY SINGLE tile to fully collide on all faces
tileset.setCollisionRange(0, tileset.total - 1, true, true, true, true);
// And this turns off collision on the only tile we don't want collision on :)
tileset.setCollision(6, false, false, false, false);
tileset.setCollision(34, false, false, false, false);
tileset.setCollision(35, false, false, false, false);
// A TilemapLayer consists of an x,y coordinate (position), a width and height, a Tileset and a Tilemap which it uses for map data.
// The x/y coordinates are in World space and you can place the tilemap layer anywhere in the world.
// The width/height is the rendered size of the layer in pixels, not the size of the map data itself.
// This one gives tileset as a string, the other an object
// layer = new Phaser.TilemapLayer(game, 0, 0, 800, 400, tileset, map, 0);
layer = new Phaser.TilemapLayer(game, 0, 0, 400, 200, tileset, map, 0);
layer.sprite.scale.setTo(2, 2);
// layer.sprite.anchor.setTo(0.5, 0.5);
game.world.add(layer.sprite);
// This is a bit nuts, ought to find a way to automate it, but it looks cool :)
map.debugMap = [ '#000000',
'#e40058', '#e40058', '#e40058', '#80d010', '#bcbcbc', '#e40058', '#000000', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc', '#e40058', '#e40058', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#80d010', '#80d010', '#80d010', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#80d010', '#0070ec',
'#0070ec', '#24188c', '#24188c', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#80d010', '#80d010', '#80d010', '#e40058',
'#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#80d010', '#bcbcbc', '#80d010', '#000000', '#80d010',
'#80d010', '#80d010', '#bcbcbc', '#e40058', '#80d010', '#80d010', '#e40058', '#e40058', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc'
];
// map.dump();
// layer.sprite.scale.setTo(2, 2);
// Works a treat :)
// game.add.sprite(320, 0, layer.texture, layer.frame);
// game.add.sprite(0, 200, layer.texture, layer.frame);
// game.add.sprite(320, 200, layer.texture, layer.frame);
// game.world.setBounds(0, 0, 2000, 2000);
// game.camera.x = 400;
sprite = game.add.sprite(200, 80, 'phaser');
// sprite.x = 140;
// sprite.y = 40;
//sprite.scale.setTo(2, 2);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
sprite.body.velocity.setTo(0, 0);
// layer.sprite.angle += 0.5;
if (cursors.up.isDown)
{
sprite.body.velocity.y = -100;
// layer.y -= 4;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 100;
// layer.y += 4;
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -100;
// layer.x -= 4;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 100;
// layer.x += 4;
}
// getTiles: function (x, y, width, height, collides, layer) {
overlap = layer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true);
if (overlap.length > 1)
{
// console.log('%c ', 'background: #000000')
for (var i = 1; i < overlap.length; i++)
{
game.physics.separateTile(sprite.body, overlap[i]);
}
}
}
function render() {
layer.render();
game.debug.renderSpriteBody(sprite);
game.debug.renderSpriteInfo(sprite, 32, 450);
// game.debug.renderCameraInfo(game.camera, 32, 32);
/*
game.context.save();
game.context.setTransform(1, 0, 0, 1, 0, 0);
game.context.fillStyle = 'rgba(255, 0, 0, 0.5)';
if (overlap.length > 1)
{
var x = 0;
var y = 0;
for (var i = 1; i < overlap.length; i++)
{
game.context.drawImage(
overlap[i].tile.tileset.image,
overlap[i].tile.x,
overlap[i].tile.y,
overlap[i].tile.width,
overlap[i].tile.height,
0 + (x * overlap[i].tile.width),
420 + (y * overlap[i].tile.height),
overlap[i].tile.width,
overlap[i].tile.height
);
if (overlap[i].tile.collideNone == false)
{
game.context.fillRect(0 + (x * overlap[i].tile.width), 420 + (y * overlap[i].tile.height), overlap[i].tile.width, overlap[i].tile.height);
}
x++;
if (x == overlap[0].tw)
{
x = 0;
y++;
}
}
}
game.context.restore();
*/
// game.debug.renderRectangle(sprite.body.hullX);
}
+197
View File
@@ -0,0 +1,197 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level3', 'assets/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/maps/cybernoid.png', 16, 16);
game.load.image('phaser', 'assets/sprites/phaser-ship.png');
}
var map;
var tileset;
var layer;
var cursors;
var overlap;
var sprite;
function create() {
game.stage.backgroundColor = '#3d3d3d';
// A Tilemap object just holds the data needed to describe the map (i.e. the json exported from Tiled, or the CSV exported from elsewhere).
// You can add your own data or manipulate the data (swap tiles around, etc) but in order to display it you need to create a TilemapLayer.
map = new Phaser.Tilemap(game, 'level3');
// A Tileset is a single image containing a strip of tiles. Each tile is broken down into its own Phaser.Tile object on import.
// You can set properties on the Tile objects, such as collision, n-way movement and meta data.
// A Tilemap uses a Tileset to render. The indexes in the map corresponding to the Tileset indexes.
// This way multiple levels can share the same single Tileset without requiring one each.
tileset = game.cache.getTileset('tiles');
// Basically this sets EVERY SINGLE tile to fully collide on all faces
tileset.setCollisionRange(0, tileset.total - 1, true, true, true, true);
// And this turns off collision on the only tile we don't want collision on :)
tileset.setCollision(6, false, false, false, false);
tileset.setCollision(31, false, false, false, false);
tileset.setCollision(34, false, false, false, false);
tileset.setCollision(35, false, false, false, false);
tileset.setCollision(46, false, false, false, false);
// A TilemapLayer consists of an x,y coordinate (position), a width and height, a Tileset and a Tilemap which it uses for map data.
// The x/y coordinates are in World space and you can place the tilemap layer anywhere in the world.
// The width/height is the rendered size of the layer in pixels, not the size of the map data itself.
// This one gives tileset as a string, the other an object
layer = new Phaser.TilemapLayer(game, 0, 0, 800, 600, tileset, map, 0);
// layer = new Phaser.TilemapLayer(game, 0, 0, 400, 200, tileset, map, 0);
// layer.sprite.scale.setTo(2, 2);
// layer.sprite.anchor.setTo(0.5, 0.5);
layer.resizeWorld();
game.world.add(layer.sprite);
// This is a bit nuts, ought to find a way to automate it, but it looks cool :)
map.debugMap = [ '#000000',
'#e40058', '#e40058', '#e40058', '#80d010', '#bcbcbc', '#e40058', '#000000', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc', '#e40058', '#e40058', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#80d010', '#80d010', '#80d010', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#80d010', '#0070ec',
'#0070ec', '#24188c', '#24188c', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#80d010', '#80d010', '#80d010', '#e40058',
'#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#80d010', '#bcbcbc', '#80d010', '#000000', '#80d010',
'#80d010', '#80d010', '#bcbcbc', '#e40058', '#80d010', '#80d010', '#e40058', '#e40058', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc'
];
// map.dump();
// layer.sprite.scale.setTo(2, 2);
// Works a treat :)
// game.add.sprite(320, 0, layer.texture, layer.frame);
// game.add.sprite(0, 200, layer.texture, layer.frame);
// game.add.sprite(320, 200, layer.texture, layer.frame);
// game.world.setBounds(0, 0, 2000, 2000);
// game.camera.x = 400;
sprite = game.add.sprite(450, 80, 'phaser');
sprite.anchor.setTo(0.5, 0.5);
// sprite.x = 140;
// sprite.y = 40;
//sprite.scale.setTo(2, 2);
// sprite.body.gravity.y = 100;
// sprite.body.bounce.x = 0.5;
// sprite.body.bounce.y = 0.2;
game.camera.follow(sprite);
game.camera.deadzone = new Phaser.Rectangle(160, 160, layer.renderWidth-320, layer.renderHeight-320);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
layer.update();
// getTiles: function (x, y, width, height, collides, layer) {
overlap = layer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true);
if (overlap.length > 1)
{
for (var i = 1; i < overlap.length; i++)
{
game.physics.separateTile(sprite.body, overlap[i]);
}
}
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
if (cursors.up.isDown)
{
sprite.body.velocity.y = -150;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 150;
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -150;
sprite.scale.x = -1;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 150;
sprite.scale.x = 1;
}
}
function render() {
layer.render();
// game.debug.renderSpriteBody(sprite);
// game.debug.renderSpriteInfo(sprite, 32, 450);
// game.debug.renderCameraInfo(game.camera, 32, 32);
/*
game.context.save();
game.context.setTransform(1, 0, 0, 1, 0, 0);
game.context.fillStyle = 'rgba(255, 0, 0, 0.5)';
if (overlap.length > 1)
{
var x = 0;
var y = 0;
for (var i = 1; i < overlap.length; i++)
{
game.context.drawImage(
overlap[i].tile.tileset.image,
overlap[i].tile.x,
overlap[i].tile.y,
overlap[i].tile.width,
overlap[i].tile.height,
0 + (x * overlap[i].tile.width),
420 + (y * overlap[i].tile.height),
overlap[i].tile.width,
overlap[i].tile.height
);
if (overlap[i].tile.collideNone == false)
{
game.context.fillRect(0 + (x * overlap[i].tile.width), 420 + (y * overlap[i].tile.height), overlap[i].tile.width, overlap[i].tile.height);
}
x++;
if (x == overlap[0].tw)
{
x = 0;
y++;
}
}
}
game.context.restore();
*/
// game.debug.renderRectangle(game.camera.deadzone, 'rgba(0,200,0,0.5)');
}
+39
View File
@@ -0,0 +1,39 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
}
var sprite;
function create() {
game.stage.backgroundColor = '#124184';
game.physics.gravity.y = 200;
sprite = game.add.sprite(200, 250, 'gameboy', 4);
sprite.name = 'green';
sprite.body.collideWorldBounds = true;
// sprite.anchor.setTo(0.5, 0.5);
sprite.body.bounce.setTo(0.9, 0.9);
sprite.body.velocity.x = 150;
}
function update() {
// sprite.worldTransform[2] += 1;
}
function render() {
game.debug.renderBodyInfo(sprite, 32, 32);
game.debug.renderPhysicsBody(sprite.body);
}