Skip to content

Instantly share code, notes, and snippets.

@aq1018
Created October 27, 2009 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aq1018/219345 to your computer and use it in GitHub Desktop.
Save aq1018/219345 to your computer and use it in GitHub Desktop.
// for beginners and if you just want to test some bullet pattern
// this is a good way to start.
function enter() {
// equivalent to @Initialize ( I think)
// Load some graphics
// Make yourself some coffee
}
function run() {
// equivalent to @MainLoop
// This gets executed every frame
// There is no @DrawLoop, everything is handled automatically
// What?
// Yup, that's right!
}
function exit() {
// This is different from @finalize
// Unload your graphics here
// Intended to clean up resources
}
// The magical line that runs the entire game...
Engine.execute(enter, run, exit);
// This handles a basic static image
Engine.Graphic = function(file){};
Engine.Graphic.prototype = {
// release the graphics memory
release : function(){}
}
// This chains up a set of graphics to make an animation
Engine.Animation = function(name){};
Engine.Animation.prototype = {
add : function(graphic, interval){}, // add a graphic to an animation
remove : function(index){} // remove a graphic from an animation
};
// This specifies which animation happens next
Engine.AnimationLink = function(fromAnimation, toAnimation, priority){};
// This groups up a set of animations and automatically control which animation to run
Engine.AnimationSet = function(){};
Engine.AnimationSet.prototype = {
add : function(animationLink){}, // specify which animation transits to where
remove : function(animationLink){}, // remove a graphic from an animation
getCurrentAnimation : function(){}, // returns current animation
setTargetAnimation : function(animationName){}, // what is the next animation to play (will auto transit)
setImmediateAnimation : function(animationName){} // skip all transitional animations and go straight to the specified animation
};
// This is the base object for all static / animation objects in game including text
Engine.Object = function(){};
Engine.Object.prototype = {
x : 0, // position x in pixels
y : 0, // position y in pixels
z : 0.0, // the z index ( between 0.0 to 1.0 )
vel : 0.0, // velocity
acc : 0.0, // acceleration
minVel : 0.0, // minimum velocity
maxVel : 0.0, // max velocity
ang : 0.0, // angle
angVel : 0.0, // angular velocity
graphic : graphic, // this the Engine.Graphic object to display
animationSet : as, // Engine.AnimationSet object, overrides graphic option
body : p // Engine.Body object, handles physics including collision
};
// If you want to make a full featured game, you can.
// This is the basic framework that sets up a 2 stage game.
// Stage is a specialized state
Stage = function(){};
Stage.prototype = Engine.extend(Engine.State, {
enter : function() {
// what happens on entering this stage
},
run : function() {
// runs every loop for this stage
if(this.isStageEnded()){
this.game.advanceStage();
}
},
exit : function() {
// what happens on exiting this stage
}
});
// Game Inherits from StateMachine
Game = function(){};
Game.prototype = Engine.extend(Engine.StateMachine.prototype, {
init : funciton(){
// instantiate 2 stages
stage1 = new Stage("stage1");
stage2 = new Stage("stage2");
// add stages to state machine
this.addState(stage1);
this.addState(stage2);
// set transition rules
// this means from stage1 can only advance to stage2
this.addTransition("advanceStage", stage1, stage2);
// set initial stage
this.setInitialState("stage1");
},
// runs the current state
run : function(){
this.getCurrentState().run();
}
});
// This is a standard state machine abstraction in javascript
// Will be in standard lib for intend usage.
// If you are not happy with this implementation, you can extend it yourself.
Engine.State = function(name, initFn, runFn, exitFn){};
Engine.StateTransition = function(name, fromStateNames, toStateNames, guardFn, transitionFn){};
Engine.StateMachine = function(){};
Engine.StateMachine.prototype = {
setInitialState : function(stateName){},
addState : function(state){},
addStateTransition : function(transition){},
getCurrentState : function(transition){},
// auto-magically create a this.<transitionName> to call
transitTo : function(transitionName){}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment