diff --git a/src/game/mouse/dodge/alien.js b/src/game/mouse/dodge/alien.js new file mode 100644 index 0000000..c97894b --- /dev/null +++ b/src/game/mouse/dodge/alien.js @@ -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; diff --git a/src/game/mouse/dodge/game.js b/src/game/mouse/dodge/game.js new file mode 100644 index 0000000..219be24 --- /dev/null +++ b/src/game/mouse/dodge/game.js @@ -0,0 +1,137 @@ +var Game = { + + create: function() { + this.game.stage.backgroundColor = "#000000"; // '#4d4d4d'; + + this.scoreManager = new ScoreManager(); + + sessionStorageManager.setIsNewAppHighestRecord(false); + + var experienceAppTimer = new ExperienceAppTimer(); + experienceAppTimer.setTimeOverListener( + (function() { this.gameOver(); }).bind(this) + ); + + // keyboard shortcut + this.keyboardShortcut = new KeyboardShortcut(); + this.keyboardShortcut.addCallback( + Phaser.KeyCode.ESC, // keyCode + null, // keyDownHandler + (function() { this.back(); }).bind(this), // keyUpHandler + "dodge" // callerClassName + ); + + // game stage + var stars = game.add.group(); + + for (var i = 0; i < 128; i++) + { + var randomY = game.rnd.integerInRange(60, GAME_SCREEN_SIZE.y - 80); + stars.create(game.world.randomX, randomY, 'star'); + } + + + + // contents + this.spaceship = new Spaceship(); + + this.aliens = []; + for(var i = 0; i < 50; i++) { + var alien = new Alien(this.spaceship); + this.aliens.push(alien); + } + + + // top ui + var screenTopUI = new ScreenTopUI(); + screenTopUI.makeBackButton( (function() { this.back(); }).bind(this) ); + screenTopUI.makeFullScreenButton(); + + var scoreBoard = new ScoreBoard(); + this.scoreManager.addOnChangeScoreListener( function(score) { + sessionStorageManager.setRecord(score); + scoreBoard.printScore(NumberUtil.numberWithCommas(score)); + }); + this.scoreManager.addOnChangeHighScoreListener( function(highScore) { + // console.log(highScore); + // sessionStorageManager.setAppHighestRecord(highScore); + sessionStorageManager.setIsNewBestRecrd(true); + // screenBottomUI.printLeftTextWithAppHighestRecord(sessionStorageManager.getAppHighestRecord()); + }); + + + // bottom ui + var screenBottomUI = new ScreenBottomUI(); + screenBottomUI.printLeftTextWithAppHighestRecord(sessionStorageManager.getAppHighestRecord()); + screenBottomUI.printCenterText(sessionStorageManager.getPlayingAppKoreanName()); + screenBottomUI.printRightText(sessionStorageManager.getPlayerName()); + + + this.startGame(); + // this.countDown(); + }, + + + back: function() { + sessionStorageManager.resetPlayingAppData(); + location.href = '../../web/client/menu_app.html'; + }, + + initListeners: function() { + }, + + /* + countDown() { + var style = { font: "bold 200px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" }; + this.countDownText = game.add.text(0, 0, "", style); + this.countDownText.setTextBounds(0, 0, game.world.width, game.world.height); + this.countDownText.stroke = "#333"; + this.countDownText.strokeThickness = 50; + + this.countDownNumber = 3; + if(isDebugMode()) + this.countDownNumber = 1; + this.tweenCountDown(); + } + + tweenCountDown() { + if(this.countDownNumber === 0) { + this.startGame(); + return; + } + + this.countDownText.text = this.countDownNumber.toString(); + this.countDownText.alpha = 1; + + var countDownTween = game.add.tween(this.countDownText); + countDownTween.to( { alpha: 0 }, 1000, Phaser.Easing.Linear.None, true); + countDownTween.onComplete.add(this.tweenCountDown, this); + + this.countDownNumber--; + } + */ + + startGame: function() { + }, + + gameOver: function() { + var gameOverText = new GameOverText(); + + game.time.events.add(GAME_OVER_WAIT_TIME_MS, this.goResult, this); + }, + + goResult: function() { + location.href = '../../web/client/result.html'; + }, + + getScore: function() { + return (this.alienTimer.activatedAlienIndex) * 2; + }, + + onAlienEscaped: function() { + this.heartManager.breakHearts(1); + } + +} + +Game.GAME_OVER_WAIT_TIME_MS = 2000; \ No newline at end of file diff --git a/src/game/mouse/dodge/loading.js b/src/game/mouse/dodge/loading.js new file mode 100644 index 0000000..ee6584d --- /dev/null +++ b/src/game/mouse/dodge/loading.js @@ -0,0 +1,94 @@ +var Loading = { + + preload: function() { + // game.load.image('loadingbar', './image/phaser.png'); + }, + + create: function() { + // var userID = sessionStorage.getItem("UserID"); + // console.log("userID : " + userID); + + // game.stage.backgroundColor = '#4d4d4d'; + + // // Progress report + // this.textProgress = game.add.text(game.world.centerX, 100, '로딩중 . . .', textStyleBasic); + // this.textProgress.anchor.setTo(0.5, 0.4); + // this.textProgress.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2); + + // // Preload bar + // this.preloadBar = this.add.sprite(game.world.centerX, game.world.centerY, 'loadingbar'); + // this.preloadBar.anchor.setTo(0.5); + // this.preloadBar.alpha = 0; + + game.load.onFileComplete.add(this.fileComplete, this); + game.load.onLoadComplete.add(this.loadComplete, this); + + this.startLoading(); + }, + + startLoading: function() { + game.load.image('icon_fullscreen', '../../../resources/image/icon/fullscreen_white.png'); + + game.load.image('star', '../../../resources/image/background/star_7x7.png'); + + game.load.image('alien_normal', '../../../resources/image/character/alien/green_alien.png'); + + game.load.image('heart_full', '../../../resources/image/ui/heart_full.png'); + game.load.image('heart_empty', '../../../resources/image/ui/heart_empty.png'); + // game.load.image('a', '../../../resources/image/icon/fullscreen_white.png'); + // game.load.image('b', '../../../resources/image/icon/fullscreen_white.png'); + // game.load.image('c', '../../../resources/image/icon/fullscreen_white.png'); + // game.load.image('d', '../../../resources/image/icon/fullscreen_white.png'); + // game.load.image('a', './image/phaser.png'); + // game.load.image('b', './image/phaser.png'); + // game.load.image('c', './image/phaser.png'); + // game.load.image('d', './image/phaser.png'); + // game.load.image('e', './image/phaser.png'); + // game.load.image('g', './image/phaser.png'); + // game.load.image('e', './image/phaser.png'); + // game.load.image('f', './image/phaser.png'); + // game.load.image('g', './image/phaser.png'); + // game.load.image('h', './image/phaser.png'); + + // game.load.image('phaser', './image/phaser.png'); + // game.load.spritesheet('button', './image/button_basic.png', 200, 100); + // game.load.image('star', './image/star_particle.png'); + // game.load.image('medal_gold', './image/medal_gold.png'); + // game.load.image('medal_silver', './image/medal_silver.png'); + // game.load.image('medal_bronze', './image/medal_bronze.png'); + + game.load.start(); + }, + + fileComplete: function(progress, cacheKey, success, totalLoaded, totalFiles) { + // this.preloadBar.alpha = progress / 100; + + // console.log('progress : ' + progress); + + // text.setText("File Complete: " + progress + "% - " + totalLoaded + " out of " + totalFiles); + + // var newImage = game.add.image(x, y, cacheKey); + // newImage.scale.set(0.3); + + // x += newImage.width + 20; + // if (x > 700) + // { + // x = 32; + // y += 332; + // } + }, + + loadComplete: function(progress, cacheKey, success, totalLoaded, totalFiles) { + // this.preloadBar.alpha = 1; + + if(isDebugMode()) { + this.state.start('Game'); + return; + } + + this.state.start('Game'); + // this.startMenu(); + // this.startTypingTestStage(); + // this.startTypingTestResult(); + } +} diff --git a/src/game/mouse/dodge/main.js b/src/game/mouse/dodge/main.js new file mode 100644 index 0000000..e4cbf44 --- /dev/null +++ b/src/game/mouse/dodge/main.js @@ -0,0 +1,16 @@ +///////////////////////////// +// Main game + +var CONTENT_ID = "Space Invaders"; + +var game = new Phaser.Game( + GAME_SCREEN_SIZE.x, GAME_SCREEN_SIZE.y, + Phaser.CANVAS, CONTENT_ID, + this, false, false +); + +game.state.add('Loading', Loading); +game.state.start('Loading'); + +game.state.add('Game', Game); +// game.state.add('Menu', Menu); diff --git a/src/game/mouse/dodge/spaceship.js b/src/game/mouse/dodge/spaceship.js new file mode 100644 index 0000000..8884cb7 --- /dev/null +++ b/src/game/mouse/dodge/spaceship.js @@ -0,0 +1,92 @@ +Spaceship.prototype = Object.create(Phaser.Sprite.prototype); +Spaceship.constructor = Spaceship; + +function Spaceship() { + this.isActivated = false; + this.isOnWaitingRoom = true; + this.speed = Spaceship.DEFAULT_SPEED; + + Phaser.Sprite.call(this, game, game.world.centerX, game.world.centerY, 'heart_full'); + game.add.existing(this); + this.scale.set(0.5); + this.anchor.set(0.5); + this.halfWidth = this.width / 2; + + game.physics.enable(this, Phaser.Physics.ARCADE); + + this.setActive(true); +} + + + +Spaceship.prototype.update = function() { + if(this.isActivated) { + game.physics.arcade.moveToPointer(this, this.speed); + + if(Phaser.Rectangle.contains(this.body, game.input.x, game.input.y)) { + this.stop(); + this.setDirection(Spaceship.DIR_MIDDLE); + return; + } + + if(this.x - this.halfWidth < game.input.x && game.input.x < this.x + this.halfWidth) { + this.setDirection(Spaceship.DIR_MIDDLE); + } else if(this.x > game.input.x) { + this.setDirection(Spaceship.DIR_LEFT); + } else { + this.setDirection(Spaceship.DIR_RIGHT); + } + } else { + this.stop(); + } +} + +Spaceship.prototype.setDirection = function(dir) { + switch(dir) { + case Spaceship.DIR_LEFT: + this.tint = 0x99ff99; + break; + + case Spaceship.DIR_MIDDLE: + this.tint = 0xffffff; + break; + + case Spaceship.DIR_RIGHT: + this.tint = 0x9999ff; + break; + } +} + +Spaceship.prototype.stop = function() { + this.body.velocity.setTo(0, 0); +} + +Spaceship.prototype.setActive = function(isActivated) { + this.isActivated = isActivated; +} + +Spaceship.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); + // } + +} + + + +Spaceship.DEFAULT_SPEED = 200; + +Spaceship.DIR_LEFT = 0; +Spaceship.DIR_MIDDLE = 1; +Spaceship.DIR_RIGHT = 2; diff --git a/src/web/client/dodge.html b/src/web/client/dodge.html new file mode 100644 index 0000000..f62a61e --- /dev/null +++ b/src/web/client/dodge.html @@ -0,0 +1,80 @@ + + +
+ + + + + + + + + + + +