|
Legend
|
//
// Josh Schoenwald *** accad 756 *** Task 12 *** Nov. 19
//
//
var dist = 7; //distance to move guybrush each frame
var chuckieState = "normal"; //defines the beginning state for LeChuck
var stanState = "normal"; //defines the beginning state for Stan
var t = 0; //t equals time, used to increment the monkey's scaling using sine
var s = 1; //used to change movement of stan based on state
var c = 1; //used to change movement of LeChuck based on state
largospit._alpha = 0; //defines "animation" of largo spitting
//
/////////////////////////// defines what to do if keypress is detected /////////////////
//
function updateGame() {
//
t = (t + (Math.PI/4)) % (2*Math.PI);
//
/////////////////////////// defines operations related to LeChuck /////////////////
//
if (chuckieState != "nofollow") { //if Lechuck is close to guybrush, not currently following him, and if stan is not around...
if ( (distanceTo(guybrush, lechuck) <= 35) && (stanState != "follow") ) {
chuckieState = "follow"; //sets lechuck's state to follow
}
}
if ((distanceTo(guybrush, lechuck) >= 100) && (chuckieState == "nofollow")) { //if guybrush has moved far enough away...
chuckieState = "normal"; //lechuck's state is reset to normal, ready for another confrontation!
}
if ((Key.getCode() == Key.SPACE) && (chuckieState == "follow")) { //if the space bar is pressed, lechuck stops following
chuckieState = "nofollow"; //changes lechuck's state
}
//
////////////////////////// defines operations related to the monkey /////////////////
//
if (distanceTo(guybrush, monkey) <= 30) { //if guybrush gets near the monkey, he gets excited (People, No!!)
monkey._xscale = rangedSin(t, 1, 0, 100, 125); //scales monkey in response to guybrush getting near
monkey._yscale = rangedSin(t, 1, 0, 100, 135);
} else {
monkey._xscale = 100; //resets monkey's size
monkey._yscale = 100;
}
//
////////////////////////// defines operations related to Stan (not the man) /////////////////
//
if (stanState != "eaten") { //if stan is near guybrush and if lechuck is not following already...
if ( (distanceTo(guybrush, stan) <= 45) && (chuckieState != "follow") ) {
stanState = "follow"; //stan attaches himself like a leach if you get too close
}
}
if ((distanceTo(stan, monkey) <= 20) && (stanState == "follow")) { //if stan gets too close to the monkey, he gets eaten
stanState = "eaten"; //bye bye stan!
}
//
////////////////////////// defines operations related to largo, the spitting champion /////////////////
//
if ( (distanceTo(guybrush, largo) <= 68) && //if guybrush is near largo, then if...
(guybrush._x < largo._x) && //if guybrush is to the left of largo (i.e. in front of him
(Math.abs(guybrush._y - largo._y) < 15) ) { //if guybrush is about at the same parallel as largo...
largospit._alpha = 100; //show the spitting largo
largo._alpha = 0; //hide the waiting largo
} else {
largospit._alpha = 0; //spitting largo is hidden
largo._alpha = 100; //largo's resting state shows
}
//
////////////////////////// performs actions based on recent key presses /////////////////
//
switch (Key.getCode()) {
case Key.DOWN : //operates if the last key pressed was 'arrow down'
testState(); //tests the states of stan and lechuck
faceGuybrush(); //turns Stan and LeChuck around to face guybrush if not already
break;
case Key.UP :
testState();
faceGuybrush();
break;
case Key.RIGHT :
testState();
faceGuybrush();
guybrush._xscale = -100; //turns guybrush to the right if he's moving right
break;
case Key.LEFT :
testState();
faceGuybrush();
guybrush._xscale = 100; //turns guybrush to the left if he's facing left
break;
}
}
//
////////////////////////// HELPER FUNCTIONS /////////////////
//
function distanceTo(char1, char2) {
//returns the distance between the 2 characters)
return Math.sqrt(Math.pow((char2._x-char1._x), 2)+Math.pow((char2._y-char1._y), 2));
//standard distance formula: D = squareroot of: (square of: (x2 - x1) + square of: (y2 - y1))
}
function faceGuybrush() {
//makes sure lechuck is always facing guybrush
if (guybrush._x < lechuck._x) {
lechuck._xscale = -100;
} else {
lechuck._xscale = 100;
}
//makes sure stan is always facing guybrush
if (guybrush._x < stan._x) {
stan._xscale = -100;
} else {
stan._xscale = 100;
}
}
//
////////////////////////// does the actual movement of the characters /////////////////
//
function movement() {
switch (Key.getCode()) {
case Key.DOWN :
bground._y -= dist; //moves bg and characters on a key press
stan._y -= (dist * s); //value of 's' is 0 when not following, and 1 when following
largo._y -= dist;
largospit._y -= dist;
monkey._y -= dist;
lechuck._y -= (dist * c); //value of 'c' is 0 when not following, and 1 when following
break;
case Key.UP :
bground._y += dist;
stan._y += (dist * s);
largo._y += dist;
largospit._y += dist;
monkey._y += dist;
lechuck._y += (dist * c);
break;
case Key.RIGHT :
bground._x -= dist;
stan._x -= (dist * s);
largo._x -= dist;
largospit._x -= dist;
monkey._x -= dist;
lechuck._x -= (dist * c);
break;
case Key.LEFT :
bground._x += dist;
stan._x += (dist * s);
largo._x += dist;
largospit._x += dist;
monkey._x += dist;
lechuck._x += (dist * c);
break;
}
}
//
////////////////////////// sets the states for lechuck and stan /////////////////
//
function testState() {
if (chuckieState == "follow") { c = 0; } //sets values based on states
if (stanState == "follow") { s = 0; } //'s = 0' means NO movement rel. to canvas, which creates following illusion
if (chuckieState != "follow") { c = 1; } //'c = 1' means movement rel. to canvas, which creates stationary illusion
if (stanState != "follow") { s = 1; }
if (stanState == "eaten") { stan._alpha = 0; } //if stan is dead, make him disappear
movement();
}
//
////////////////////////// returns sine mapped to specified range /////////////////
//
function rangedSin(t, f, p, lo, hi) {
var a = (hi-lo)*.5;
var mid = lo+a;
return mid+(a*Math.sin(f*t+p));
}
//
//
// Josh Schoenwald *** accad 756 *** Task 12 *** Nov. 19
//