Added energy consumption and display, updated Ship prefab to use the new Oversimplified.GameObject.SimpleMove() to stop if crashed.

This commit is contained in:
Robbie Antenesse 2016-04-07 18:20:23 -06:00
parent 2c25adab8b
commit bfcb459d74
2 changed files with 52 additions and 10 deletions

View File

@ -20,7 +20,9 @@ var pr_ship = OS.P.Add("Ship", {
moveStepSize: 3, moveStepSize: 3,
moveStepAmount: 5 * Oversimplified.R[Oversimplified.R.currentRoom].stepSpeed, moveStepAmount: 5 * Oversimplified.R[Oversimplified.R.currentRoom].stepSpeed,
moveStepProgress: 0, moveStepProgress: 0,
doTakeStep: false doTakeStep: false,
energyRefillTimer: 0
}); });
pr_ship.Do = function () { pr_ship.Do = function () {
@ -36,6 +38,9 @@ pr_ship.Do = function () {
} else if (ct_down().down) { } else if (ct_down().down) {
this.currentSpeed--; this.currentSpeed--;
} }
if (this.currentSpeed > 1 && G.stats.maxEnergy / G.stats.energy > this.currentSpeed + G.stats.crew) {
this.currentSpeed--;
}
this.currentSpeed = Math.clamp(this.currentSpeed, 0, 4); this.currentSpeed = Math.clamp(this.currentSpeed, 0, 4);
this.moveStepProgress += this.currentSpeed * this.moveStepAmount; this.moveStepProgress += this.currentSpeed * this.moveStepAmount;
@ -47,52 +52,74 @@ pr_ship.Do = function () {
} }
this.SeamlessScroll(); this.SeamlessScroll();
console.log(G.player.name + " created at " + G.player.x + ", " + G.player.y); // console.log(G.player.name + " created at " + G.player.x + ", " + G.player.y);
} }
pr_ship.AfterDo = function () { pr_ship.AfterDo = function () {
this.CheckMovement(); this.CheckMovement();
this.UpdateEnergy();
} }
pr_ship.CheckMovement = function () { pr_ship.CheckMovement = function () {
var moveAmount = (G.stats.speed + this.currentSpeed) * OS.S.pixelScale; var moveAmount = (G.stats.speed + this.currentSpeed) * OS.S.pixelScale;
var movedSuccessfully = false;
switch (this.direction) { switch (this.direction) {
case 0: case 0:
if (this.image.currentAnimation != "Ship Right") this.SetAnimation("Ship Right"); if (this.image.currentAnimation != "Ship Right") this.SetAnimation("Ship Right");
if (this.doTakeStep) this.SimpleMove(moveAmount, 0, true); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(moveAmount, 0, true, 8);
break; break;
case 45: case 45:
if (this.image.currentAnimation != "Ship Up-Right") this.SetAnimation("Ship Up-Right"); if (this.image.currentAnimation != "Ship Up-Right") this.SetAnimation("Ship Up-Right");
if (this.doTakeStep) this.SimpleMove(moveAmount, -moveAmount, true); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(moveAmount, -moveAmount, true, 8);
break; break;
case 90: case 90:
if (this.image.currentAnimation != "Ship Up") this.SetAnimation("Ship Up"); if (this.image.currentAnimation != "Ship Up") this.SetAnimation("Ship Up");
if (this.doTakeStep) this.SimpleMove(0, -moveAmount, true); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(0, -moveAmount, true, 8);
break; break;
case 135: case 135:
if (this.image.currentAnimation != "Ship Up-Left") this.SetAnimation("Ship Up-Left"); if (this.image.currentAnimation != "Ship Up-Left") this.SetAnimation("Ship Up-Left");
if (this.doTakeStep) this.SimpleMove(-moveAmount, -moveAmount, true); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(-moveAmount, -moveAmount, true, 8);
break; break;
case 180: case 180:
if (this.image.currentAnimation != "Ship Left") this.SetAnimation("Ship Left"); if (this.image.currentAnimation != "Ship Left") this.SetAnimation("Ship Left");
if (this.doTakeStep) this.SimpleMove(-moveAmount, 0, true); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(-moveAmount, 0, true, 8);
break; break;
case 225: case 225:
if (this.image.currentAnimation != "Ship Down-Left") this.SetAnimation("Ship Down-Left"); if (this.image.currentAnimation != "Ship Down-Left") this.SetAnimation("Ship Down-Left");
if (this.doTakeStep) this.SimpleMove(-moveAmount, moveAmount, true); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(-moveAmount, moveAmount, true, 8);
break; break;
case 270: case 270:
if (this.image.currentAnimation != "Ship Down") this.SetAnimation("Ship Down"); if (this.image.currentAnimation != "Ship Down") this.SetAnimation("Ship Down");
if (this.doTakeStep) this.SimpleMove(0, moveAmount, true); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(0, moveAmount, true, 8);
break; break;
case 315: case 315:
if (this.image.currentAnimation != "Ship Down-Right") this.SetAnimation("Ship Down-Right"); if (this.image.currentAnimation != "Ship Down-Right") this.SetAnimation("Ship Down-Right");
if (this.doTakeStep) this.SimpleMove(moveAmount, moveAmount, true); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(moveAmount, moveAmount, true, 8);
break; break;
default: default:
console.log("No valid direction"); console.log("No valid direction");
break; break;
} }
if (this.doTakeStep && !movedSuccessfully) {
this.currentSpeed = 0;
}
}
pr_ship.UpdateEnergy = function () {
this.energyRefillTimer++;
if (this.doTakeStep) {
G.stats.energy -= ((this.currentSpeed / G.stats.speed) + ((G.stats.hunger + G.stats.thirst) * 0.1)) * 0.25;
if (this.energyRefillTimer >= (100 / G.stats.crew) + ((G.stats.hunger + G.stats.thirst) * 10)) {
G.stats.energy += G.stats.crew;
this.energyRefillTimer = 0;
}
if (G.stats.energy < 0) G.stats.energy = 0;
if (G.stats.energy > G.stats.maxEnergy) G.stats.energy = G.stats.maxEnergy;
}
} }
pr_ship.SeamlessScroll = function () { pr_ship.SeamlessScroll = function () {

View File

@ -56,6 +56,9 @@ rm_Ocean.DrawAbove = function () {
// Draw the speed indicator in Bottom Left corner. // Draw the speed indicator in Bottom Left corner.
OS.context.drawImage(rm_Ocean.speedGaugeImg, G.player.currentSpeed * 32, 0, 32, 32, 16, OS.camera.height - 32 - 16, 32, 32); OS.context.drawImage(rm_Ocean.speedGaugeImg, G.player.currentSpeed * 32, 0, 32, 32, 16, OS.camera.height - 32 - 16, 32, 32);
// Draw Energy Bar
this.DrawEnergyBar();
// drawPixelText("Testing 1 2 3!", 0, 0, 0, "white", 4); // drawPixelText("Testing 1 2 3!", 0, 0, 0, "white", 4);
// drawPixelText("Testing 1 2 3!", 0, 64, 0, "white", 6); // drawPixelText("Testing 1 2 3!", 0, 64, 0, "white", 6);
} }
@ -64,3 +67,15 @@ rm_Ocean.DoLast = function () {
// Clear Objects on room exit. This is best practice unless you need persistent objects. // Clear Objects on room exit. This is best practice unless you need persistent objects.
//rm_Ocean.objects = {}; //rm_Ocean.objects = {};
} }
rm_Ocean.DrawEnergyBar = function () {
var percentage = G.stats.energy / G.stats.maxEnergy;
var barHeight = 2 * OS.S.pixelScale;
var maxBarWidth = 32;
var barWidth = Math.round(maxBarWidth * percentage) * OS.S.pixelScale;
var saveFillStyle = OS.context.fillStyle;
OS.context.fillStyle = "#0055FF";
OS.context.fillRect(64, OS.camera.height - barHeight - 16, barWidth, barHeight);
OS.context.fillStyle = saveFillStyle;
}