Files
chocomae/src/game/mouse/grilled_meat/meat_base.js
T

366 lines
10 KiB
JavaScript

MeatBase.prototype = Object.create(Phaser.Sprite.prototype);
MeatBase.constructor = MeatBase;
function MeatBase() {
Phaser.Sprite.call(this, game, 500, Game.MEAT_PLATE_POSITION_Y, '');
this.anchor.set(0.5);
this.smoothed = false;
game.add.existing(this);
this.initMeatTypeVariables();
this.initMouseEventHandler();
this.whiteSmokeParticle = new Smoke(Meat.DONENESS_RARE, 0, 0);
this.redSmokeParticle = new Smoke(Meat.DONENESS_WELLDONE, 0, 0);
this.blackSmokeParticle = new Smoke(Meat.DONENESS_BURN_BLACK, 0, 0);
}
MeatBase.prototype.update = function() {
var posY = this.y;
if(posY <= God.POSITION_Y) {
this.scale.set(this.baseScale);
} else if(posY > God.POSITION_Y && posY < HeatPlate.POSITION_Y) {
var scaleRate = (posY - God.POSITION_Y) / (HeatPlate.POSITION_Y - God.POSITION_Y);
this.scale.set(this.baseScale + 0.3 * scaleRate);
} else {
this.scale.set(this.baseScale + 0.3);
}
if(this.isOnHeatPlate == true && game.input.mousePointer.isUp == true) {
this.cooking();
}
}
MeatBase.prototype.initMeatTypeVariables = function() {
this.className = "MeatBase";
this.baseScale = 0.5;
this.cookingSide = MeatBase.MEAT_NONE;
this.cookingTime = [];
this.cookingGrade = [];
this.cookingTime[MeatBase.MEAT_BACK] = 0;
this.cookingTime[MeatBase.MEAT_FRONT] = 0;
this.cookingGrade[MeatBase.MEAT_BACK] = MeatBase.DONENESS_RARE;
this.cookingGrade[MeatBase.MEAT_FRONT] = MeatBase.DONENESS_RARE;
this.isActivated = true;
this.isStartCook = false;
this.isOnHeatPlate = false;
this.spriteNameRare = "";
this.spriteNameWelldone = "";
this.spriteNameBurnBlack = "";
this.initCookingSpeedArray();
this.initScoreArray();
}
MeatBase.prototype.initCookingSpeedArray = function() {
this.cookingSpeedArray = [];
this.cookingSpeedArray[HeatPlate.BURN_LEVEL_WEAK] = 1;
this.cookingSpeedArray[HeatPlate.BURN_LEVEL_NORMAL] = 2;
this.cookingSpeedArray[HeatPlate.BURN_LEVEL_STRONG] = 3;
}
MeatBase.prototype.initScoreArray = function() {
this.scoreArray = [];
this.scoreArray[MeatBase.DONENESS_RARE] = 0;
this.scoreArray[MeatBase.DONENESS_WELLDONE] = 0;
this.scoreArray[MeatBase.DONENESS_BURN_BLACK] = 0;
}
MeatBase.prototype.initMouseEventHandler = function() {
this.downPositionX;
this.downPositionY;
this.inputEnabled = true;
this.input.enableDrag();
this.events.onInputDown.add(this.onInputDown, this);
this.events.onInputUp.add(this.onInputUp, this);
}
MeatBase.prototype.onInputDown = function(sprite, pointer) {
// console.log("-------------------------");
// console.log("onInputDown : " + pointer.x + ", " + pointer.y);
this.downPositionX = Math.floor(sprite.x);
this.downPositionY = Math.floor(sprite.y);
// this.stopCook();
}
MeatBase.prototype.onInputUp = function(sprite, pointer) {
// console.log("-------------------------");
// console.log("onInputUp : " + pointer.x + ", " + pointer.y);
var upPositionX = Math.floor(sprite.x);
var upPositionY = Math.floor(sprite.y);
// console.log("downPositionX : + " + this.downPositionX + ", downPositY : " + this.downPositionY);
// console.log("upPositionX : + " + upPositionX + ", upPositionY : " + upPositionY);
if(this.isInClickArea(this.downPositionX, this.downPositionY, upPositionX, upPositionY) == true)
this.onClickListener();
else {
this.onDragStopListener();
}
}
MeatBase.prototype.isInClickArea = function(downX, downY, upX, upY) {
if(downX - 5 < upX && upX < downX + 5 && downY - 5 < upY && upY < downY + 5)
return true;
return false;
}
MeatBase.prototype.onClickListener = function(pointer) {
// console.log("onClickListener");
if(this.mainGame.isOverHeatPlate(this.x, this.y, this.width, this.height) == true) {
if(this.isFront() == true)
this.flipToBack()
else
this.flipToFront();
// console.log("flip cooking side : " + this.cookingSide);
this.startCook();
}
}
MeatBase.prototype.onDragStopListener = function(pointer) {
// console.log("onDragStop");
if(this.mainGame.isOverHeatPlate(this.x, this.y, this.width, this.height) == true) {
// console.log("isOverHeatPlate : " + isOverHeatPlate(this.x, this.y, this.width, this.height));
this.startCook();
} else {
this.stopCook();
}
if(this.mainGame.isOverGod(this.x, this.y) == true) {
// console.log("isOverGod : " + isOverGod(this.x, this.y));
this.mainGame.godEatMeat(this);
} else if(this.mainGame.isOverTrashCan(this.x, this.y) == true) {
this.mainGame.throwAway(this);
}
}
MeatBase.prototype.getClassName = function() {
return this.className;
}
MeatBase.prototype.getTotalCookingTime = function() {
return this.cookingTime[MeatBase.MEAT_BACK] + this.cookingTime[MeatBase.MEAT_FRONT];
}
MeatBase.prototype.getScore = function() {
var meatDoneness = this.getMeatGrade();
var scoreDoneness = this.scoreArray[meatDoneness];
var totalCookingTime = this.getTotalCookingTime();
if(meatDoneness == MeatBase.DONENESS_BURN_BLACK)
return scoreDoneness;
return scoreDoneness * totalCookingTime;
}
MeatBase.prototype.isFront = function() {
if(this.cookingSide == MeatBase.MEAT_BACK)
return true;
return false;
}
MeatBase.prototype.flipToFront = function() {
this.cookingSide = MeatBase.MEAT_BACK;
// console.log("flipToFront : " + this.cookingSide);
this.angle = MeatBase.ANGLE_FRONT;
// console.log("flipToFront");
// console.log("back : " + this.getMeatGradeBySide(MeatBase.MEAT_BACK) + ", front : " + this.getMeatGradeBySide(MeatBase.MEAT_FRONT));
this.upsideDownSprite(MeatBase.MEAT_FRONT);
}
MeatBase.prototype.flipToBack = function() {
this.cookingSide = MeatBase.MEAT_FRONT;
// console.log("flipToBack : " + this.cookingSide);
this.angle = MeatBase.ANGLE_BACK;
// console.log("flipToBack");
// console.log("back : " + this.getMeatGradeBySide(MeatBase.MEAT_BACK) + ", front : " + this.getMeatGradeBySide(MeatBase.MEAT_FRONT));
this.upsideDownSprite(MeatBase.MEAT_BACK);
}
MeatBase.prototype.upsideDownSprite = function(meatDirection) {
if(this.getMeatGradeBySide(meatDirection) == MeatBase.DONENESS_BURN_BLACK) {
// console.log("sprite burn black");
this.loadTexture(this.spriteNameBurnBlack);
} else if(this.getMeatGradeBySide(meatDirection) == MeatBase.DONENESS_WELLDONE) {
// console.log("sprite welldone");
this.loadTexture(this.spriteNameWelldone);
} else {
// console.log("sprite rare");
this.loadTexture(this.spriteNameRare);
}
}
MeatBase.prototype.show = function() {
this.isActivated = true;
this.alpha = 1;
}
MeatBase.prototype.hide = function() {
this.isActivated = false;
this.alpha = 0;
this.stopCook();
}
MeatBase.prototype.isActivate = function() {
return this.isActivated;
}
MeatBase.prototype.startCook = function() {
this.isStartCook = true;
this.isOnHeatPlate = true;
// console.log("this.isOnHeatPlate : " + this.isOnHeatPlate);
// console.log("startCook : " + this.cookingSide);
if(this.cookingSide == MeatBase.MEAT_NONE)
this.flipToFront();
this.startAnimateSmoke();
}
MeatBase.prototype.stopCook = function() {
this.isOnHeatPlate = false;
// console.log("this.isOnHeatPlate : " + this.isOnHeatPlate);
this.stopAnimateSmoke();
}
MeatBase.prototype.getMeatGrade = function() {
if(this.cookingGrade[MeatBase.MEAT_FRONT] == MeatBase.DONENESS_WELLDONE && this.cookingGrade[MeatBase.MEAT_BACK] == MeatBase.DONENESS_WELLDONE)
return MeatBase.DONENESS_WELLDONE;
else if(this.cookingGrade[MeatBase.MEAT_FRONT] == MeatBase.DONENESS_BURN_BLACK || this.cookingGrade[MeatBase.MEAT_BACK] == MeatBase.DONENESS_BURN_BLACK)
return MeatBase.DONENESS_BURN_BLACK;
else
return MeatBase.DONENESS_RARE;
}
MeatBase.prototype.getMeatGradeBySide = function(cookingSide) {
return this.cookingGrade[cookingSide];
}
MeatBase.prototype.setActive = function(isActivated, x, y) {
this.isActivated = isActivated;
if(isActivated == false) {
this.alpha = 0;
this.x = -100;
this.y = -100;
return;
}
this.cookingSide = MeatBase.MEAT_NONE;
this.cookingTime[MeatBase.MEAT_BACK] = 0;
this.cookingTime[MeatBase.MEAT_FRONT] = 0;
this.cookingGrade[MeatBase.MEAT_BACK] = MeatBase.DONENESS_RARE;
this.cookingGrade[MeatBase.MEAT_FRONT] = MeatBase.DONENESS_RARE;
this.x = x;
this.y = y;
this.loadTexture(this.spriteNameRare);
this.angle = MeatBase.ANGLE_NONE;
this.alpha = 1;
}
MeatBase.prototype.cooking = function() {
this.cookingTime[this.cookingSide] += 1;
// console.log("back : " + this.cookingTime[MeatBase.MEAT_BACK] + ", front : " + this.cookingTime[MeatBase.MEAT_FRONT]);
if(this.cookingTime[MeatBase.MEAT_BACK] > MeatBase.COOK_TIME_BURN) {
if(this.getMeatGradeBySide(MeatBase.MEAT_BACK) != MeatBase.DONENESS_BURN_BLACK) {
this.cookingGrade[MeatBase.MEAT_BACK] = MeatBase.DONENESS_BURN_BLACK;
this.changeAnimateSmokeColor(MeatBase.DONENESS_BURN_BLACK);
}
} else if(this.cookingTime[MeatBase.MEAT_BACK] > MeatBase.COOK_TIME_WELLDONE) {
if(this.getMeatGradeBySide(MeatBase.MEAT_BACK) != MeatBase.DONENESS_WELLDONE) {
this.cookingGrade[MeatBase.MEAT_BACK] = MeatBase.DONENESS_WELLDONE;
this.changeAnimateSmokeColor(MeatBase.DONENESS_WELLDONE);
}
}
if(this.cookingTime[MeatBase.MEAT_FRONT] > MeatBase.COOK_TIME_BURN) {
if(this.getMeatGradeBySide(MeatBase.MEAT_FRONT) != MeatBase.DONENESS_BURN_BLACK) {
this.cookingGrade[MeatBase.MEAT_FRONT] = MeatBase.DONENESS_BURN_BLACK;
this.changeAnimateSmokeColor(MeatBase.DONENESS_BURN_BLACK);
}
} else if(this.cookingTime[MeatBase.MEAT_FRONT] > MeatBase.COOK_TIME_WELLDONE) {
if(this.getMeatGradeBySide(MeatBase.MEAT_FRONT) != MeatBase.DONENESS_WELLDONE) {
this.cookingGrade[MeatBase.MEAT_FRONT] = MeatBase.DONENESS_WELLDONE;
this.changeAnimateSmokeColor(MeatBase.DONENESS_WELLDONE);
}
}
}
MeatBase.prototype.stopAnimateSmoke = function() {
this.whiteSmokeParticle.stop();
this.redSmokeParticle.stop();
this.blackSmokeParticle.stop();
}
MeatBase.prototype.startAnimateSmoke = function() {
var cookingSideGrade = this.cookingGrade[this.cookingSide];
this.changeAnimateSmokeColor(cookingSideGrade);
}
MeatBase.prototype.changeAnimateSmokeColor = function(type) {
// console.log("changeAnimateSmokeColor : " + type);
this.stopAnimateSmoke();
switch(type) {
case MeatBase.DONENESS_RARE:
this.whiteSmokeParticle.start(this.x, this.y);
break;
case MeatBase.DONENESS_WELLDONE:
this.redSmokeParticle.start(this.x, this.y);
break;
case MeatBase.DONENESS_BURN_BLACK:
this.blackSmokeParticle.start(this.x, this.y);
break;
}
}
MeatBase.CLASS_NAME = "MeatBase";
MeatBase.MEAT_NONE = 0;
MeatBase.MEAT_FRONT = 1;
MeatBase.MEAT_BACK = 2;
MeatBase.ANGLE_NONE = 0;
MeatBase.ANGLE_FRONT = 30;
MeatBase.ANGLE_BACK = -30;
MeatBase.DONENESS_RARE = 0;
MeatBase.DONENESS_WELLDONE = 1;
MeatBase.DONENESS_BURN_BLACK = 2;
MeatBase.DONENESS_NONE = 3;
MeatBase.COOK_TIME_WELLDONE = 200;
MeatBase.COOK_TIME_BURN = 400;