anxiety/scripts/game/HP.js

197 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-04-09 18:59:19 +00:00
Loader.addImages([
{ id:"hp", src:"sprites/ui/hp.png" }
]);
2019-02-17 21:54:29 +00:00
2019-04-23 19:12:41 +00:00
Loader.addSounds([
{ id:"hit", src:"sounds/sfx/hit.mp3" },
{ id:"hit_big", src:"sounds/sfx/hit_big.mp3" },
{ id:"hit_bb", src:"sounds/sfx/hit_bb.mp3" }
2019-04-23 19:12:41 +00:00
]);
2019-02-17 21:54:29 +00:00
// The Class!
function HitPoints(){
var self = this;
// My DOM & canvas
2019-04-18 11:40:22 +00:00
self.dom = $("#game_hp");
2019-02-17 21:54:29 +00:00
self.canvas = document.createElement("canvas");
self.canvas.width = 360 * 2;
2019-02-18 17:24:52 +00:00
self.canvas.height = 100 * 2;
2019-02-17 21:54:29 +00:00
self.canvas.style.width = self.canvas.width/2 + "px";
self.canvas.style.height = self.canvas.height/2 + "px";
self.context = self.canvas.getContext("2d");
self.dom.appendChild(self.canvas);
// My sprite
2019-04-09 18:59:19 +00:00
self.image = Library.images.hp;
2019-02-17 21:54:29 +00:00
// My stats
self.reset = function(){
self.hong = 100;
self.beebee = 100;
2019-05-07 19:40:03 +00:00
self.leftWhite = 1;
self.rightWhite = 1;
2019-02-17 21:54:29 +00:00
};
self.reset();
2019-04-18 11:40:22 +00:00
// Show/hide
self.show = function(){
self.dom.style.top = "0px";
};
2019-06-18 20:21:14 +00:00
self.hide = function(instant){
if(instant){
self.dom.style.display = "none";
setTimeout(function(){
self.dom.style.display = "block";
},2000);
}
2019-04-18 11:40:22 +00:00
self.dom.style.top = "-100px";
};
subscribe("hp_show", self.show);
subscribe("hp_hide", self.hide);
2019-02-17 21:54:29 +00:00
// Attack!
self.doDamage = function(str, target){
// Absolute or Relative Damage?
var num = parseFloat(str);
var isAbsolute = (str.slice(-1)=="p"); // p = absolute, % = relative
if(isAbsolute){
self[target] -= num;
}else{
var relativeDamage = Math.floor( self[target] * (num/100) );
self[target] -= relativeDamage;
}
// Floor bound
if(self[target]<0){
self[target] = 0;
}
};
2019-04-18 11:40:22 +00:00
// Who's been attacked?
subscribe("attack", function(target, damage, type){
2019-04-19 15:57:10 +00:00
Game.clearText(); // BYE
2019-04-18 11:40:22 +00:00
if(target=="hong"){
self.doDamage(damage, "hong");
self.leftShake = 30;
publish("attack_hong",[type]);
}else{
self.doDamage(damage, "beebee");
self.rightShake = 30;
2019-06-12 16:23:26 +00:00
publish("attack_bb");
2019-04-18 11:40:22 +00:00
}
2019-04-19 15:57:10 +00:00
2019-04-23 19:12:41 +00:00
// Sound
if(damage=="100p"){ // FULL!
sfx("hit_big");
}else{
sfx("hit");
}
if(target=="bb"){
sfx("hit_bb");
}
2019-04-23 19:12:41 +00:00
2019-04-18 11:40:22 +00:00
});
2019-02-17 21:54:29 +00:00
// Draw
self.leftShake = 0;
2019-05-02 15:55:41 +00:00
self.leftRed = self.leftWhite = 1;
2019-02-17 21:54:29 +00:00
self.rightShake = 0;
2019-05-02 15:55:41 +00:00
self.rightRed = self.rightWhite = 1;
2019-02-17 21:54:29 +00:00
self.drawHalf = function(ctx, isRight){
ctx.save();
// Which side?
var side = isRight ? "right" : "left";
var hp = isRight ? self.beebee : self.hong;
// Shaking
2019-05-02 15:55:41 +00:00
var stoppedShaking = (self[side+"Shake"]==0);
if(stoppedShaking){
self[side+"Shake"]=0;
}else{
2019-02-17 21:54:29 +00:00
var amp = self[side+"Shake"]/7;
var shakeY = Math.sin(self[side+"Shake"]*1.3)*amp;
ctx.translate(0,shakeY);
self[side+"Shake"]--;
}
2019-05-02 15:55:41 +00:00
// Damage
if(!isRight){ // Hong
var d = self.hong/100;
self.leftRed = self.leftRed*0.8 + d*0.2;
if(stoppedShaking){
if(self.leftWhite > self.leftRed){
self.leftWhite -= 0.001;
}
}
}else{
var d = self.beebee/100;
self.rightRed = self.rightRed*0.8 + d*0.2;
if(stoppedShaking){
if(self.rightWhite > self.rightRed){
self.rightWhite -= 0.001;
}
2019-02-17 21:54:29 +00:00
}
}
2019-05-02 15:55:41 +00:00
// BG: Black
var sx=isRight ? 360 : 0, sy=200*0, sw=360, sh=200;
ctx.drawImage(self.image, sx,sy,sw,sh, sx/2,0,sw/2,sh/2);
// Draw White INSIDE (source-atop)
if(self[side+"Shake"]==0){ // if not shaking, slowly reduce
if( self[side+"WhiteWidth"] > self[side+"Width"] ){
self[side+"WhiteWidth"] -= 0.1;
}
}
var sx=isRight ? 360 : 0, sy=200*1, sw=360, sh=200;
2019-05-05 19:56:00 +00:00
var offset = (1-self[side+"White"])*295;//self[side+"WhiteWidth"];
2019-05-02 15:55:41 +00:00
offset *= isRight ? -1 : 1;
ctx.globalCompositeOperation = "source-atop";
ctx.drawImage(self.image, (sx-offset),sy,sw,sh, sx/2,0,sw/2,sh/2);
2019-05-02 15:55:41 +00:00
// Red
var sx=isRight ? 360 : 0, sy=200*2, sw=360, sh=200;
2019-05-05 19:56:00 +00:00
var offset = (1-self[side+"Red"])*295;//self[side+"WhiteWidth"];
2019-05-02 15:55:41 +00:00
offset *= isRight ? -1 : 1;
ctx.globalCompositeOperation = "source-atop";
ctx.drawImage(self.image, (sx-offset),sy,sw,sh, sx/2,0,sw/2,sh/2);
2019-05-02 15:55:41 +00:00
// Restore
2019-02-17 21:54:29 +00:00
ctx.restore();
};
self.draw = function(){
var ctx = self.context;
ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
ctx.save();
ctx.scale(2,2);
// Draw Left & Right Sides
self.drawHalf(ctx, false);
self.drawHalf(ctx, true);
2019-05-02 15:55:41 +00:00
// Draw BG BELOW
ctx.globalCompositeOperation = "destination-over";
var sx=0, sy=200*4, sw=720, sh=200;
ctx.drawImage(self.image, sx,sy,sw,sh, 0,0,sw/2,sh/2);
// Draw "VS"
ctx.globalCompositeOperation = "source-over";
var sx=0, sy=200*3, sw=720, sh=200;
2019-02-17 21:54:29 +00:00
ctx.drawImage(self.image, sx,sy,sw,sh, 0,0,sw/2,sh/2);
ctx.restore();
2019-05-02 15:55:41 +00:00
2019-02-17 21:54:29 +00:00
};
}