[Day17] Vite 出小蜜蜂~ 介面 (HUD)!

Day17

开始做 介面 (HUD)
接下来都是用之前有实作过的技巧!

Render

因为要调整 Text 的位置,我们需要调整一下 render

function Text(instance: Renderer & Transform) {
  if (instance.renderer.type !== "text") return;

  const src = instance.renderer.src;

+ const text = new _Text(src, {
    fontFamily: "VT323",
    fontSize: 16,
    fill: 0xffffff,
  });

+ const { x, y } = instance.position;
+ text.position.set(x, y);

+ return text;
}

export function render(stage: Container, instance: GameObject & Renderer) {
  let child: DisplayObject | undefined;

  if (instance.renderer.type === "graphics" && canTransform(instance)) {
    child = Graphics(graphics, instance);
  }

+ if (instance.renderer.type === "text" && canTransform(instance)) {
    child = Text(instance);
  }

  child && stage.addChild(child);
}

Base Function

透过定义 Base Function 根据 props 的不同产生不同 Text

type IText = GameObject & Renderer & Transform;

type TextProps = {
  src: string;
  position: Vector;
};
function Text({ src, position }: TextProps): IText {
  return {
    renderer: {
      type: "text",
      src,
    },
    position,
  };
}

Score Component

透过 Array 下去做 Group

type ScoreProps = {
  title: string;
  value?: number;
  x: number;
};
function Score({ title, value, x }: ScoreProps): IText[] {
  return [
    Text({ src: title, position: { x, y: 0 } }),
    Text({
      src: value ? String(value).padStart(4, "0") : "",
      position: { x: x + 12, y: 12 },
    }),
  ];
}

Mount

组合这些画面元件。

export default function GameHUD(): IText[] {
  return [
    ...Score({ title: "SCORE<1>", value: 70, x: 10 }),
    ...Score({ title: "HI-SCORE", value: 880, x: 90 }),
    ...Score({ title: "SCORE<2>", x: 160 }),
  ];
}

调整一下就完成了。

-- src/scenes/Game.ts

export default function Game(screen: Rectangle): Scene<Container> {
  let instances: GameObject[] = [
    LaserCannon(screen),
    ...spawn(Enemy, points),
+   ...GameHUD(),
  ];

  const update = ap(
    SequentialMovement({
      counts: instances.filter(isEnemy).length,
      step: 2,
    }),
    RandomlyShoot({
      row: ROW_WIDTH,
      rate: 1000,
    })
  );

  return {
    update(delta) {
      collisionDetect(instances.filter(canCollision).filter(canTransform));

      update(delta, instances);

      instances.forEach((instance) => {
        if (canControl(instance)) {
          instance.handleInput(getKeyPressed());
        }

        if (canShoot(instance) && instance.canShoot) {
          requestAnimationFrame(() => {
            instances = [...instances, instance.shoot()];
          });

          instance.canShoot = false;
        }

        if (instance.destroy) {
          requestAnimationFrame(() => {
            instances = instances.filter((_instance) => _instance !== instance);
          });

          return;
        }

        instance.update?.(delta);
      });
    },

    render(stage) {
      clear();

      instances
        .filter(canRender)
        .forEach((instance) => render(stage, instance));
    },
  };
}

Optimization

这时应该会再度遇到记忆体问题,我们要将每次产生出来的 PIXI.Text 释放掉。

const graphics = new _Graphics();
let pools: DisplayObject[] = [];

export function clear() {
  pools.forEach((obj) => obj.destroy());
  pools = [];

  graphics.clear();
}
function Text(instance: Renderer & Transform) {
  if (instance.renderer.type !== "text") return;

  const src = instance.renderer.src;

  const text = new _Text(src, {
    fontFamily: "VT323",
    fontSize: 16,
    fill: 0xffffff,
  });

  const { x, y } = instance.position;

  text.position.set(x, y);

+ pools.push(text);

  return text;
}

这样就不会因为 PIXI.Text 而产生记忆体问题。

关於兔兔们:


<<:  Day11:今天来谈一下Microsoft-Defender-for-Endpoint执行辨识项和实体调查

>>:  Basic Customization

Day 3: LeetCode 995. Minimum Number of K Consecutive Bit Flips

Tag:随意刷-未写过的 Source: 995. Minimum Number of K Cons...

程序码流程规划之...日记文

今天,依然是篇日记文,每天遇到的事有太多好写了。 一早 同事:Mandy, 今天是 deadline...

Ubuntu巡航记(4) -- Rust 安装

前言 Rust 是一个现代版的 C/C++ 程序语言,它加入物件导向、套件安装(cargo)、函数式...

Day 23 - Promise

Promise 解决了回调地狱,在处理一些需要花费比较长时间的任务时使用 Promise 就可以进行...

Django #1 安装环境

1. Python windows Linux sudo apt install python3-p...