Remove the menu camera system
This commit is contained in:
		
							
								
								
									
										46
									
								
								src/game.ts
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								src/game.ts
									
									
									
									
									
								
							| @@ -1,7 +1,6 @@ | ||||
| import { BG_OUTER } from "./colors.ts"; | ||||
| import { D, I } from "./engine/public.ts"; | ||||
| import { IGame, Point, Size } from "./engine/datatypes.ts"; | ||||
| import { getPageLocation, Page } from "./layout.ts"; | ||||
| import { getHotbar, Hotbar } from "./hotbar.ts"; | ||||
| import { getSkillsModal, SkillsModal } from "./skillsmodal.ts"; | ||||
| import { getVNModal, VNModal } from "./vnmodal.ts"; | ||||
| @@ -9,60 +8,17 @@ import { Gameplay, getGameplay } from "./gameplay.ts"; | ||||
| import { getEndgameModal } from "./endgamemodal.ts"; | ||||
| import { CheckModal, getCheckModal } from "./checkmodal.ts"; | ||||
|  | ||||
| class MenuCamera { | ||||
|   // measured in whole screens | ||||
|   position: Point; | ||||
|   target: Point; | ||||
|  | ||||
|   constructor({ position, target }: { position: Point; target: Point }) { | ||||
|     this.position = position; | ||||
|     this.target = target; | ||||
|   } | ||||
|  | ||||
|   update() { | ||||
|     let adjust = (x0: number, x1: number) => { | ||||
|       if (Math.abs(x1 - x0) < 0.01) { | ||||
|         return x1; | ||||
|       } | ||||
|       return (x0 * 8 + x1 * 2) / 10; | ||||
|     }; | ||||
|     this.position = new Point( | ||||
|       adjust(this.position.x, this.target.x), | ||||
|       adjust(this.position.y, this.target.y), | ||||
|     ); | ||||
|   } | ||||
| } | ||||
| export class Game implements IGame { | ||||
|   camera: MenuCamera; | ||||
|   page: Page; | ||||
|   #mainThing: Gameplay | VNModal | null; | ||||
|   #bottomThing: CheckModal | SkillsModal | Hotbar | null; | ||||
|  | ||||
|   constructor() { | ||||
|     this.camera = new MenuCamera({ | ||||
|       position: new Point(0, 0), | ||||
|       target: new Point(0, 0), | ||||
|     }); | ||||
|     this.page = "Gameplay"; | ||||
|  | ||||
|     this.#mainThing = null; | ||||
|     this.#bottomThing = null; | ||||
|   } | ||||
|  | ||||
|   update() { | ||||
|     if (I.isKeyPressed("w")) { | ||||
|       this.page = "Gameplay"; | ||||
|     } | ||||
|     if (I.isKeyPressed("s")) { | ||||
|       this.page = "Thralls"; | ||||
|     } | ||||
|  | ||||
|     this.camera.target = getPageLocation(this.page); | ||||
|     D.camera = new Point( | ||||
|       D.size.w * this.camera.position.x, | ||||
|       D.size.h * this.camera.position.y, | ||||
|     ); | ||||
|     this.camera.update(); | ||||
|     D.camera = new Point(0, 0); | ||||
|  | ||||
|     // state-specific updates | ||||
|     this.updateGameplay(); | ||||
|   | ||||
| @@ -54,7 +54,6 @@ export function withCamera(part: UIPart, cb: () => void) { | ||||
| } | ||||
|  | ||||
| // specific | ||||
| export type Page = "Gameplay" | "Thralls"; | ||||
| export type UIPart = | ||||
|   | "BottomModal" | ||||
|   | "FullscreenPopover" | ||||
| @@ -63,52 +62,8 @@ export type UIPart = | ||||
|   | "Gameplay" | ||||
|   | "Thralls"; | ||||
|  | ||||
| export function getPartPage(part: UIPart): Page | null { | ||||
|   switch (part) { | ||||
|     case "FullscreenPopover": | ||||
|       return null; | ||||
|     case "BottomModal": | ||||
|     case "Hotbar": | ||||
|     case "HUD": | ||||
|     case "Gameplay": | ||||
|       return "Gameplay"; | ||||
|     case "Thralls": | ||||
|       return "Thralls"; | ||||
|   } | ||||
|  | ||||
|   throw `invalid part: ${part}`; | ||||
| } | ||||
|  | ||||
| export function getPageLocation(page: Page): Point { | ||||
|   // NOTE: Measured in screens, not pixels | ||||
|   switch (page) { | ||||
|     case "Gameplay": | ||||
|       return new Point(0, 0); | ||||
|     case "Thralls": | ||||
|       return new Point(0, 1); | ||||
|   } | ||||
|  | ||||
|   throw `invalid page: ${page}`; | ||||
| } | ||||
|  | ||||
| export function getPartLocation(part: UIPart): Rect { | ||||
|   // TODO: in pixels, not screens | ||||
|   let { w: screenW, h: screenH } = D.size; | ||||
|   let page = getPartPage(part); | ||||
|   let pageOffset = page ? getPageLocation(page) : null; | ||||
|   let layoutRect = internalGetPartLayoutRect(part); | ||||
|  | ||||
|   if (pageOffset == null) { | ||||
|     // follow camera | ||||
|     return layoutRect.offset(D.camera); | ||||
|   } | ||||
|  | ||||
|   return layoutRect.offset( | ||||
|     new Point(pageOffset.x * screenW, pageOffset.y * screenH), | ||||
|   ); | ||||
| } | ||||
|  | ||||
| export function internalGetPartLayoutRect(part: UIPart) { | ||||
| export function getPartLocation(part: UIPart) { | ||||
|   switch (part) { | ||||
|     case "BottomModal": | ||||
|       return getLayoutRect(new Size(384, 128), { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user