function Animal(type, x, y) { this.animalType = type; this.sprite = null; this.posX = x; this.posY = y; this.sprite = game.add.sprite(this.posX, this.posY); //, "snail_run1"); // this.spriteFrameNames.run1); this.sprite.anchor.set(0.5); } Animal.prototype.setIconSprite = function(species) { this.setSpecies(species); this.spriteFrameNames = this.loadSpriteNames(); this.sprite.loadTexture(this.spriteFrameNames.run1); } Animal.prototype.setSpecies = function(species) { this.species = species; } Animal.prototype.loadSpriteNames = function() { 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; } Animal.prototype.setAlpha = function(value) { this.sprite.alpha = value; } Animal.prototype.tweenAnimation = function(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; } } Animal.prototype.setupAnimation = function() { this.spriteFrameNames = this.loadSpriteNames(); this.playingSpriteFrameName = this.spriteFrameNames.run1; this.animationFrameID = 1; this.animationSpeedSec = 1000 / this.species.runningFPS; this.stopAnimation(); this.animateionUpdate(); } Animal.prototype.startAnimation = function(species) { this.setSpecies(species); this.setupAnimation(); if(this.timerEvent === null) this.timerEvent = game.time.events.loop(this.animationSpeedSec, this.animateionUpdate, this); } Animal.prototype.animateionUpdate = function() { this.animationFrameID = 1 - this.animationFrameID; if(this.animationFrameID === 0) { this.sprite.loadTexture(this.spriteFrameNames.run2); } else { this.sprite.loadTexture(this.spriteFrameNames.run1); } this.sprite.width = 150; this.sprite.height = 150; } Animal.prototype.stopAnimation = function() { if(this.timerEvent !== null) { game.time.events.remove(this.timerEvent); this.timerEvent = null; } } Animal.animalLevelIDByRecord = function(record, gameType) { for(let i = Animal.SPECIES_DATA.length - 1; i > 0; i--) { if(record >= this.typingCount(Animal.SPECIES_DATA[i], gameType)) return i; } return 0; } Animal.typingCount = function(speciesData, gameType) { if(gameType === Animal.TYPE_PRACTICE) return speciesData.practiceTypingCount; else return speciesData.testTypingCount; } Animal.loadResources = function() { 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.TYPE_PRACTICE = 0; Animal.TYPE_TEST = 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" : 3.3, "practiceTypingCount" : 10, "testTypingCount" : 50 }, { "species" : "lion", "runningFPS" : 4.4, "practiceTypingCount" : 15, "testTypingCount" : 100 }, { "species" : "rabbit", "runningFPS" : 5.5, "practiceTypingCount" : 20, "testTypingCount" : 200 }, { "species" : "ostrich", "runningFPS" : 7.7, "practiceTypingCount" : 25, "testTypingCount" : 300 }, { "species" : "horse", "runningFPS" : 9.9, "practiceTypingCount" : 30, "testTypingCount" : 400 }, { "species" : "dog", "runningFPS" : 12.2, "practiceTypingCount" : 35, "testTypingCount" : 500 }, { "species" : "cheetah", "runningFPS" : 16, "practiceTypingCount" : 40, "testTypingCount" : 600 }, { "species" : "eagle", "runningFPS" : 20, "practiceTypingCount" : 50, "testTypingCount" : 700 } ]; Animal.SPRITE_NAMES = { "icon" : "_icon", "run1" : "_run1", "run2" : "_run2" };