Add: dodge app

This commit is contained in:
2018-12-02 14:34:29 +09:00
parent cf63482b3b
commit 0f799183ee
6 changed files with 528 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
Alien.prototype = Object.create(Phaser.Sprite.prototype);
Alien.constructor = Alien;
function Alien(spaceship) {
this.spaceship = spaceship;
this.isActivated = false;
this.speed = Alien.DEFAULT_SPEED_DOT_PER_SEC;
this.angleFromSpaceship = 0;
this.angleFromAlien = 0;
Phaser.Sprite.call(this, game, 0, 0, 'alien_normal');
this.anchor.set(0.5);
game.add.existing(this);
this.moveRandomPosition();
this.setActive(true);
}
Alien.prototype.moveRandomPosition = function() {
this.angleFromAlien = game.rnd.integerInRange(0, 360);
// console.log(this.angleFromAlien);
var radians = this.angleFromAlien * Math.PI / 180;
var radious = Alien.START_RADIOUS_PX * ( ( (100 + game.rnd.integerInRange(0, Alien.START_RANDOM_RADIOUS_PERCENT) ) / 100) );
var posX = Math.sin(radians) * radious;
var posY = Math.cos(radians) * radious;
this.x = this.spaceship.x + posX;
this.y = this.spaceship.y + posY;
this.angle = this.getSpriteAngle(this.angleFromAlien);
}
Alien.prototype.update = function() {
if(!this.isActivated)
return;
var deltaTime = game.time.elapsed / 1000;
var radians = this.angleFromAlien * Math.PI / 180;
var moveX = Math.sin(radians) * this.speed * deltaTime;
var moveY = Math.cos(radians) * this.speed * deltaTime;
this.x -= moveX;
this.y -= moveY;
var distanceX = this.x - game.world.centerX;
var distanceY = this.y - game.world.centerY;
var distanceFromCenter = Math.sqrt(Math.abs(distanceX * distanceX) + Math.abs(distanceY * distanceY));
if(distanceFromCenter > Alien.START_RADIOUS_PX)
this.moveRandomPosition();
}
Alien.prototype.stop = function() {
this.setActive(false);
}
Alien.prototype.setActive = function(isActivated) {
this.isActivated = isActivated;
}
Alien.prototype.getSpriteAngle = function (angle){
return 360 - angle;
}
Alien.prototype.getAngle = function (x1, y1, x2, y2){
var dx = x2 - x1;
var dy = y2 - y1;
var rad= Math.atan2(dx, dy);
var degree = (rad * 180) / Math.PI ;
if(degree < 0)
degree += 360;
return degree;
}
Alien.prototype.goRandomPosition = function() {
// set random position
var SPAWN_BOX_LEFT = 80;
var SPAWN_BOX_WIDTH = game.world.width - 160;
var SPAWN_BOX_TOP = 280;
var SPAWN_BOX_HEIGHT = game.world.height - 400;
this.x = SPAWN_BOX_LEFT + Math.random() * SPAWN_BOX_WIDTH;
this.y = SPAWN_BOX_TOP + Math.random() * SPAWN_BOX_HEIGHT;
// if(isDebugMode()) {
// this.game.add.graphics()
// .beginFill(0xffffff, 0.1)
// .drawRect(SPAWN_BOX_LEFT, SPAWN_BOX_TOP, SPAWN_BOX_WIDTH, SPAWN_BOX_HEIGHT);
// }
}
Alien.DEFAULT_SPEED_DOT_PER_SEC = 100;
Alien.START_RADIOUS_PX = 600;
Alien.START_RANDOM_RADIOUS_PERCENT = 40;