Skip to content

Commit

Permalink
Added Master Slime
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Marler committed Jul 2, 2015
1 parent 68b4a68 commit 1934b6a
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 11 deletions.
205 changes: 203 additions & 2 deletions SlimeAI.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,219 @@ function setAngryRedSlime(ai) {

};
}
function setMasterSlime(ai) {
ai.state = -1;

ai.randomJump40Percent = function() {
if (Math.random() <= 0.40) {
this.jump();
}
};
ai.ballXWhenBelow = function(yLimit) {
var toEnemyWall = this.ballToEnemyWall;
var velocityToEnemyWall = this.ballVXToEnemyWall;
var ballY = ball.y;
var ballVelocityY = ball.velocityY;
while(1) {
ballVelocityY--;
ballY += ballVelocityY;
if(ballY <= yLimit)
return toEnemyWall;
toEnemyWall += velocityToEnemyWall;
if(toEnemyWall <= 0) {
toEnemyWall = 0;
velocityToEnemyWall = - velocityToEnemyWall;
} else if(toEnemyWall >= 1000) {
toEnemyWall = 1000;
velocityToEnemyWall = - velocityToEnemyWall;
}
}
}
ai.performServe = function(me) {
if(this.state == -1) {
if(Math.random() < 0.3) {
if(this.enemyToTheirWall < 30) {
this.state = 0;
} else if(this.enemyToTheirWall > 200) {
this.state = 1;
} else {
this.state = 2;
}
} else {
this.state = 2;
}
if(Math.random() < 0.3) {
this.state = Math.trunc(Math.random()*3);
}
}

if(this.state == 0 || this.state == 1) {
var j = (this.state == 0) ? 860 : 840;
if(ball.velocityY > 12 && this.meToEnemyWall < j) {
this.moveToWall();
}
if(this.meToEnemyWall > j) {
this.stopMovement();
}
if(ball.velocityY == -3 && this.meToEnemyWall != 800) {
this.jump();
}
if(this.state == 0 && ball.velocityY < -12 && me.y != 0 && this.meToEnemyWall >= j-15) {
this.moveToNet();
}
if(this.ballToEnemyWall < 700) {
this.state = -1;
}
} else if(this.state == 2) {
var limit = 770;
if (ball.velocityY > 12 && this.meToEnemyWall > limit) {
this.moveToNet();
}
if (this.meToEnemyWall <= limit) {
this.stopMovement();
}
if (ball.velocityY == -2 && this.meToEnemyWall != 800) {
this.jump();
}
if (me.y != 0 && this.ballToEnemyWall > 800) {
this.state = 3;
var prob;
if(this.enemyToTheirWall < 200) {
prob = 0.7;
} else if(this.enemyToTheirWall > 300) {
prob = 0.3;
} else {
prob = 0.5;
}
if(Math.random() < prob) {
this.state++;
}
}
} else if(this.state == 3) {
var limit = 585;
if (this.meToEnemyWall > limit) {
this.moveToNet();
}
if (this.meToEnemyWall <= limit) {
this.stopMovement();
}
if (this.ballToEnemyWall <= 730) {
this.jump();
}
if (this.ballToEnemyWall < 540) {
this.state = -1;
}
} else if(this.state == 4) {
var limit = 585;
if (this.meToEnemyWall > limit) {
this.moveToNet();
}
if (this.meToEnemyWall <= limit) {
this.stopMovement();
}
if (this.ballToEnemyWall <= 730) {
this.jump();
}
if (this.ballToEnemyWall < 600) {
this.moveToWall();
}
if (this.ballToEnemyWall < 580) {
this.stopMovement();
}
if (this.ballToEnemyWall < 540) {
this.state = -1;
}
}
}
ai.moveLogic = function(me,enemy) {
keysDown[KEY_UP] = false;

if(this.state != -1 || (this.ballToEnemyWall == 800 && this.ballVXToEnemyWall == 0)) {
this.performServe(me);
return;
}
if(this.ballToEnemyWall < 500)
this.state = -1;

var xWhenBallBelowMe = this.ballXWhenBelow(me.y + me.velocityY + 30);

var something;
if(xWhenBallBelowMe < 600) {
something = 0;
} else if(xWhenBallBelowMe < 700) {
something = 10;
} else {
something = 20;
}

if(xWhenBallBelowMe < 450) {
if (Math.abs(this.meToEnemyWall - 666) < 10) {
this.stopMovement();
} else if (666 < this.meToEnemyWall) {
this.moveToNet();
} else if (666 > this.meToEnemyWall) {
this.moveToWall();
}
} else if (Math.abs(this.meToEnemyWall - xWhenBallBelowMe - something) < 10) {
this.stopMovement();
} else if (xWhenBallBelowMe + something < this.meToEnemyWall) {
this.moveToNet();
} else if (xWhenBallBelowMe + something > this.meToEnemyWall) {
this.moveToWall();
}

if ( (this.meToEnemyWall <= 900 || Math.random() >= 0.4) &&
xWhenBallBelowMe >= 620 &&
(ball.y >= 130 || ball.velocityY >= 0) &&
(Math.random() >= 0.6)) {

if ((this.meToEnemyWall >= 900 && this.ballToEnemyWall > 830) ||
(this.meToEnemyWall <= 580 && this.ballToEnemyWall < 530 &&
Math.abs(this.ballToEnemyWall - this.meToEnemyWall) < 100)) {
this.jump();
} else if (this.ballToEnemyWall != this.meToEnemyWall &&
Math.pow(this.ballToEnemyWall - this.meToEnemyWall,2) * 2 + Math.pow(ball.y - me.y,2) < Math.pow(185,2)) {
this.jump();
} else if (this.ballToEnemyWall != this.meToEnemyWall &&
this.ballVXToEnemyWall * this.ballVXToEnemyWall + ball.velocityY * ball.velocityY < 20 &&
this.ballToEnemyWall - this.meToEnemyWall < 30) {
this.jump();
} else if (Math.abs(this.ballToEnemyWall - this.meToEnemyWall) < 150) {
if ((ball.y > 50) && (ball.y < 250)) {
this.jump();
}
}
}
};
}

var slimeAIs = [
{name:"Pathetic White Slime",
color:'#fff',
backImageName:'sky',
legacySkyColor:'#00f',
legacyGroundColor:'#888',
legacyBallColor:'#ff0',
newGroundColor:'#ca6',
backTextColor:'#000',
//initAI:setMasterSlime},
initAI:setPatheticWhiteSlime},
{name:"Angry Red Slime",
color:'#f70',
color:'#f00',
backImageName:'cave',
legacySkyColor:'#f00',
legacySkyColor:'#1e5000',
legacyGroundColor:'#444',
legacyBallColor:'#88f',
newGroundColor:'#444',
backTextColor:'#fff',
initAI:setAngryRedSlime},
{name:"Slime Master",
color:'#000',
backImageName:'sunset',
legacySkyColor:'#623939',
legacyGroundColor:'#00a800',
legacyBallColor:'#fff',
newGroundColor:'#655040',
backTextColor:'#fff',
initAI:setMasterSlime}
];
30 changes: 21 additions & 9 deletions SlimeVolleyballLegacy.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
ctx.drawImage(ballImage, -radiusPix, -radiusPix);
ctx.setTransform(1,0,0,1,0,0);
} else {
ctx.fillStyle = this.color;
ctx.fillStyle = legacyBallColor;
ctx.beginPath();
ctx.arc(xPix, yPix, radiusPix, 0, TWO_PI);
ctx.fill();
Expand Down Expand Up @@ -157,6 +157,9 @@
var pixelsPerUnitY;
var updatesToPaint;
var legacySkyColor;
var legacyGroundColor;
var legacyBallColor;
var newGroundColor;
var backImage;
var backTextColor;
var backImages = {};
Expand Down Expand Up @@ -373,17 +376,14 @@
if(legacyGraphics) {
ctx.fillStyle=legacySkyColor;
ctx.fillRect(0, 0, viewWidth, courtYPix);
ctx.fillStyle='#aaa'
ctx.fillRect(0, courtYPix, viewWidth, viewHeight - courtYPix);
ctx.fillStyle='#fff'
ctx.fillRect(viewWidth/2-2,7*viewHeight/10,4,viewHeight/10+5);
ctx.fillStyle = legacyGroundColor;
} else {
ctx.drawImage(backImage, 0, 0);
ctx.fillStyle='#ca6'
ctx.fillRect(0, courtYPix, viewWidth, viewHeight - courtYPix);
ctx.fillStyle='#fff'
ctx.fillRect(viewWidth/2-2,7*viewHeight/10,4,viewHeight/10+5);
ctx.fillStyle = newGroundColor;
}
ctx.fillRect(0, courtYPix, viewWidth, viewHeight - courtYPix);
ctx.fillStyle='#fff'
ctx.fillRect(viewWidth/2-2,7*viewHeight/10,4,viewHeight/10+5);
// render scores
renderPoints(slimeLeftScore, 30, 40);
renderPoints(slimeRightScore, viewWidth - 30, -40);
Expand Down Expand Up @@ -619,6 +619,11 @@
localCaveImage.onload = function() {
backImages['cave'] = this;
}
var sunsetImage = new Image();
sunsetImage.src = 'sunset.jpg';
sunsetImage.onload = function() {
backImages['sunset'] = this;
}

var localBallImage = new Image();
localBallImage.src = 'vball.png';
Expand Down Expand Up @@ -658,6 +663,9 @@
legacySkyColor = slimeAIProps.legacySkyColor;
backImage = backImages[slimeAIProps.backImageName];
backTextColor = slimeAIProps.backTextColor;
legacyGroundColor= slimeAIProps.legacyGroundColor;
legacyBallColor = slimeAIProps.legacyBallColor;
newGroundColor = slimeAIProps.newGroundColor;

slimeRight.img = null;
slimeAI = newSlimeAI(false,slimeAIProps.name);
Expand All @@ -666,6 +674,10 @@
legacySkyColor = '#00f';
backImage = backImages['sky'];
backTextColor = '#000';
legacyGroundColor= '#888';
legacyBallColor = '#fff';
newGroundColor = '#ca6';

slimeRight.img = redSlimeImage;
slimeAI = null;
}
Expand Down
Binary file added sunset.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1934b6a

Please sign in to comment.