Open CyberLord09 opened 7 months ago
Level within our group level
1. Level creation
Take the current alien level and duplicate it (change all game assets and add new background and elements)
Original code:
new GameLevel( {tag: "avenida", callback: this.playerOffScreenCallBack, objects: avenidaGameObjects } );
// Space Game Level definition...
const spaceGameObjects = [
// GameObject(s), the order is important to z-index...
{ name: 'space', id: 'background', class: Background, data: this.assets.backgrounds.space },
{ name: 'grass', id: 'platform', class: Platform, data: this.assets.platforms.grass },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.2, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.2368, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.5, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.5368, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.4, yPercentage: 1 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.4, yPercentage: 0.9 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.4, yPercentage: 0.8 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.4, yPercentage: 0.7 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.alien, xPercentage: 0.4, yPercentage: 0.6 },
{ name: 'alien', id: 'alien', class: Alien, data: this.assets.enemies.alien, xPercentage: 0.3, minPosition: 0.07 },
{ name: 'alien', id: 'alien', class: Alien, data: this.assets.enemies.alien, xPercentage: 0.5, minPosition: 0.3 },
{ name: 'alienSpecial', id: 'alien', class: Alien, data: this.assets.enemies.alien, xPercentage: 0.75, minPosition: 0.5 }, //this special name is used for random event 2 to make sure that only one of the Goombas ends the random event
{ name: 'flyingUFO', id: 'flyingUFO', class: FlyingUFO, data: this.assets.enemies.flyingUFO, xPercentage: 0.1, minPosition: 0.05},
{ name: 'flyingUFO', id: 'flyingUFO', class: FlyingUFO, data: this.assets.enemies.flyingUFO, xPercentage: 0.5, minPosition: 0.05},
{ name: 'monkey', id: 'player', class: Player, data: this.assets.players.monkey },
{ name: 'tree', id: 'tree', class: Tree, data: this.assets.obstacles.tree },
{ name: 'complete2', id: 'background', class: BackgroundTransitions, data: this.assets.backgrounds.complete2 },
];
Create new level by changing "const spaceGameObjects" to "const miniGameObjects"
new GameLevel( {tag: "avenida", callback: this.playerOffScreenCallBack, objects: avenidaGameObjects } );
// Space Game Level definition...
const miniGameObjects = [
Remove all game objects
new GameLevel( {tag: "avenida", callback: this.playerOffScreenCallBack, objects: avenidaGameObjects } );
// Space Game Level definition...
const miniGameObjects = [
// GameObject(s), the order is important to z-index...
];
Find background image and import it and name it then add to GameSetup.js
start: { src: "/images/platformer/backgrounds/home.png" },
hills: { src: "/images/platformer/backgrounds/hills.png" },
avenida: { src: "/images/platformer/backgrounds/avenidawide3.jpg" },
mountains: { src: "/images/platformer/backgrounds/mountains.jpg" },
clouds : { src: "/images/platformer/backgrounds/clouds.png"},
space: { src: "/images/platformer/backgrounds/planet.jpg" },
mini: { src: "/images/platformer/backgrounds/mini.png" },
castles: { src: "/images/platformer/backgrounds/castles.png" },
loading: { src: "/images/platformer/backgrounds/greenscreen.png" },
complete: { src: "/images/platformer/backgrounds/OneStar.png" },
complete2: { src: "/images/platformer/backgrounds/TwoStar.png" },
end: { src: "/images/platformer/backgrounds/Congratulations!!!.png" }
mini: { src: "/images/platformer/backgrounds/mini.png" },
Create and add new game objects to the new mini level in GameSetup.js Add the new background created above
const miniGameObjects = [
// GameObject(s), the order is important to z-index...
{ name: 'mini', id: 'background', class: Background, data: this.assets.backgrounds.mini },
{ name: 'rock', id: 'platform', class: Platform, data: this.assets.platforms.rock },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.59, yPercentage: 0.35 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.6268, yPercentage: 0.35 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.3, yPercentage: 0.35 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.3368, yPercentage: 0.35 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.3, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.3368, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.4684, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.6, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.6368, yPercentage: 0.85 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.3736, yPercentage: 0.35 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.3736, yPercentage: 0.4334 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.3736, yPercentage: 0.5167 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.3736, yPercentage: 0.6 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.4104, yPercentage: 0.6 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.4472, yPercentage: 0.6 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.484, yPercentage: 0.6 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.5208, yPercentage: 0.6 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.5576, yPercentage: 0.6 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.5576, yPercentage: 0.5167 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.5576, yPercentage: 0.4334 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.5576, yPercentage: 0.35 },
{ name: 'blocks', id: 'jumpPlatform', class: BlockPlatform, data: this.assets.platforms.lava, xPercentage: 0.5576, yPercentage: 1.05 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.29, yPercentage: 0.75 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.33, yPercentage: 0.75 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.4584, yPercentage: 0.75 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.37, yPercentage: 0.5 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.41, yPercentage: 0.5 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.45, yPercentage: 0.5 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.49, yPercentage: 0.5 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.53, yPercentage: 0.5 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.57, yPercentage: 0.5 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.59, yPercentage: 0.75 },
{ name: 'coin', id: 'coin', class: Coin, data: this.assets.obstacles.coin, xPercentage: 0.63, yPercentage: 0.75 },
{ name: 'mario', id: 'player', class: PlayerHills, data: this.assets.players.mario },
{ name: 'tube', id: 'tube', class: Tube, data: this.assets.obstacles.tube },
{ name: 'complete', id: 'background', class: BackgroundTransitions, data: this.assets.backgrounds.complete },
];
// Space Game Level added to the GameEnv ...
new GameLevel( {tag: "mini", callback: this.playerOffScreenCallBack, objects: miniGameObjects} );
Implementing Tubes in Mini Level
We created two tubes in the mini-level, where one of them is positioned at the top left corner of the screen:
When the mini level loads, the player is supposed to spawn from inside the tube and drop downwards. (This action is supposed to happen after the player jumps through the tube on the main level). I created the tube image by turning the tube upside down and into a blue color: Then, I made a Tube1.js file that is extended from Tube.js and made some adjustments to positioning the image, like changing the tubeX and tubeY values. I also changed the values inside ctx.drawImage so that the entire image is drawn:
To make the player spawn in the same x as the tube, I added this.setX() and made it set to tubeX:
To change the y-position of the player, add this.hillsStart to the constructor of PlayerMini.js
Create an update loop so that the player will spawn at the top of the screen upon loading into the level
Transitioning Between Levels
To have the player transition from the mini level to the Greece level through the tube, the player needed to transition to the previous level. I did this in PlayerMini.js:
The current system for loading the next level is teleporting the player off of the map with this.x = gameenv.width + 1, which I commented out. Instead, I used transitionToLevel function to transition to a level I wanted by changing the number in GameEnv.levels[] to match the level number. This allows the player to transition from one level to any level I wanted.
I also did this for the flag in Greece level to have it transition the players two levels ahead (from level 3 to 5):