Skip to content

Commit

Permalink
1: Tidied up language in comments
Browse files Browse the repository at this point in the history
2: Removed trailing extra star **/ in docblock comments as looks wrong in some IDE's
3: Renamed Sceengraph to Scenegraph. Should not break anything as handled inside the API.
  • Loading branch information
FostUK committed Mar 14, 2012
1 parent 5b4c212 commit 54a8b44
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 261 deletions.
78 changes: 39 additions & 39 deletions demos/4/superjavascriptfighter2.js
Expand Up @@ -2,10 +2,10 @@ var PLAYGROUND_WIDTH = 800 ;
var PLAYGROUND_HEIGHT = 200;

$(function(){
// This is the AI that determines the next move
// This is the AI that determines the next move
// level=0: totaly random
// level=1: totaly "rational"

// possible move;
IDLE= 0;
WALK_FORWARD= 1;
Expand All @@ -14,50 +14,50 @@ $(function(){
KICK= 4;
BLOCK= 5;
BEATEN= 6;

//constantes:
NEAR= 100;

// this is a methods that returns a random element from the given array
function or(choice){
return choice[Math.round(Math.random()*(choice.length-1))];
};

// return the distance between the opponents
function distance(a, b){
return Math.abs(a.position().left-b.position().left);
};

function nextMove(level, a, b){
if(Math.random() > level){
return Math.round(Math.random()*5);
}
switch(b.data("fighter").currentState){
// if the adversary is idle or moves away from us we get near him or attack ihm
case IDLE:
case WALK_BACKWARD:
case BLOCK:
case IDLE:
case WALK_BACKWARD:
case BLOCK:
if(distance(a,b) < NEAR){
return or([KICK, PUNCH, WALK_BACKWARD]);
} else {
return or([WALK_FORWARD, IDLE]);
}
break;
// if the adversary moves toward us we get away or attack ihm
case WALK_FORWARD:
case WALK_FORWARD:
if(distance(a,b) < NEAR){
return or([KICK, PUNCH, WALK_BACKWARD]);
} else {
return or([WALK_FORWARD, IDLE]);
}
break;
// if we are under attack we either block go back or try to fight back
case PUNCH:
case PUNCH:
case KICK:
return or([BLOCK, PUNCH, KICK, IDLE]);
break;
// if beaten we block or go back
case BEATEN:
case BEATEN:
return or([BLOCK, WALK_BACKWARD, IDLE]);
break;
}
Expand All @@ -68,38 +68,38 @@ $(function(){
fighter = sprite.data("fighter");
adversary = $(fighter.adversary);
adversaryFighter = adversary.data("fighter");

var nextState = nextMove(0.8, sprite, adversary);

changeAnimation(sprite, fighter.animations, nextState, fighter.currentState);

if(nextState == PUNCH || nextState == KICK){
sprite.z(20);
} else if(fighter.currentState == PUNCH || fighter.currentState == KICK){
sprite.z(0);
}

fighter.currentState = nextState;
}

var scrollStage = function (offset){

if(offset > 50){
offset = 50;
} else if(offset < -50) {
offset = -50;
}
$("#foreground").x(-800 + offset/0.5);

$("#ground").x(-300 + offset);
$("#fighters").x(offset);

$("#background1").x(50 + offset/2);
$("#background2").x(30 + offset/4);
$("#background3").x(90 + offset/5);

}

/*replace with new*/
var changeAnimation = function(sprite, animationArry, newAnimation , oldAnimation){
sprite
Expand All @@ -109,10 +109,10 @@ $(function(){
.y(sprite.position().top + animationArry[newAnimation].deltaY - animationArry[oldAnimation].deltaY)
.x(sprite.position().left + animationArry[newAnimation].deltaX - animationArry[oldAnimation].deltaX);
};

// the game
$("#playground").playground({height: PLAYGROUND_HEIGHT, width: PLAYGROUND_WIDTH, refreshRate: 30, keyTracker: true});

//Playground Sprites
var foreground = new $.gameQuery.Animation({imageURL: "./stage/foreground.png", type: $.gameQuery.ANIMATION_VERTICAL});
var ground = new $.gameQuery.Animation({imageURL: "./stage/ground.png"});
Expand All @@ -139,8 +139,8 @@ $(function(){
{posx:-800, posy: 165,
height: 44, width: 2000,
animation: foreground});
$("#sceengraph").css("background-color","#121423");
$("#scenegraph").css("background-color","#121423");

//Fighters
var cvs = {
currentState : IDLE,
Expand Down Expand Up @@ -196,7 +196,7 @@ $(function(){
geometry: $.gameQuery.GEOMETRY_RECTANGLE,
callback: animate});
$("#cvs").data("fighter", cvs);

var abobo = {
currentState : IDLE,
position: 500,
Expand Down Expand Up @@ -257,11 +257,11 @@ $(function(){
var cvs = $("#cvs");
var cvsF = cvs.data("fighter");
var cvsLeft = cvs.position().left;

var abobo = $("#abobo");
var aboboF = abobo.data("fighter");
var aboboLeft = abobo.position().left;

//hit?
if(cvsLeft+cvsF.animations[cvsF.currentState].width - 2 > aboboLeft){
if((cvsF.currentState == KICK || cvsF.currentState == PUNCH) && aboboF.currentState != BEATEN){
Expand All @@ -279,7 +279,7 @@ $(function(){
cvsF.currentState = BEATEN;
}
}

//Move
if(cvsF.currentState == WALK_FORWARD){
if((cvsLeft+cvsF.animations[cvsF.currentState].width+2) < aboboLeft){
Expand All @@ -288,32 +288,32 @@ $(function(){
} else if ((cvsLeft > 50) && (cvsF.currentState == WALK_BACKWARD)){
cvs.x(cvsLeft-2)
}

if(aboboF.currentState == WALK_FORWARD){
if((cvsLeft+cvsF.animations[cvsF.currentState].width+2) < aboboLeft){
abobo.x(aboboLeft - 2);
}
} else if ((aboboLeft < 650) && (aboboF.currentState == WALK_BACKWARD)){
abobo.x(aboboLeft + 2);
}

var al = abobo.position().left - aboboF.animations[aboboF.currentState].deltaX;
var cl = cvs.position().left - cvsF.animations[cvsF.currentState].deltaX;

var centerPos = (al - cl)/2 + cl;
scrollStage(-(centerPos-400)*0.5);

return false;
}, 30);




//start loading!
$.loadCallback(function(percent){
$("#loadingBar").width(600*percent);
});

//initialize the start button
$.playground().startGame(function(){
$("#welcomMessage").fadeOut(2000, function(){$(this).remove()});
Expand Down
10 changes: 6 additions & 4 deletions jquery.gamequery.ext.js
@@ -1,12 +1,13 @@
/*
/**
* gameQuery.ext
*
* Copyright (c) 2010 Rob Britton
* licensed under the MIT (MIT-LICENSE.txt)
*/
$ = $.extend($, {gameQuery: {}});

/* QuadTree
/**
* QuadTree
* A space-partitioning tree useful for efficient collision detection.
*/
$.gameQuery.QuadTree = function (width, height){
Expand Down Expand Up @@ -157,8 +158,9 @@ $.gameQuery.getTimeElapsed = function (){
return gap.getMilliseconds();
}

/* View classes for handling scrolling backgrounds
*
/**
* View classes for handling scrolling backgrounds
*
* @param viewport The jQuery element where the view is displayed
* @param background The jQuery element to be scrolled around in the viewport
*/
Expand Down

0 comments on commit 54a8b44

Please sign in to comment.