Greetings!
I am currently working on my ‘WorldScene’ class for my game. A lot of this code was smashed into place from tutorials and other places, so I am having a little trouble working with it. Could I get some feedback on how I am doing this and perhaps some suggestions on better ways to accomplish the same thing? My biggest concern right now is modularity between scenes and objects I plan on introducing to the scenes. If anyone has suggestions on how I might accomplish that I would greatly appreciate it!
Code below for critique:
/**********************************
* Scene: WorldScene
* Description:
*/
var WorldScene = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function WorldScene ()
{
Phaser.Scene.call(this, { key: 'WorldScene' });
},
preload: function ()
{
},
create: function ()
{
//set tilemap reference key
var map = this.make.tilemap({ key: 'map' });
//set spritesheet reference key
var tiles = map.addTilesetImage('spritesheet', 'tiles');
//create our layers
var grass = map.createStaticLayer('Grass', tiles, 0, 0);
var obstacles = map.createStaticLayer('Obstacles', tiles, 0, 0);
var interactives = map.createStaticLayer('Interactive', tiles, 0, 0);
//set the 'obstacles' layer to have collision
obstacles.setCollisionByExclusion([-1]);
interactives.setCollisionByExclusion([-1]);
//setup physics bounds
this.physics.world.bounds.width = map.widthInPixels;
this.physics.world.bounds.height = map.heightInPixels;
this.newPlayer(100, 100);
this.player.canMove = true;
this.physics.add.collider(this.player, obstacles);
this.newBot(150,125);
this.bot.canMove = true;
this.bot.leftTimer = 100;
this.bot.rightTimer = 100;
this.bot.upTimer = 100;
this.bot.downTimer = 100;
//////////////////////////
//START EXPERIMENTAL AREA
//////////////////////////
this.physics.add.collider(this.player, this.bot, function ()
{
//WE HAVE DETECTED COLLISION BETWEEN this.player AND interactives!
//this.player.canMove = false;
//writeHistory('Touched an interactive!');
//writeHistory("Hi! Im sam!");
//console.log("Overlapping!");
}, null, this);
////////////////////////
//END EXPERIMENTAL AREA
////////////////////////
//setup camera boundaries to map size
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
//setup camera to follow player
this.cameras.main.startFollow(this.player);
//???
this.cameras.main.roundPixels = true;
this.playerAnimationContainer ();
this.botAnimationContainer ();
//setup cursors object for player movement
this.cursors = this.input.keyboard.createCursorKeys();
},
update: function (time, delta)
{
this.botPatrolContainer ();
this.playerMovementContainer ();
},
newBot: function (x, y)
{
this.bot = this.physics.add.sprite(x, y, 'player', 9); //bot--remove when done
this.bot.setCollideWorldBounds(true);
},
newPlayer: function (x, y)
{
//setup 'player' object with physics
this.player = this.physics.add.sprite(x, y, 'player', 6);
//setup 'player' movement boundaries (collision detection with edge of screen)
this.player.setCollideWorldBounds(true);
//setup collision detection between 'obstacles' layer and the 'player' object
},
/**************************************************
* Method: playerMovementContainer
* Description: Contains all player related movement
* and runs appropriate animations
*/
playerMovementContainer: function ()
{
this.player.body.setVelocity(0);
// update player horizontal location on map/screen based on input
if (this.player.canMove == true)
{
if (this.cursors.left.isDown)
{
this.player.body.setVelocityX(-80);
this.player.anims.play('left', true);
this.player.flipX = true; // flip animations to look like the player is moving left
}
else if (this.cursors.right.isDown)
{
this.player.body.setVelocityX(80);
this.player.anims.play('right', true);
// if the player object is already flipped, we flip it back
if (this.player.flipX == true) {
this.player.flipX = false;
}
}
// update player veritical location on map/screen based on input
if (this.cursors.up.isDown)
{
this.player.body.setVelocityY(-80);
this.player.anims.play('up', true);
}
else if (this.cursors.down.isDown)
{
this.player.body.setVelocityY(80);
this.player.anims.play('down', true);
}
}
if ((!this.cursors.down.isDown && !this.cursors.up.isDown && !this.cursors.right.isDown && !this.cursors.left.isDown))
{
this.player.anims.stop();
}
},
/**************************************************
* Method: playerAnimationContiner
* Description: Contains all animations relating to
* the player
*/
playerAnimationContainer: function ()
{
// animation with key 'left', we don't need left and right as we will use one and flip the sprite
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('player', { frames: [1, 7, 1, 13]}),
frameRate: 10,
repeat: -1
});
// animation with key 'right'
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('player', { frames: [1, 7, 1, 13] }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'up',
frames: this.anims.generateFrameNumbers('player', { frames: [2, 8, 2, 14]}),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'down',
frames: this.anims.generateFrameNumbers('player', { frames: [ 0, 6, 0, 12 ] }),
frameRate: 10,
repeat: -1
});
},
/**************************************************
* Method: botAnimationContiner
* Description: Contains all animations relating to
* a bot
*/
botAnimationContainer: function ()
{
// animation with key 'left', we don't need left and right as we will use one and flip the sprite
this.anims.create({
key: 'botLeft',
frames: this.anims.generateFrameNumbers('player', { frames: [4, 10, 4, 16]}),
frameRate: 6,
repeat: -1
});
// animation with key 'right'
this.anims.create({
key: 'botRight',
frames: this.anims.generateFrameNumbers('player', { frames: [4, 10, 4, 16] }),
frameRate: 6,
repeat: -1
});
this.anims.create({
key: 'botUp',
frames: this.anims.generateFrameNumbers('player', { frames: [5, 11, 5, 17]}),
frameRate: 6,
repeat: -1
});
this.anims.create({
key: 'botDown',
frames: this.anims.generateFrameNumbers('player', { frames: [3, 9, 3, 15] }),
frameRate: 6,
repeat: -1
});
},
/**************************************************
* Method: botMovementManager
* Description: Manages all bot related movement
* and runs appropriate animations
*/
botMovementManager: function (botDirection, botCanMove)
{
if (botCanMove == false)
{
this.bot.body.setVelocityX(0);
this.bot.body.setVelocityY(0);
}else{
switch(botDirection) {
case 'botLeft':
this.bot.body.setVelocityX(-25);
this.bot.flipX = true;
break;
case 'botRight':
this.bot.body.setVelocityX(25);
if (this.bot.flipX == true) {
this.bot.flipX = false;
}
break;
case 'botUp':
this.bot.body.setVelocityY(-25);
break;
case 'botDown':
this.bot.body.setVelocityY(25);
break;
}
this.bot.anims.play(botDirection, true);
}
},
/**************************************************
* Method: botPatrolContainer
* Description: Contains directions for bot patrol
* path.
*/
botPatrolContainer: function ()
{
/*****************************************
* Predetermined path of movement for bot
*/
if (this.bot.leftTimer > 0)
{
this.bot.leftTimer--;
this.botMovementManager ('botLeft', true);
}else if (this.bot.leftTimer == 0 && this.bot.upTimer > 0)
{
this.bot.upTimer--;
this.botMovementManager ('botLeft', false);
this.botMovementManager ('botUp', true);
}else if (this.bot.upTimer == 0 && this.bot.rightTimer > 0)
{
this.bot.rightTimer--;
this.botMovementManager ('botUp', false);
this.botMovementManager ('botRight', true);
}else if (this.bot.rightTimer == 0 && this.bot.downTimer > 0)
{
this.bot.downTimer--;
this.botMovementManager ('botRight', false);
this.botMovementManager ('botDown', true);
}else
{
this.botMovementManager ('botDown', false);
this.bot.leftTimer = 100;
this.bot.upTimer = 100;
this.bot.rightTimer = 100;
this.bot.downTimer = 100;
}
}
});