Files
chocomae/src/game/typing/word_flyingsaucer/flyingsaucer.js
T
2019-01-06 07:51:54 +09:00

322 lines
8.7 KiB
JavaScript

FlyingSaucer.prototype = Object.create(Phaser.Sprite.prototype);
FlyingSaucer.constructor = FlyingSaucer;
function FlyingSaucer(lineIndex) {
this.lineIndex = lineIndex;
this.initVariables();
Phaser.Sprite.call(this, game, game.world.centerX, game.world.centerY, 'alien_normal');
game.add.existing(this);
game.physics.arcade.enable(this);
this.scale.set(1);
this.anchor.set(0.5);
this.halfWidth = this.width / 2;
var COCKPIT_OFFSET_Y = -5;
var BODY_OFFSET_Y = 28;
var JET_OFFSET_Y = 52;
this.cockpit = this.makeCockpit(0, COCKPIT_OFFSET_Y);
this.addChild(this.cockpit);
this.jetLeft = this.makeJet(-50, JET_OFFSET_Y);
this.addChild(this.jetLeft);
this.jetCenter = this.makeJet(0, JET_OFFSET_Y);
this.addChild(this.jetCenter);
this.jetRight = this.makeJet(50, JET_OFFSET_Y);
this.addChild(this.jetRight);
var shipBody = this.makeShipBody(0, BODY_OFFSET_Y);
this.addChild(shipBody);
this.explodeEmitter = this.makeExplodeEmitter();
var style = { font: "20px Arial", fill: FlyingSaucer.TEXT_COLOR_NORMAL};
this.wordText = game.add.text(0, 0, "text", style);
this.wordText.anchor.set(0.5);
this.setActive(true);
}
FlyingSaucer.prototype.initVariables = function() {
this.isActivated = false;
this.wordLevel = 0;
this.wordScore = 0;
this.speed = FlyingSaucer.DEFAULT_SPEED;
this.jetStatus = FlyingSaucer.JET_STATUS_HIGH;
this.scoreManager = null;
this.heartManager = null;
this.wordManager = null;
this.gameLevelManager = null;
this.timerEvent = null;
}
FlyingSaucer.prototype.makeCockpit = function(posX, posY) {
var cockpit = game.make.sprite(posX, posY, 'ship_cockpit');
cockpit.anchor.set(0.5);
cockpit.alpha = 0.6;
cockpit.tint = 0xff00ff;
return cockpit;
}
FlyingSaucer.prototype.makeJet = function(posX, posY) {
var jet = game.make.sprite(posX, posY, 'ship_jet');
jet.anchor.set(0.5);
jet.tint = FlyingSaucer.JET_COLOR_HIGH;
return jet;
}
FlyingSaucer.prototype.makeShipBody = function(posX, posY) {
var body = game.make.sprite(posX, posY, 'ship_body');
body.anchor.set(0.5);
return body;
}
FlyingSaucer.prototype.makeExplodeEmitter = function() {
explodeEmitter = game.add.emitter(this.x, this.y, 10);
explodeEmitter.makeParticles('star');
explodeEmitter.minParticleScale = 1;
explodeEmitter.maxParticleScale = 3;
explodeEmitter.forEach( function(particle) { particle.tint = 0xadff2f; } );
explodeEmitter.gravity = 0;
return explodeEmitter;
}
FlyingSaucer.prototype.update = function() {
if(this.isActivated) {
var deltaTime = game.time.elapsed / 1000;
this.y += this.speed * deltaTime;
this.wordText.y = this.y + FlyingSaucer.WORD_TEXT_OFFSET_Y;
this.updateJetColor();
if(this.y > FlyingSaucer.CRASH_POS_Y)
this.crash();
} else {
this.stop();
}
}
FlyingSaucer.prototype.updateJetColor = function() {
var landingRatio = this.getLandingRatio();
if(this.jetStatus == FlyingSaucer.JET_STATUS_HIGH && landingRatio < 0.9) {
this.jetStatus = FlyingSaucer.JET_STATUS_MIDDLE;
this.changeJetColor(FlyingSaucer.JET_COLOR_MIDDLE);
} else if(this.jetStatus == FlyingSaucer.JET_STATUS_MIDDLE && landingRatio < 0.1) {
this.jetStatus = FlyingSaucer.JET_STATUS_LOW;
this.changeJetColor(FlyingSaucer.JET_COLOR_LOW);
}
}
FlyingSaucer.prototype.changeJetColor = function(color) {
this.jetLeft.tint = color;
this.jetCenter.tint = color;
this.jetRight.tint = color;
}
FlyingSaucer.prototype.changeCockpitColor = function(color) {
this.cockpit.tint = FlyingSaucer.COKCPIT_COLOR[this.wordLevel - 1];
}
FlyingSaucer.prototype.setScoreManager = function(scoreManager) {
this.scoreManager = scoreManager;
}
FlyingSaucer.prototype.setHeartManager = function(heartManager) {
this.heartManager = heartManager;
}
FlyingSaucer.prototype.setWordManager = function(wordManager) {
this.wordManager = wordManager;
}
FlyingSaucer.prototype.setGameLevelManager = function(gameLevelManager) {
this.gameLevelManager = gameLevelManager;
}
FlyingSaucer.prototype.setWord = function(wordText) {
this.wordText.text = wordText;
}
FlyingSaucer.prototype.stop = function() {
this.body.velocity.setTo(0, 0);
}
FlyingSaucer.prototype.setActive = function(isActivated) {
this.isActivated = isActivated;
if(this.isActivated) {
this.alpha = 1;
} else {
this.alpha = 0;
}
}
FlyingSaucer.prototype.moveInitialPos = function() {
this.x = 100 + this.lineIndex * 200;
this.y = FlyingSaucer.START_POS_Y;
this.speed += FlyingSaucer.SPEED_UP_AMOUNT;
this.changeCockpitColor();
this.jetStatus = FlyingSaucer.JET_STATUS_HIGH;
this.changeJetColor(FlyingSaucer.JET_COLOR_HIGH);
this.wordText.x = this.x;
this.wordText.y = this.y + FlyingSaucer.WORD_TEXT_OFFSET_Y;
this.wordText.clearColors();
this.wordText.addColor(FlyingSaucer.TEXT_COLOR_NORMAL, 0);
}
FlyingSaucer.prototype.startAttack = function() {
this.wordLevel = this.gameLevelManager.getWordLevel();
this.word = this.wordManager.getWord(this.wordLevel);
this.setWord(this.word);
this.wordScore = this.gameLevelManager.getWordScore();
// console.log(this.wordScore);
this.moveInitialPos();
this.setActive(true);
}
FlyingSaucer.prototype.checkInputText = function(inputText) {
// console.log("inputText : " + inputText);
// console.log("this.wordText : " + this.wordText);
this.wordText.clearColors();
this.wordText.addColor(FlyingSaucer.TEXT_COLOR_NORMAL, 0);
if(inputText == null || inputText.length == 0)
return;
var word = this.wordText.text;
var inputTextLength = inputText.length;
var index = 0;
for(; index < inputTextLength; index++) {
// console.log("i : " + index);
// console.log("inputTextLength : " + inputTextLength);
// console.log("inputText.charAt(i) : " + inputText.charAt(index));
// console.log("this.wordText.text.charAt(i) : " + this.wordText.text.charAt(index));
if(index == 0 && word.charAt(index) == inputText.charAt(index)) {
this.wordText.addColor(FlyingSaucer.TEXT_COLOR_CORRECT, index);
} else if(word.charAt(index) != inputText.charAt(index)) {
this.wordText.addColor(FlyingSaucer.TEXT_COLOR_NORMAL, index);
return;
}
}
this.wordText.addColor(FlyingSaucer.TEXT_COLOR_NORMAL, index);
}
FlyingSaucer.prototype.getLandingRatio = function(inputText) {
var heightDown = FlyingSaucer.CRASH_POS_Y - FlyingSaucer.START_POS_Y;
var distanceDown = this.y - FlyingSaucer.START_POS_Y;
return (heightDown - distanceDown) / heightDown;
}
FlyingSaucer.prototype.checkWord = function(inputText) {
// console.log("inputText : " + inputText);
// console.log("this.wordText : " + this.wordText);
// var word = this.wordText.text;
if(this.word == inputText) {
this.gameLevelManager.clearWord(this.wordLevel, this.getLandingRatio());
this.wordManager.removeWord(this.wordLevel, this.word);
this.setWord("");
this.setActive(false);
this.explode();
this.plusScore();
return;
}
}
FlyingSaucer.prototype.explode = function(inputText) {
this.explodeEmitter.x = this.x;
this.explodeEmitter.y = this.y;
this.explodeEmitter.start(true, 1000, null, 100);
this.timerEvent = game.time.events.add(Phaser.Timer.SECOND, this.startAttack, this);
}
FlyingSaucer.prototype.plusScore = function(inputText) {
// console.log(this.wordScore);
// console.log(this.getLandingRatio());
var totalScore = Math.floor(this.wordScore * (this.getLandingRatio() * 100));
this.scoreManager.plusScore(totalScore);
var scoreText = new ScoreText(this.x, this.y, totalScore);
}
FlyingSaucer.prototype.crash = function() {
this.moveInitialPos();
this.heartManager.breakHearts(1);
}
FlyingSaucer.prototype.destroy = function() {
this.setActive(false);
this.setWord("");
}
FlyingSaucer.loadResource = function() {
if(!game.cache.checkImageKey('alien_normal'))
game.load.image('alien_normal', '../../../resources/image/character/alien/green_alien.png');
if(!game.cache.checkImageKey('ship_cockpit'))
game.load.image('ship_cockpit', '../../../resources/image/character/alien/cockpit.png');
if(!game.cache.checkImageKey('ship_body'))
game.load.image('ship_body', '../../../resources/image/character/alien/ship.png');
if(!game.cache.checkImageKey('ship_jet'))
game.load.image('ship_jet', '../../../resources/image/character/alien/jet.png');
}
FlyingSaucer.DEFAULT_SPEED = 10; // dot per sec
FlyingSaucer.SPEED_UP_AMOUNT = 4; // dot per sec
FlyingSaucer.SPEED_DOWN_AMOUNT = FlyingSaucer.SPEED_UP_AMOUNT / 2; // dot per sec
FlyingSaucer.START_POS_Y = 100; // px
FlyingSaucer.CRASH_POS_Y = GAME_SCREEN_SIZE.y - 200; // px
FlyingSaucer.WORD_TEXT_OFFSET_Y = 30; // px
FlyingSaucer.TEXT_COLOR_NORMAL = "#000000";
FlyingSaucer.TEXT_COLOR_CORRECT = "#ffff4d";
FlyingSaucer.JET_STATUS_HIGH = 0;
FlyingSaucer.JET_STATUS_MIDDLE = 1;
FlyingSaucer.JET_STATUS_LOW = 2;
FlyingSaucer.JET_COLOR_HIGH = 0x00ffff;
FlyingSaucer.JET_COLOR_MIDDLE = 0xffff00;
FlyingSaucer.JET_COLOR_LOW = 0xff0000;
FlyingSaucer.COKCPIT_COLOR = [
0xff0000, 0xff6600, 0xff0000, 0x00ffff, 0x00ffff, 0x6666ff, 0xff00ff
]