Files
chocomae/src/game/lib/animal.js
T

175 lines
6.5 KiB
JavaScript

class Animal {
constructor(type, x, y) {
this.animalType = type;
this.sprite = null;
this.posX = x;
this.posY = y;
}
animalLevelIDByRecord(record) {
for(let i = Animal.SPECIES_DATA.length - 1; i > 0; i--) {
if(record >= this.typingCount(Animal.SPECIES_DATA[i]))
return i;
}
return 0;
}
typingCount(speciesData) {
if(sessionStorageManager.playingAppID <= 20)
return speciesData.practiceTypingCount;
else
return speciesData.testTypingCount;
}
loadSpriteNames() {
let spriteNames = {};
spriteNames.icon = this.species.species + Animal.SPRITE_NAMES.icon;
spriteNames.run1 = this.species.species + Animal.SPRITE_NAMES.run1;
spriteNames.run2 = this.species.species + Animal.SPRITE_NAMES.run2;
return spriteNames;
}
setSpecies(species) {
this.species = species;
if(this.animalType === Animal.TYPE_ICON)
return;
this.setupAnimation();
}
tweenAnimation(animationType) {
switch(animationType) {
case Animal.ANIMATION_TYPE_CHANGE:
this.sprite.width = 200;
this.sprite.height = 200;
game.add.tween(this.sprite).to( { width: 150, height: 150 }, 500, Phaser.Easing.Bounce.Out, true);
break;
case Animal.ANIMATION_TYPE_DAMAGE:
this.sprite.width = 200;
game.add.tween(this.sprite).to( { width: 150 }, 500, Phaser.Easing.Bounce.In, true);
// game.add.tween(this.sprite).to( { width: 50 }, 500, Phaser.Easing.Elastic.Out, true);
break;
}
}
setupAnimation() {
this.spriteFrameNames = this.loadSpriteNames();
this.playingSpriteFrameName = this.spriteFrameNames.run1;
this.animationFrameID = 1;
this.animationSpeedSec = 1000 / this.species.runningFPS;
if(this.sprite === null) {
this.sprite = game.add.sprite(this.posX, this.posY, this.spriteFrameNames.run1);
this.sprite.anchor.set(0.5);
this.sprite.width = 150;
this.sprite.height = 150;
}
this.stopAnimation();
this.animateionUpdate();
}
startAnimation() {
if(this.timerEvent === null)
this.timerEvent = game.time.events.loop(this.animationSpeedSec, this.animateionUpdate, this);
}
animateionUpdate() {
this.animationFrameID = 1 - this.animationFrameID;
if(this.animationFrameID === 0) {
this.sprite.loadTexture(this.spriteFrameNames.run2);
} else {
this.sprite.loadTexture(this.spriteFrameNames.run1);
}
}
stopAnimation() {
if(this.timerEvent !== null) {
game.time.events.remove(this.timerEvent);
this.timerEvent = null;
}
}
static loadResources() {
game.load.image('snail_shadow', '../../../resources/image/character/animal/snail/shadow.png');
game.load.image('snail_icon', '../../../resources/image/character/animal/snail/run2.png');
game.load.image('snail_run1', '../../../resources/image/character/animal/snail/run1.png');
game.load.image('snail_run2', '../../../resources/image/character/animal/snail/run2.png');
game.load.image('turtle_icon', '../../../resources/image/character/animal/turtle/run2.png');
game.load.image('turtle_run1', '../../../resources/image/character/animal/turtle/run1.png');
game.load.image('turtle_run2', '../../../resources/image/character/animal/turtle/run2.png');
game.load.image('cat_icon', '../../../resources/image/character/animal/cat/run2.png');
game.load.image('cat_run1', '../../../resources/image/character/animal/cat/run1.png');
game.load.image('cat_run2', '../../../resources/image/character/animal/cat/run2.png');
game.load.image('lion_icon', '../../../resources/image/character/animal/lion/run2.png');
game.load.image('lion_run1', '../../../resources/image/character/animal/lion/run1.png');
game.load.image('lion_run2', '../../../resources/image/character/animal/lion/run2.png');
game.load.image('rabbit_icon', '../../../resources/image/character/animal/rabbit/run2.png');
game.load.image('rabbit_run1', '../../../resources/image/character/animal/rabbit/run1.png');
game.load.image('rabbit_run2', '../../../resources/image/character/animal/rabbit/run2.png');
game.load.image('ostrich_icon', '../../../resources/image/character/animal/ostrich/run2.png');
game.load.image('ostrich_run1', '../../../resources/image/character/animal/ostrich/run1.png');
game.load.image('ostrich_run2', '../../../resources/image/character/animal/ostrich/run2.png');
game.load.image('horse_icon', '../../../resources/image/character/animal/horse/run2.png');
game.load.image('horse_run1', '../../../resources/image/character/animal/horse/run1.png');
game.load.image('horse_run2', '../../../resources/image/character/animal/horse/run2.png');
game.load.image('dog_icon', '../../../resources/image/character/animal/dog/run2.png');
game.load.image('dog_run1', '../../../resources/image/character/animal/dog/run1.png');
game.load.image('dog_run2', '../../../resources/image/character/animal/dog/run2.png');
game.load.image('cheetah_icon', '../../../resources/image/character/animal/cheetah/run2.png');
game.load.image('cheetah_run1', '../../../resources/image/character/animal/cheetah/run1.png');
game.load.image('cheetah_run2', '../../../resources/image/character/animal/cheetah/run2.png');
game.load.image('eagle_icon', '../../../resources/image/character/animal/eagle/run2.png');
game.load.image('eagle_run1', '../../../resources/image/character/animal/eagle/run1.png');
game.load.image('eagle_run2', '../../../resources/image/character/animal/eagle/run2.png');
}
}
Animal.TYPE_ANIMATION = 0;
Animal.TYPE_ICON = 1;
Animal.ANIMATION_TYPE_CHANGE = 0;
Animal.ANIMATION_TYPE_DAMAGE = 1;
Animal.SPECIES_DATA = [
{ "species" : "snail", "runningFPS" : 1.1, "practiceTypingCount" : 0, "testTypingCount" : 0 },
{ "species" : "turtle", "runningFPS" : 2.2, "practiceTypingCount" : 5, "testTypingCount" : 20 },
{ "species" : "cat", "runningFPS" : 4.4, "practiceTypingCount" : 10, "testTypingCount" : 50 },
{ "species" : "lion", "runningFPS" : 6.9, "practiceTypingCount" : 15, "testTypingCount" : 100 },
{ "species" : "rabbit", "runningFPS" : 9.3, "practiceTypingCount" : 20, "testTypingCount" : 200 },
{ "species" : "ostrich", "runningFPS" : 12.8, "practiceTypingCount" : 30, "testTypingCount" : 300 },
{ "species" : "horse", "runningFPS" : 16.2, "practiceTypingCount" : 40, "testTypingCount" : 400 },
{ "species" : "dog", "runningFPS" : 20.1, "practiceTypingCount" : 50, "testTypingCount" : 500 },
{ "species" : "cheetah", "runningFPS" : 25.4, "practiceTypingCount" : 60, "testTypingCount" : 600 },
{ "species" : "eagle", "runningFPS" : 30.5, "practiceTypingCount" : 80, "testTypingCount" : 700 }
];
Animal.SPRITE_NAMES = {
"icon" : "_icon",
"run1" : "_run1",
"run2" : "_run2"
};