Skip to content

Commit

Permalink
Make p5.play compatible with vue-p5
Browse files Browse the repository at this point in the history
  • Loading branch information
pspaul committed Sep 24, 2018
1 parent cfbd533 commit e164c34
Showing 1 changed file with 68 additions and 66 deletions.
134 changes: 68 additions & 66 deletions p5.play.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ by Paolo Pedercini/molleindustria, 2015
http://molleindustria.org/
*/

(function(root, factory) {
if (typeof define === 'function' && define.amd)
define('p5.play', ['p5'], function(p5) { (factory(p5)); });
else if (typeof exports === 'object')
factory(require('../p5'));
else
factory(root.p5);
}(this, function(p5) {
export default function init(p5) {

//(function(root, factory) {
//if (typeof define === 'function' && define.amd)
//define('p5.play', ['p5'], function(p5) { (factory(p5)); });
//else if (typeof exports === 'object')
//factory(require('../p5'));
//else
//factory(root.p5);
//}(this, function(p5) {

/**
* p5.play is a library for p5.js to facilitate the creation of games and gamelike
* projects.
Expand Down Expand Up @@ -706,11 +709,9 @@ defineLazyP5Property('quadTree', function() {
/*
//framerate independent delta, doesn't really work
p5.prototype.deltaTime = 1;
var now = Date.now();
var then = Date.now();
var INTERVAL_60 = 0.0166666; //60 fps
function updateDelta() {
then = now;
now = Date.now();
Expand Down Expand Up @@ -3987,15 +3988,15 @@ function construct(constructor, args) {
function Quadtree( bounds, max_objects, max_levels, level ) {

this.active = true;
this.max_objects = max_objects || 10;
this.max_levels = max_levels || 4;
this.max_objects = max_objects || 10;
this.max_levels = max_levels || 4;

this.level = level || 0;
this.bounds = bounds;
this.level = level || 0;
this.bounds = bounds;

this.objects = [];
this.object_refs = [];
this.nodes = [];
this.objects = [];
this.object_refs = [];
this.nodes = [];
}

Quadtree.prototype.updateBounds = function() {
Expand Down Expand Up @@ -4030,61 +4031,61 @@ Quadtree.prototype.updateBounds = function() {
};

/*
* Split the node into 4 subnodes
*/
* Split the node into 4 subnodes
*/
Quadtree.prototype.split = function() {

var nextLevel = this.level + 1,
subWidth = Math.round( this.bounds.width / 2 ),
subHeight = Math.round( this.bounds.height / 2 ),
x = Math.round( this.bounds.x ),
y = Math.round( this.bounds.y );
var nextLevel = this.level + 1,
subWidth = Math.round( this.bounds.width / 2 ),
subHeight = Math.round( this.bounds.height / 2 ),
x = Math.round( this.bounds.x ),
y = Math.round( this.bounds.y );

//top right node
this.nodes[0] = new Quadtree({
x : x + subWidth,
y : y,
width : subWidth,
height : subHeight
x : x + subWidth,
y : y,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);

//top left node
this.nodes[1] = new Quadtree({
x : x,
y : y,
width : subWidth,
height : subHeight
x : x,
y : y,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);

//bottom left node
this.nodes[2] = new Quadtree({
x : x,
y : y + subHeight,
width : subWidth,
height : subHeight
x : x,
y : y + subHeight,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);

//bottom right node
this.nodes[3] = new Quadtree({
x : x + subWidth,
y : y + subHeight,
width : subWidth,
height : subHeight
x : x + subWidth,
y : y + subHeight,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
};


/*
* Determine the quadtrant for an area in this node
*/
* Determine the quadtrant for an area in this node
*/
Quadtree.prototype.getIndex = function( pRect ) {
if(!pRect.collider)
return -1;
else
{
var index = -1,
verticalMidpoint = this.bounds.x + (this.bounds.width / 2),
horizontalMidpoint = this.bounds.y + (this.bounds.height / 2),
var index = -1,
verticalMidpoint = this.bounds.x + (this.bounds.width / 2),
horizontalMidpoint = this.bounds.y + (this.bounds.height / 2),

//pRect can completely fit within the top quadrants
topQuadrant = (pRect.collider.top() < horizontalMidpoint && pRect.collider.top() + pRect.collider.size().y < horizontalMidpoint),
Expand Down Expand Up @@ -4115,10 +4116,10 @@ Quadtree.prototype.getIndex = function( pRect ) {


/*
* Insert an object into the node. If the node
* exceeds the capacity, it will split and add all
* objects to their corresponding subnodes.
*/
* Insert an object into the node. If the node
* exceeds the capacity, it will split and add all
* objects to their corresponding subnodes.
*/
Quadtree.prototype.insert = function( obj ) {
//avoid double insertion
if(this.objects.indexOf(obj) === -1)
Expand Down Expand Up @@ -4163,8 +4164,8 @@ Quadtree.prototype.insert = function( obj ) {


/*
* Return all objects that could collide with a given area
*/
* Return all objects that could collide with a given area
*/
Quadtree.prototype.retrieve = function( pRect ) {


Expand Down Expand Up @@ -4202,8 +4203,8 @@ Quadtree.prototype.retrieveFromGroup = function( pRect, group ) {
};

/*
* Get all objects stored in the quadtree
*/
* Get all objects stored in the quadtree
*/
Quadtree.prototype.getAll = function() {

var objects = this.objects;
Expand All @@ -4217,8 +4218,8 @@ Quadtree.prototype.getAll = function() {


/*
* Get the node in which a certain object is stored
*/
* Get the node in which a certain object is stored
*/
Quadtree.prototype.getObjectNode = function( obj ) {

var index;
Expand Down Expand Up @@ -4249,9 +4250,9 @@ Quadtree.prototype.getObjectNode = function( obj ) {


/*
* Removes a specific object from the quadtree
* Does not delete empty subnodes. See cleanup-function
*/
* Removes a specific object from the quadtree
* Does not delete empty subnodes. See cleanup-function
*/
Quadtree.prototype.removeObject = function( obj ) {

var node = this.getObjectNode( obj ),
Expand All @@ -4264,8 +4265,8 @@ Quadtree.prototype.removeObject = function( obj ) {


/*
* Clear the quadtree and delete all objects
*/
* Clear the quadtree and delete all objects
*/
Quadtree.prototype.clear = function() {

this.objects = [];
Expand All @@ -4282,9 +4283,9 @@ Quadtree.prototype.clear = function() {


/*
* Clean up the quadtree
* Like clear, but objects won't be deleted but re-inserted
*/
* Clean up the quadtree
* Like clear, but objects won't be deleted but re-inserted
*/
Quadtree.prototype.cleanup = function() {

var objects = this.getAll();
Expand All @@ -4307,10 +4308,10 @@ function updateTree() {
}

//keyboard input
p5.prototype.registerMethod('pre', p5.prototype.readPresses);
//p5.prototype.registerMethod('pre', p5.prototype.readPresses);

//automatic sprite update
p5.prototype.registerMethod('pre', p5.prototype.updateSprites);
//p5.prototype.registerMethod('pre', p5.prototype.updateSprites);

//quadtree update
p5.prototype.registerMethod('post', updateTree);
Expand Down Expand Up @@ -4346,4 +4347,5 @@ p5.prototype._warn = function(message) {
}
};

}));
//}));
};

0 comments on commit e164c34

Please sign in to comment.