1 decade ago by AndreasElia
Hello,
Yesterday I spent many hours trying to figure out a way to do random map generation. I got the game to show random tiles every time the game was loaded but all of the tiles were collisions so the player couldn't actually move around on them.
I also couldn't think of any way to interact with any of the tiles such as remove an individual tile. The code provided now doesn't work or show anything on screen. I have no idea what happened but it just stopped working.
I would appreciate any help you can give, thanks!
Here is the code:
Yesterday I spent many hours trying to figure out a way to do random map generation. I got the game to show random tiles every time the game was loaded but all of the tiles were collisions so the player couldn't actually move around on them.
I also couldn't think of any way to interact with any of the tiles such as remove an individual tile. The code provided now doesn't work or show anything on screen. I have no idea what happened but it just stopped working.
I would appreciate any help you can give, thanks!
Here is the code:
ig.module( 'game.main' ) .requires( 'impact.game', 'impact.collision-map', 'impact.background-map', 'impact.debug.debug', 'game.entities.player', 'impact.font' ) .defines(function(){ MyGame = ig.Game.extend({ font: new ig.Font( 'media/04b03.font.png' ), tiles: new ig.Image( 'media/tileset.png' ), map: [], mapSize: 64, init: function() { for( var y = 0; y < this.mapSize; y++ ) { for( var x = 0; x < this.mapSize; x++ ) { this.map[y] = this.getRow(); this.map[x] = this.getColumn(); } } this.collisionMap = new ig.CollisionMap( this.mapSize, this.map ); var bgmap = new ig.BackgroundMap( this.mapSize, this.map, this.tiles ); this.backgroundMaps.push( bgmap ); console.log(this.map); ig.input.bind( ig.KEY.W, 'up' ); ig.input.bind( ig.KEY.S, 'down' ); ig.input.bind( ig.KEY.A, 'left' ); ig.input.bind( ig.KEY.D, 'right' ); ig.game.spawnEntity( EntityPlayer, 0, 0 ); }, getRow: function() { var row = []; for( var x = 0; x < this.mapSize; x++ ) { // row[x] = Math.random() > 0.93 ? 1 : 0; row[x] = Math.ceil(Math.random() * 5); } return row; }, getColumn: function() { var column = []; for( var y = 0; y < this.mapSize; y++ ) { // column[y] = Math.random() > 0.93 ? 1 : 0; column[y] = Math.ceil(Math.random() * 5); } return column; }, update: function() { // Update all entities and backgroundMaps this.parent(); var player = ig.game.getEntitiesByType( EntityPlayer )[0]; var newCamX = player.pos.x - ig.system.width/2; var newCamY = player.pos.y - ig.system.height/2; // Calculate the level width and height in pixels, subtract screen size var maxX = this.collisionMap.width * this.collisionMap.tilesize - ig.system.width; var maxY = this.collisionMap.height * this.collisionMap.tilesize - ig.system.height; // Set new screen coordinates this.screen.x = newCamX.limit( 0, maxX ); this.screen.y = newCamY.limit( 0, maxY ); if (player.pos.y > this.screen.y + ig.system.height - this.collisionMap.tilesize) { player.pos.y = this.screen.y + ig.system.height - this.collisionMap.tilesize; } if (player.pos.y < this.screen.y) { player.pos.y = this.screen.y; } if (player.pos.x > this.screen.x + ig.system.width - this.collisionMap.tilesize) { player.pos.x = this.screen.x + ig.system.width - this.collisionMap.tilesize; } if (player.pos.x < this.screen.x) { player.pos.x = this.screen.x; } // Add your own, additional update code here }, draw: function() { // Draw all entities and backgroundMaps this.parent(); } }); // Start the Game with 60fps, a resolution of 320x240, scaled // up by a factor of 2 ig.main( '#canvas', MyGame, 60, 320, 240, 2 ); });