From a9e90a7b708e52a8875530eb00313146b43a9926 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 23 Aug 2014 19:18:36 +0200 Subject: Added wrapping of game world --- .editorconfig | 2 +- assets/levels/maze.lvl | 2 +- game/prefabs/player.js | 15 +++++++++++++++ game/states/play.js | 43 ++++++++++++++++++++++++++++++++++++++++--- todo.md | 3 --- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/.editorconfig b/.editorconfig index d1d8a41..4b4fd7c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,7 +1,7 @@ # http://editorconfig.org root = true -[*] +[game/*] indent_style = space indent_size = 2 end_of_line = lf diff --git a/assets/levels/maze.lvl b/assets/levels/maze.lvl index 4d59c43..e2d2a3e 100644 --- a/assets/levels/maze.lvl +++ b/assets/levels/maze.lvl @@ -8,7 +8,7 @@ ####.###.#.###.#### ####.#.......#.#### ####.#.## ##.#.#### -# B...# #...A # + B...# #...A ####.#.## ##.#.#### ####.#.......#.#### ####.#.#####.#.#### diff --git a/game/prefabs/player.js b/game/prefabs/player.js index e33b109..a4d1b57 100644 --- a/game/prefabs/player.js +++ b/game/prefabs/player.js @@ -46,12 +46,27 @@ Player.prototype.update = function() { Player.prototype.move = function(newX, newY, callback, callbackContext) { this.moving = true; + var tween = this.game.add.tween(this).to({x: newX, y: newY}, 500); tween.onComplete.add(callback, callbackContext); tween.onComplete.add(this.finishMovement, this); + tween.start(); }; +Player.prototype.multistepMove = function(moveX, moveY, teleportX, teleportY, finalX, finalY, callback, callbackContext) { + this.moving = true; + + var firstTween = this.game.add.tween(this).to({x: moveX, y: moveY}, 500); + + firstTween.onComplete.add(function() { + this.teleport(teleportX, teleportY); + this.move(finalX, finalY, callback, callbackContext); + }, this); + + firstTween.start(); +} + Player.prototype.teleport = function(newX, newY) { this.x = newX; this.y = newY; diff --git a/game/states/play.js b/game/states/play.js index 882882c..b79ea32 100644 --- a/game/states/play.js +++ b/game/states/play.js @@ -177,8 +177,16 @@ Play.prototype = { player.maxScore = maxScore; }, this); + this.gameWidth = 0; + this.gameHeight = 0; this.walls.forEach(function(wall) { this.addToMap(wall.x, wall.y); + if (wall.x >= this.gameWidth) { + this.gameWidth = wall.x+1; + } + if (wall.y >= this.gameHeight) { + this.gameHeight = wall.y+1; + } }, this); this.updatePlayerTurn(0); }, @@ -226,9 +234,38 @@ Play.prototype = { var newX = player.x + deltaX; var newY = player.y + deltaY; - if (!this.checkMap(newX, newY) && player.isMyTurn && !player.moving) { + if (this.checkMap(newX, newY) || !player.isMyTurn || player.moving) { + return; + } + + var intermediateX = newX; + var teleportX = newX; + var intermediateY = newY; + var teleportY = newY; + + if (newX >= this.gameWidth) { + newX = 0; + teleportX = -1; + } + if (newX < 0) { + newX = this.gameWidth-1; + teleportX = this.gameWidth; + } + if (newY >= this.gameHeight) { + newY = 0; + teleportY = -1; + } + if (newY < 0) { + newY = this.gameHeight-1; + teleportY = this.gameHeight; + } + + if (newX === intermediateX && newY === intermediateY) { player.move(newX, newY, this.togglePlayerTurn, this); } + else { + player.multistepMove(intermediateX, intermediateY, teleportX, teleportY, newX, newY, this.togglePlayerTurn, this); + } }, playerPillCollision: function(player, pill) { player.score += pill.score; @@ -241,8 +278,8 @@ Play.prototype = { playerPlayerCollision: function(playerA, playerB) { var eatenPlayer = playerA.isMyTurn ? playerB : playerA; - var respawnX = Math.ceil(this.map.length/2)-1; - var respawnY = Math.ceil(this.map[0].length/2)-1; + var respawnX = Math.ceil(this.gameWidth/2)-1; + var respawnY = Math.ceil(this.gameHeight/2)-1; eatenPlayer.teleport(respawnX, respawnY); }, diff --git a/todo.md b/todo.md index a5d80a1..ded59ab 100644 --- a/todo.md +++ b/todo.md @@ -1,7 +1,4 @@ -* Make levels wrap horizontally and vertically -* Add player death (respawn at center) * Add poison pills -* Add players eating other players * Add restrictions on movement into the center * Add tutorial / instructions page * Refine orientation controls -- cgit v1.2.3