Clean Up
Before we move onto the pipe manager, we're going to delete any code we added related to testing our pipes.
We're going to need to delete several lines of code from main.ts
:
1import spriteSheetUrl from "#/assets/image/spritesheet.png";2import { BoxCollider } from "#/components/box-collider";3import { CircleCollider } from "#/components/circle-collider";4import { SpriteAnimation } from "#/components/sprite-animation";5import { SpriteAnimationDetails } from "#/components/sprite-animation-details";6import { SpriteData } from "#/components/sprite-data";7import { Vector2d } from "#/components/vector2d";8import { config } from "#/config";9import { Bird } from "#/entities/bird";10import { Ground } from "#/entities/ground";-import { Pipe } from "#/entities/pipe";12import { Game, GameState } from "#/game";13import { loadImage } from "#/lib/asset-loader";14import { circleRectangleIntersects } from "#/lib/collision";15import { spriteMap } from "#/sprite-map";1617const spriteSheet = await loadImage(spriteSheetUrl);18const game = new Game();1920const canvas = document.querySelector("canvas");21if (canvas == null) {22throw new Error("Could not find canvas element");23}2425canvas.width = config.gameWidth;26canvas.height = config.gameHeight;2728const context = canvas.getContext("2d");2930if (context == null) {31throw new Error("Could not obtain 2d context");32}3334context.imageSmoothingEnabled = false;3536// Add event listener to trigger bird flap37canvas.addEventListener("click", () => {38switch (game.state) {39case GameState.Title: {40game.state = GameState.Playing;41bird.flap();4243break;44}4546case GameState.Playing: {47bird.flap();4849break;50}5152case GameState.GameOver: {53game.reset();54bird.reset();55ground.start();5657break;58}59}60});6162window.addEventListener("keypress", (event) => {63if (event.code === "KeyD") {64game.debug = !game.debug;65}66});6768const ground = new Ground({69boxCollider: new BoxCollider(700,710,72spriteMap.ground.width,73spriteMap.ground.height74),75game,76position: new Vector2d(0, config.gameHeight - spriteMap.ground.height),77spriteData: spriteMap.ground,78spriteSheet: spriteSheet,79scrollSpeed: 120,80});8182const bird = new Bird({83game,84spriteSheet: spriteSheet,85position: new Vector2d(config.gameWidth / 4, config.gameHeight / 2),86spriteData: new SpriteData(87spriteMap.bird.idle.sourceX,88spriteMap.bird.idle.sourceY,89spriteMap.bird.idle.width,90spriteMap.bird.idle.height91),92flapAnimation: new SpriteAnimation(93new SpriteAnimationDetails(94spriteMap.bird.animations.flap.sourceX,95spriteMap.bird.animations.flap.sourceY,96spriteMap.bird.animations.flap.width,97spriteMap.bird.animations.flap.height,98spriteMap.bird.animations.flap.frameWidth,99spriteMap.bird.animations.flap.frameHeight100),1010.3102),103circlCollider: new CircleCollider(0, 0, 12),104});105-const pipeTop = new Pipe({-game,-position: new Vector2d(-config.gameWidth / 2 - spriteMap.pipes.green.slice.width / 2,-config.gameHeight / 2 - 50-),-rimSpriteData: spriteMap.pipes.green.bottom,-sliceSpriteData: spriteMap.pipes.green.slice,-spriteSheet,-type: "top",-});--const pipeBottom = new Pipe({-game,-position: new Vector2d(-config.gameWidth / 2 - spriteMap.pipes.green.slice.width / 2,-config.gameHeight / 2 + 50-),-rimSpriteData: spriteMap.pipes.green.top,-sliceSpriteData: spriteMap.pipes.green.slice,-spriteSheet,-type: "bottom",-});129130let last = performance.now();131132/**133* The game loop.134*/135const frame = (hrt: DOMHighResTimeStamp) => {136const dt = Math.min(1000, hrt - last) / 1000;137138context.clearRect(0, 0, canvas.width, canvas.height);139140ground.update(dt);141bird.update(dt);142143const didBirdHitGround = circleRectangleIntersects(144bird.position.x + bird.circleCollider.offsetX,145bird.position.y + bird.circleCollider.offsetY,146bird.circleCollider.radius,147ground.position.x + ground.boxCollider.offsetX,148ground.position.y + ground.boxCollider.offsetY,149ground.boxCollider.width,150ground.boxCollider.height151);152153if (didBirdHitGround) {154bird.die();155ground.stop();156game.state = GameState.GameOver;157}158159// Draw the background160context.drawImage(161spriteSheet,162spriteMap.background.sourceX,163spriteMap.background.sourceY,164spriteMap.background.width,165spriteMap.background.height,1660,1670,168spriteMap.background.width,169spriteMap.background.height170);171172bird.draw(context);-pipeTop.draw(context);-pipeBottom.draw(context);175ground.draw(context);176-const x = config.gameWidth / 2 - spriteMap.pipes.green.slice.width / 2;-const y = config.gameHeight / 2;-const buffer = 50;-const lineLength = 65;--// Where the bottom of the top pipe will be-context.fillStyle = "red";-context.fillRect(x, y - buffer, lineLength, 1);-// Center of the canvas (our spawn point)-context.fillStyle = "purple";-context.fillRect(x, y, lineLength, 1);-// Where the top of the bottom pipe will be-context.fillStyle = "red";-context.fillRect(x, y + buffer, lineLength, 1);191192last = hrt;193194requestAnimationFrame(frame);195};196197// Start the game loop.198requestAnimationFrame(frame);
Now that we're in a nice clean state again, let's move onto the pipe manager!