Skip to content

Commit

Permalink
Refactor to use Greensock, cleanup method names, commenting, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSurabian committed Nov 8, 2015
1 parent 061b597 commit 90c8a73
Show file tree
Hide file tree
Showing 13 changed files with 12,398 additions and 1,833 deletions.
Binary file modified dist/audio.ogg
Binary file not shown.
12,996 changes: 11,573 additions & 1,423 deletions dist/duckhunt.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<html>
<head>
<title>pixi.js example 1</title>
<title>DuckHuntJS</title>
<style>
body {
overflow:hidden;
Expand Down
5 changes: 3 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ gulp.task('deploy', function() {
]));
});

gulp.task('default', ['images', 'audio', 'jshint', 'jscs', 'modules']);
gulp.task('dev', ['default', 'watch', 'serve']);
gulp.task('js', ['jshint', 'jscs', 'modules']);
gulp.task('dev', ['default', 'watch', 'serve']);
gulp.task('default', ['images', 'audio', 'js']);
1 change: 0 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Game from './src/modules/Game';

document.addEventListener('DOMContentLoaded', function() {

// create the root of the scene graph
let game = new Game({
spritesheet: 'sprites.json'
}).load();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"bluebird": "^2.9.32",
"browserify": "^10.2.4",
"glob": "^5.0.13",
"gsap": "^1.18.0",
"gulp": "^3.9.0",
"gulp-babel": "^5.1.0",
"gulp-connect": "^2.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports.pointDistance = function(point1, point2) {
};

module.exports.directionOfTravel = function(pointStart, pointEnd) {
let direction;
let direction = '';

//positive means down
let rise = pointEnd.y - pointStart.y;
Expand Down
88 changes: 73 additions & 15 deletions src/modules/Character.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
const PIXI = require('pixi.js');

const _find = require('lodash/collection/find');
const _result = require('lodash/object/result');
import PIXI from 'pixi.js';
import TimelineLight from 'gsap/src/uncompressed/TimelineLite.js';
import _noop from 'lodash/utility/noop';
import _find from 'lodash/collection/find';
import _result from 'lodash/object/result';

class Character extends PIXI.extras.MovieClip {

constructor(assetKey, resourceKey, states) {
let gameTextures = PIXI.loader.resources[resourceKey].textures;
/**
* Character Constructor
* @param {String} spriteId The leading id of this Character's resources in the spritesheet
* @param {String} spritesheet The object property to ask PIXI's resource loader for
* @param {{name:String, animationSpeed:Number}[]} states The states that can be found in the spritesheet for the
* given sprite id.
*/
constructor(spriteId, spritesheet, states) {
let gameTextures = PIXI.loader.resources[spritesheet].textures;
for (let textureKey in gameTextures) {
if (!gameTextures.hasOwnProperty(textureKey) || textureKey.indexOf(assetKey) === -1) {
if (!gameTextures.hasOwnProperty(textureKey) || textureKey.indexOf(spriteId) === -1) {
continue;
}

let parts = textureKey.split('/');
parts.length -= 1; //truncate to remove media file

let state = parts.join('/').replace(assetKey + '/', '');
let state = parts.join('/').replace(spriteId + '/', '');

// Only add textures if the state is supported by the class
let stateObj = _find(states, {name: state});
Expand All @@ -35,26 +42,77 @@ class Character extends PIXI.extras.MovieClip {
}
}

// Give the MovieClip a default state
super(states[0].textures);
this.states = states;
this.animationSpeed = this.states[0].animationSpeed;
this.timeline = new TimelineLight({
autoRemoveChildren:true
});
return this;
}

/**
* stopAndClearTimeline
* Helper method that stops any existing animation where it is, and removes all other animations
* that are scheduled to run in the Character's timeline.
* @returns {Character}
*/
stopAndClearTimeline() {
this.timeline.pause();
let timelineItem = this.timeline.getChildren();
for (let i = 0; i < timelineItem.length; i++) {
timelineItem[i].kill();
}
this.timeline.play();
return this;
}

/**
* isActive
* Helper method that determines whether the Character's timeline is active
* @returns {Boolean}
*/
isActive() {
return this.timeline.isActive();
}

/**
* addToTimeline
* Adds any valid item to the timeline, typically this will be a function or a tween
* @param {Function|PIXI.Tween} item
* @returns {Character}
*/
addToTimeline(item) {
this.timeline.add(item);
return this;
}

setState(state) {
let stateObj = _find(this.states, {name: state});
/**
* state - setter
* Helper method that sets the state on the character and adjusts the object's texture if possible
* @param {String} value Name of the state to set on the character which should match a texture
* specified in the spritesheet
* @throws {Error} In order for a state to be set, a texture must be specified in the spritesheet
*/
set state(value) {
let stateObj = _find(this.states, {name: value});
if (!stateObj) {
return;
throw new Error('The requested state (' + state + ') is not availble for this Character.');
}
this.stateVal = value;
this._textures = stateObj.textures;
this.animationSpeed = stateObj.animationSpeed;
this.loop = stateObj.hasOwnProperty('loop') ? stateObj.loop : true;
this.play();
}

setPosition(x, y) {
this.position.set(x, y);
/**
* state - get
* Helper methods that returns the existin
* @returns {String}
*/
get state() {
return this.stateVal ? this.stateVal : '';
}

}
Expand Down
Loading

0 comments on commit 90c8a73

Please sign in to comment.