import { Juego } from "./main"; export type Pos = { x: number; y: number; }; export type Box = Pos & { width: number; height: number; }; export function posInBox(box: Box, pos: Pos) { return ( pos.x > box.x && pos.x < box.x + box.width && pos.y > box.y && pos.y < box.y + box.height ); } export function boxCollision(box1: Box, box2: Box) { // http://stackoverflow.com/questions/2440377/ddg#7301852 return !( box1.y + box1.height < box2.y || box1.y > box2.y + box2.height || box1.x + box1.width < box2.x || box1.x > box2.x + box2.width ); } export function drawText( juego: Juego, text: string, pos: Pos, { bold = false, size = 2, align = "left", baseline = "top", maxWidth, }: { align?: CanvasTextAlign; baseline?: CanvasTextBaseline; bold?: boolean; // in rem size?: number; maxWidth?: number; } ): Box { juego.ctx.font = `${bold ? "bold " : ""}${size * 12}px sans-serif`; juego.ctx.textAlign = align; juego.ctx.textBaseline = baseline; juego.ctx.fillText(text, pos.x, pos.y, maxWidth); const measure = juego.ctx.measureText(text); return { ...pos, width: measure.width, height: measure.actualBoundingBoxDescent, }; } export function randomFromArray(array: T[]): T { return array[Math.floor(Math.random() * array.length)]; } export function isMobile(juego: Juego): boolean { return !!juego.touches; } export function isTouching(juego: Juego, box: Box): boolean { return !!( juego.touches && Array.from(juego.touches).some((t) => posInBox(box, { x: t.pageX, y: t.pageY }) ) ); } export async function share() { const text = "¿Sabías que Larreta quiere vender la Costanera para construir un mega emprendimiento inmobiliario de lujo? Estamos juntando firmas para frenarlo, ya vamos 20 mil y nos faltan 20 mil más. ¿Te animas a jugar a este videojuego para salvar la costanera?"; try { await navigator.share({ title: "Salvá la Costanera", url: location.href, text, }); } catch (error) { location.href = `https://api.whatsapp.com/send?text=${encodeURIComponent( text + " " + location.href )}`; } }