mirror of
https://github.com/unanmed/HumanBreak.git
synced 2026-11-27 20:34:30 +08:00
feat: 新的渲染系统
This commit is contained in:
1 parent
af38d2e47e
commit
2f8fbd375c
34 files changed
+1173
-179
No files matched your search
@@ -0,0 +1,184 @@
|
||||
import { ReadonlyMat3, ReadonlyVec3, mat3, vec2, vec3 } from 'gl-matrix';
|
||||
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
|
||||
|
||||
interface CameraEvent extends EmitableEvent {}
|
||||
|
||||
export class Camera extends EventEmitter<CameraEvent> {
|
||||
mat: mat3 = mat3.create();
|
||||
|
||||
x: number = 0;
|
||||
y: number = 0;
|
||||
scaleX: number = 1;
|
||||
scaleY: number = 1;
|
||||
rad: number = 0;
|
||||
|
||||
/**
|
||||
* 重设摄像机的所有参数
|
||||
*/
|
||||
reset() {
|
||||
mat3.identity(this.mat);
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
this.scaleX = 1;
|
||||
this.scaleY = 1;
|
||||
this.rad = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改摄像机的缩放,叠加关系
|
||||
*/
|
||||
scale(x: number, y: number = x) {
|
||||
mat3.scale(this.mat, this.mat, [x, y]);
|
||||
this.scaleX *= x;
|
||||
this.scaleY *= y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动摄像机,叠加关系
|
||||
*/
|
||||
move(x: number, y: number) {
|
||||
mat3.translate(this.mat, this.mat, [x, y]);
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 旋转摄像机,叠加关系
|
||||
*/
|
||||
rotate(rad: number) {
|
||||
mat3.rotate(this.mat, this.mat, rad);
|
||||
this.rad += rad;
|
||||
if (this.rad >= Math.PI * 2) {
|
||||
const n = Math.floor(this.rad / Math.PI / 2);
|
||||
this.rad -= n * Math.PI * 2;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置摄像机的缩放,非叠加关系
|
||||
*/
|
||||
setScale(x: number, y: number = x) {
|
||||
mat3.scale(this.mat, this.mat, [x / this.scaleX, y / this.scaleY]);
|
||||
this.scaleX = x;
|
||||
this.scaleY = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置摄像机的位置,非叠加关系
|
||||
*/
|
||||
setTranslate(x: number, y: number) {
|
||||
mat3.translate(this.mat, this.mat, [x - this.x, y - this.y]);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置摄像机的旋转,非叠加关系
|
||||
*/
|
||||
setRotate(rad: number) {
|
||||
mat3.rotate(this.mat, this.mat, rad - this.rad);
|
||||
this.rad = rad;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置摄像机的变换矩阵,叠加模式
|
||||
* @param a 水平缩放
|
||||
* @param b 垂直倾斜
|
||||
* @param c 水平倾斜
|
||||
* @param d 垂直缩放
|
||||
* @param e 水平移动
|
||||
* @param f 垂直移动
|
||||
*/
|
||||
transform(
|
||||
a: number,
|
||||
b: number,
|
||||
c: number,
|
||||
d: number,
|
||||
e: number,
|
||||
f: number
|
||||
) {
|
||||
mat3.multiply(
|
||||
this.mat,
|
||||
this.mat,
|
||||
mat3.fromValues(a, b, 0, c, d, 0, e, f, 1)
|
||||
);
|
||||
this.calAttributes();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置摄像机的变换矩阵,非叠加模式
|
||||
* @param a 水平缩放
|
||||
* @param b 垂直倾斜
|
||||
* @param c 水平倾斜
|
||||
* @param d 垂直缩放
|
||||
* @param e 水平移动
|
||||
* @param f 垂直移动
|
||||
*/
|
||||
setTransform(
|
||||
a: number,
|
||||
b: number,
|
||||
c: number,
|
||||
d: number,
|
||||
e: number,
|
||||
f: number
|
||||
) {
|
||||
mat3.set(this.mat, a, b, 0, c, d, 0, e, f, 1);
|
||||
this.calAttributes();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新计算 translation scaling rotation
|
||||
*/
|
||||
calAttributes() {
|
||||
const [x, y] = getTranslation(this.mat);
|
||||
const [scaleX, scaleY] = getScaling(this.mat);
|
||||
const rad = getRotation(this.mat);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.scaleX = scaleX;
|
||||
this.scaleY = scaleY;
|
||||
this.rad = rad;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据摄像机的信息,将一个点转换为计算后的位置
|
||||
* @param camera 摄像机
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
static transformed(camera: Camera, x: number, y: number) {
|
||||
return multiplyVec3(camera.mat, [x, y, 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据摄像机的信息,将一个计算后的位置逆转换为原位置
|
||||
* @param camera 摄像机
|
||||
* @param x 横坐标
|
||||
* @param y 纵坐标
|
||||
*/
|
||||
static untransformed(camera: Camera, x: number, y: number) {
|
||||
const invert = mat3.create();
|
||||
mat3.invert(invert, camera.mat);
|
||||
return multiplyVec3(invert, [x, y, 1]);
|
||||
}
|
||||
}
|
||||
|
||||
function multiplyVec3(mat: ReadonlyMat3, vec: ReadonlyVec3) {
|
||||
return vec3.fromValues(
|
||||
mat[0] * vec[0] + mat[3] * vec[1] + mat[6] * vec[2],
|
||||
mat[1] * vec[0] + mat[4] * vec[1] + mat[7] * vec[2],
|
||||
mat[2] * vec[0] + mat[5] * vec[1] + mat[8] * vec[2]
|
||||
);
|
||||
}
|
||||
|
||||
function getTranslation(mat: ReadonlyMat3): vec2 {
|
||||
return [mat[6], mat[7]];
|
||||
}
|
||||
|
||||
function getScaling(mat: ReadonlyMat3): vec2 {
|
||||
return [Math.hypot(mat[0], mat[3]), Math.hypot(mat[1], mat[4])];
|
||||
}
|
||||
|
||||
function getRotation(mat: ReadonlyMat3): number {
|
||||
return Math.atan2(mat[3], mat[0]);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { MotaOffscreenCanvas2D } from '../fx/canvas2d';
|
||||
import { Camera } from './camera';
|
||||
import {
|
||||
ICanvasCachedRenderItem,
|
||||
RenderItem,
|
||||
RenderItemPosition,
|
||||
withCacheRender
|
||||
} from './item';
|
||||
|
||||
export class Container extends RenderItem implements ICanvasCachedRenderItem {
|
||||
children: RenderItem[] = [];
|
||||
sortedChildren: RenderItem[] = [];
|
||||
|
||||
canvas: MotaOffscreenCanvas2D;
|
||||
|
||||
constructor(type: RenderItemPosition = 'static') {
|
||||
super();
|
||||
this.canvas = new MotaOffscreenCanvas2D();
|
||||
this.type = type;
|
||||
this.canvas.withGameScale(true);
|
||||
}
|
||||
|
||||
render(
|
||||
canvas: HTMLCanvasElement,
|
||||
ctx: CanvasRenderingContext2D,
|
||||
camera: Camera
|
||||
): void {
|
||||
this.emit('beforeUpdate', this);
|
||||
withCacheRender(this, canvas, ctx, camera, c => {
|
||||
this.sortedChildren.forEach(v => {
|
||||
v.render(c.canvas, c.ctx, camera);
|
||||
});
|
||||
});
|
||||
this.emit('afterUpdate', this);
|
||||
}
|
||||
|
||||
size(width: number, height: number) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.canvas.size(width, height);
|
||||
this.writing = this.using;
|
||||
this.update(this);
|
||||
}
|
||||
|
||||
pos(x: number, y: number) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加子元素到这个容器上,然后在下一个tick执行更新
|
||||
* @param children 要添加的子元素
|
||||
*/
|
||||
appendChild(children: RenderItem[]) {
|
||||
children.forEach(v => (v.parent = this));
|
||||
this.children.push(...children);
|
||||
this.sortedChildren = this.children
|
||||
.slice()
|
||||
.sort((a, b) => a.zIndex - b.zIndex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
import { Ticker } from 'mutate-animate';
|
||||
import { MotaCanvas2D } from '../fx/canvas2d';
|
||||
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
|
||||
import { debounce } from 'lodash-es';
|
||||
|
||||
// 重写样板的勇士绘制
|
||||
|
||||
const canvas = MotaCanvas2D.for('@hero', false);
|
||||
|
||||
Mota.require('var', 'loading').once('coreInit', () => {
|
||||
canvas.withGameScale(true);
|
||||
canvas.setHD(false);
|
||||
canvas.size(480, 480);
|
||||
canvas.pos(0, 0);
|
||||
canvas.css(`z-index: 40`);
|
||||
canvas.canvas.classList.add('no-anti-aliasing');
|
||||
canvas.setTarget(core.dom.gameDraw);
|
||||
canvas.mount();
|
||||
});
|
||||
|
||||
const DIR_INDEX: Record<Dir, number> = {
|
||||
down: 0,
|
||||
left: 1,
|
||||
right: 2,
|
||||
up: 3
|
||||
};
|
||||
|
||||
interface HeroDrawItem {
|
||||
id: AllIds | 'hero';
|
||||
image: HTMLCanvasElement;
|
||||
}
|
||||
|
||||
interface HeroDrawing extends HeroDrawItem {
|
||||
x: number;
|
||||
y: number;
|
||||
dir: Dir;
|
||||
}
|
||||
|
||||
interface HeroRendererEvent extends EmitableEvent {
|
||||
beforeDraw: () => void;
|
||||
afterDraw: () => void;
|
||||
}
|
||||
|
||||
const resetMoving = debounce((render: HeroRenderer) => {
|
||||
render.moving = 0;
|
||||
}, 500);
|
||||
|
||||
export class HeroRenderer extends EventEmitter<HeroRendererEvent> {
|
||||
followers: HeroDrawItem[] = [];
|
||||
hero!: HeroDrawItem;
|
||||
|
||||
/** 移动状态 */
|
||||
moving: number = 0;
|
||||
/** 移动过程中的偏移 */
|
||||
offset: number = 0;
|
||||
/** 勇士绘制时的alpha通道 */
|
||||
alpha: number = 1;
|
||||
|
||||
ticker: Ticker = new Ticker();
|
||||
|
||||
/** 是否是后退状态 */
|
||||
private back: boolean = false;
|
||||
/** 是否正在移动 */
|
||||
private isMoving: boolean = false;
|
||||
/** 上一次换腿(?的时间 */
|
||||
private lastMoving: number = 0;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
Mota.require('var', 'loading').once('coreInit', () => {
|
||||
this.ticker.add(time => {
|
||||
if (core.status.heroMoving < 0 || !this.isMoving) return;
|
||||
if (time - this.lastMoving > core.values.moveSpeed) {
|
||||
this.lastMoving = time;
|
||||
this.moving++;
|
||||
this.moving %= 4;
|
||||
}
|
||||
this.draw();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置勇士绘制信息
|
||||
*/
|
||||
setHero() {
|
||||
const image = core.material.images.hero;
|
||||
const canvas = new MotaCanvas2D();
|
||||
canvas.setHD(false);
|
||||
canvas.size(image.width, image.height);
|
||||
canvas.ctx.drawImage(image, 0, 0);
|
||||
this.hero = {
|
||||
id: 'hero',
|
||||
image: canvas.canvas
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行绘制
|
||||
*/
|
||||
draw() {
|
||||
if (!core.isPlaying()) return;
|
||||
const { ctx, canvas: can } = canvas;
|
||||
const { x, y, direction: dir } = core.status.hero.loc;
|
||||
ctx.clearRect(0, 0, can.width, can.height);
|
||||
ctx.globalAlpha = this.alpha;
|
||||
|
||||
this.emit('beforeDraw');
|
||||
|
||||
const data: HeroDrawing[] = [];
|
||||
data.push({ ...this.hero, x, y, dir });
|
||||
core.status.hero.followers.forEach((v, i) => {
|
||||
const { id, image } = this.followers[i];
|
||||
data.push({ x: v.x, y: v.y, dir: v.direction, id, image });
|
||||
});
|
||||
|
||||
const { offsetX: ox, offsetY: oy } = core.bigmap;
|
||||
const sgn = this.back ? -1 : 1;
|
||||
const offset = this.offset * sgn;
|
||||
|
||||
const frame = this.isMoving ? this.moving % 4 : 0;
|
||||
|
||||
data.forEach(v => {
|
||||
// hero offset x
|
||||
const hox = offset * core.utils.scan[v.dir].x;
|
||||
const hoy = offset * core.utils.scan[v.dir].y;
|
||||
const cx = v.x * 32 + 16 - ox + hox;
|
||||
const cy = v.y * 32 + 16 - oy + hoy;
|
||||
const { width, height } = v.image;
|
||||
const pw = width / 4;
|
||||
const ph = height / 4;
|
||||
const line = DIR_INDEX[v.dir];
|
||||
|
||||
const px = cx - pw / 2;
|
||||
const py = cy - ph + 16;
|
||||
|
||||
const sx = frame * pw;
|
||||
const sy = line * ph;
|
||||
|
||||
ctx.drawImage(v.image, sx, sy, pw, ph, px, py, pw, ph);
|
||||
});
|
||||
|
||||
this.emit('afterDraw');
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置绘制状态为正在移动还是静止
|
||||
*/
|
||||
move(moving: boolean) {
|
||||
this.isMoving = moving;
|
||||
if (!moving) {
|
||||
resetMoving(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前是否为后退状态
|
||||
*/
|
||||
backward(back: boolean) {
|
||||
this.back = back;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置绘制时的alpha
|
||||
*/
|
||||
setAlpha(alpha: number) {
|
||||
this.alpha = alpha;
|
||||
}
|
||||
}
|
||||
|
||||
const render = new HeroRenderer();
|
||||
|
||||
export { render as heroRender };
|
||||
@@ -0,0 +1,200 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
|
||||
import { MotaOffscreenCanvas2D } from '../fx/canvas2d';
|
||||
import { Camera } from './camera';
|
||||
|
||||
export type RenderFunction = (
|
||||
canvas: MotaOffscreenCanvas2D,
|
||||
camera: Camera
|
||||
) => void;
|
||||
|
||||
export type RenderItemPosition = 'absolute' | 'static';
|
||||
|
||||
interface IRenderCache {
|
||||
/** 缓存列表 */
|
||||
cacheList: Map<string, MotaOffscreenCanvas2D>;
|
||||
/** 当前正在使用的缓存 */
|
||||
using?: string;
|
||||
/** 下次绘制需要写入的缓存 */
|
||||
writing?: string;
|
||||
|
||||
/**
|
||||
* 在之后的绘制中使用缓存,如果缓存不存在,那么就不使用缓存,并在下一次绘制结束后写入
|
||||
* @param index 缓存的索引,不填时表示不使用缓存
|
||||
*/
|
||||
useCache(index?: string): void;
|
||||
|
||||
/**
|
||||
* 将下次绘制写入缓存并使用
|
||||
* @param index 缓存的索引,不填时表示不进行缓存
|
||||
*/
|
||||
cache(index?: string): void;
|
||||
|
||||
/**
|
||||
* 清除指定或所有缓存
|
||||
* @param index 要清除的缓存,不填代表清除所有
|
||||
*/
|
||||
clearCache(index?: string): void;
|
||||
}
|
||||
|
||||
export interface IRenderUpdater {
|
||||
/**
|
||||
* 更新这个渲染元素
|
||||
* @param item 触发更新事件的元素,可以是自身触发。如果不填表示手动触发,而非渲染内容发生变化而引起的触发
|
||||
*/
|
||||
update(item?: RenderItem): void;
|
||||
}
|
||||
|
||||
export interface ICanvasCachedRenderItem {
|
||||
/** 离屏画布,首先渲染到它上面,然后由Renderer渲染到最终画布上 */
|
||||
canvas: MotaOffscreenCanvas2D;
|
||||
}
|
||||
|
||||
interface IRenderAnchor {
|
||||
anchorX: number;
|
||||
anchorY: number;
|
||||
|
||||
/**
|
||||
* 设置渲染元素的位置锚点
|
||||
* @param x 锚点的横坐标,小数,0表示最左边,1表示最右边
|
||||
* @param y 锚点的纵坐标,小数,0表示最上边,1表示最下边
|
||||
*/
|
||||
setAnchor(x: number, y: number): void;
|
||||
}
|
||||
|
||||
interface RenderItemEvent extends EmitableEvent {
|
||||
beforeUpdate: (item?: RenderItem) => void;
|
||||
afterUpdate: (item?: RenderItem) => void;
|
||||
}
|
||||
|
||||
export abstract class RenderItem
|
||||
extends EventEmitter<RenderItemEvent>
|
||||
implements IRenderCache, IRenderUpdater, IRenderAnchor
|
||||
{
|
||||
zIndex: number = 0;
|
||||
|
||||
x: number = 0;
|
||||
y: number = 0;
|
||||
width: number = 200;
|
||||
height: number = 200;
|
||||
|
||||
cacheList: Map<string, MotaOffscreenCanvas2D> = new Map();
|
||||
|
||||
using?: string;
|
||||
writing?: string;
|
||||
|
||||
anchorX: number = 0;
|
||||
anchorY: number = 0;
|
||||
|
||||
/** 渲染模式,absolute表示绝对位置,static表示跟随摄像机移动,只对顶层元素有效 */
|
||||
type: 'absolute' | 'static' = 'static';
|
||||
|
||||
parent?: RenderItem;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.using = '@default';
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染这个对象
|
||||
* @param canvas 渲染至的画布
|
||||
* @param ctx 渲染至的画布的渲染上下文
|
||||
* @param camera 渲染时使用的摄像机
|
||||
*/
|
||||
abstract render(
|
||||
canvas: HTMLCanvasElement,
|
||||
ctx: CanvasRenderingContext2D,
|
||||
camera: Camera
|
||||
): void;
|
||||
|
||||
/**
|
||||
* 修改这个对象的大小
|
||||
*/
|
||||
abstract size(width: number, height: number): void;
|
||||
|
||||
/**
|
||||
* 修改这个对象的位置
|
||||
*/
|
||||
abstract pos(x: number, y: number): void;
|
||||
|
||||
useCache(index?: string): void {
|
||||
if (isNil(index)) {
|
||||
this.using = void 0;
|
||||
return;
|
||||
}
|
||||
if (!this.cacheList.has(index)) {
|
||||
this.writing = index;
|
||||
}
|
||||
this.using = index;
|
||||
}
|
||||
|
||||
cache(index?: string): void {
|
||||
this.writing = index;
|
||||
this.using = index;
|
||||
}
|
||||
|
||||
clearCache(index?: string): void {
|
||||
if (isNil(index)) {
|
||||
this.writing = void 0;
|
||||
this.using = void 0;
|
||||
this.cacheList.clear();
|
||||
} else {
|
||||
this.cacheList.delete(index);
|
||||
}
|
||||
}
|
||||
|
||||
setAnchor(x: number, y: number): void {
|
||||
this.anchorX = x;
|
||||
this.anchorY = y;
|
||||
}
|
||||
|
||||
update(item?: RenderItem): void {
|
||||
this.writing = this.using;
|
||||
this.using = void 0;
|
||||
this.parent?.update(item);
|
||||
}
|
||||
}
|
||||
|
||||
export function withCacheRender(
|
||||
item: RenderItem & ICanvasCachedRenderItem,
|
||||
canvas: HTMLCanvasElement,
|
||||
ctx: CanvasRenderingContext2D,
|
||||
camera: Camera,
|
||||
fn: RenderFunction
|
||||
) {
|
||||
const { width, height } = item;
|
||||
const ax = width * item.anchorX;
|
||||
const ay = height * item.anchorY;
|
||||
let write = item.writing;
|
||||
if (!isNil(item.using)) {
|
||||
const cache = item.cacheList.get(item.using);
|
||||
if (cache) {
|
||||
ctx.drawImage(
|
||||
cache.canvas,
|
||||
item.x - ax,
|
||||
item.y - ay,
|
||||
item.width,
|
||||
item.height
|
||||
);
|
||||
return;
|
||||
}
|
||||
write = item.using;
|
||||
}
|
||||
const { canvas: c, ctx: ct } = item.canvas;
|
||||
ct.clearRect(0, 0, c.width, c.height);
|
||||
fn(item.canvas, camera);
|
||||
if (!isNil(write)) {
|
||||
const cache = item.cacheList.get(write);
|
||||
if (cache) {
|
||||
const { canvas, ctx } = cache;
|
||||
ctx.drawImage(c, 0, 0);
|
||||
} else {
|
||||
item.cacheList.set(write, MotaOffscreenCanvas2D.clone(item.canvas));
|
||||
}
|
||||
}
|
||||
|
||||
ctx.drawImage(c, item.x - ax, item.y - ay, item.width, item.height);
|
||||
item.using = write;
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import { Sprite } from '../sprite';
|
||||
|
||||
type CanvasStyle = string | CanvasGradient | CanvasPattern;
|
||||
|
||||
export class Text extends Sprite {
|
||||
text: string;
|
||||
|
||||
fillStyle?: CanvasStyle = '#fff';
|
||||
strokeStyle?: CanvasStyle;
|
||||
font?: string = '';
|
||||
|
||||
private length: number = 0;
|
||||
private descent: number = 0;
|
||||
|
||||
constructor(text: string = '') {
|
||||
super();
|
||||
|
||||
this.text = text;
|
||||
if (text.length > 0) this.calBox();
|
||||
|
||||
this.renderFn = ({ canvas, ctx }) => {
|
||||
ctx.save();
|
||||
ctx.textBaseline = 'bottom';
|
||||
ctx.fillStyle = this.fillStyle ?? 'transparent';
|
||||
ctx.strokeStyle = this.strokeStyle ?? 'transparent';
|
||||
ctx.font = this.font ?? '';
|
||||
|
||||
if (this.fillStyle) {
|
||||
ctx.fillText(this.text, 0, this.descent);
|
||||
}
|
||||
if (this.strokeStyle) {
|
||||
ctx.strokeText(this.text, 0, this.descent);
|
||||
}
|
||||
ctx.restore();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文字的长度
|
||||
*/
|
||||
measure() {
|
||||
this.canvas.ctx.save();
|
||||
this.canvas.ctx.textBaseline = 'bottom';
|
||||
this.canvas.ctx.font = this.font ?? '';
|
||||
const res = this.canvas.ctx.measureText(this.text);
|
||||
this.canvas.ctx.restore();
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置显示文字
|
||||
* @param text 显示的文字
|
||||
*/
|
||||
setText(text: string) {
|
||||
this.text = text;
|
||||
this.writing = this.using;
|
||||
this.using = void 0;
|
||||
this.calBox();
|
||||
if (this.parent) this.update(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置使用的字体
|
||||
* @param font 字体
|
||||
*/
|
||||
setFont(font: string) {
|
||||
this.font = font;
|
||||
this.calBox();
|
||||
if (this.parent) this.update(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字体样式
|
||||
* @param fill 填充样式
|
||||
* @param stroke 描边样式
|
||||
*/
|
||||
setStyle(fill?: CanvasStyle, stroke?: CanvasStyle) {
|
||||
this.fillStyle = fill;
|
||||
this.strokeStyle = stroke;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算字体所占空间,从而确定这个元素的大小
|
||||
*/
|
||||
calBox() {
|
||||
const { width, fontBoundingBoxAscent } = this.measure();
|
||||
this.length = width;
|
||||
this.descent = fontBoundingBoxAscent;
|
||||
this.size(width, fontBoundingBoxAscent);
|
||||
}
|
||||
}
|
||||
|
||||
type SizedCanvasImageSource = Exclude<
|
||||
CanvasImageSource,
|
||||
VideoFrame | SVGElement
|
||||
>;
|
||||
|
||||
export class Image extends Sprite {
|
||||
image: SizedCanvasImageSource;
|
||||
|
||||
constructor(image: SizedCanvasImageSource) {
|
||||
super();
|
||||
this.image = image;
|
||||
this.canvas.withGameScale(false);
|
||||
this.size(image.width, image.height);
|
||||
|
||||
this.renderFn = ({ canvas, ctx }) => {
|
||||
ctx.drawImage(this.image, 0, 0);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置图片资源
|
||||
* @param image 图片资源
|
||||
*/
|
||||
setImage(image: SizedCanvasImageSource) {
|
||||
this.image = image;
|
||||
this.size(image.width, image.height);
|
||||
this.writing = this.using;
|
||||
this.using = void 0;
|
||||
this.update(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
import { MotaCanvas2D, MotaOffscreenCanvas2D } from '../fx/canvas2d';
|
||||
import { Camera } from './camera';
|
||||
import { Container } from './container';
|
||||
import { RenderItem, withCacheRender } from './item';
|
||||
import { Image, Text } from './preset/misc';
|
||||
|
||||
export class MotaRenderer extends Container {
|
||||
canvas: MotaOffscreenCanvas2D;
|
||||
camera: Camera;
|
||||
|
||||
/** 摄像机缓存,如果是需要快速切换摄像机的场景,使用缓存可以大幅提升性能表现 */
|
||||
cameraCache: Map<Camera, MotaOffscreenCanvas2D> = new Map();
|
||||
target: MotaCanvas2D;
|
||||
|
||||
private needUpdate: boolean = false;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.canvas = new MotaOffscreenCanvas2D();
|
||||
this.camera = new Camera();
|
||||
this.target = new MotaCanvas2D(`render-main`);
|
||||
this.width = 480;
|
||||
this.height = 480;
|
||||
this.target.withGameScale(true);
|
||||
this.target.size(480, 480);
|
||||
this.canvas.withGameScale(true);
|
||||
this.canvas.size(480, 480);
|
||||
this.target.css(`z-index: 100`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用某个摄像机
|
||||
* @param camera 要使用的摄像机
|
||||
* @param noCache 是否不使用缓存,当切换至的目标摄像机相比切走时发生了例如位置的变化时,一般需要设置为true,
|
||||
* 否则会使用上一次被切换走时的缓存
|
||||
*/
|
||||
useCamera(camera: Camera, noCache: boolean = false) {
|
||||
const cache = MotaOffscreenCanvas2D.clone(this.canvas);
|
||||
this.cameraCache.set(this.camera, cache);
|
||||
this.camera = camera;
|
||||
const nowCache = this.cameraCache.get(camera);
|
||||
if (!nowCache || !noCache) this.render();
|
||||
else this.renderCache(nowCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除某个摄像机的画面缓存
|
||||
* @param camera 要删除的缓存对应的摄像机
|
||||
*/
|
||||
freeCameraCache(camera: Camera) {
|
||||
this.cameraCache.delete(camera);
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染游戏画面
|
||||
*/
|
||||
render() {
|
||||
const { canvas, ctx } = this.target;
|
||||
const camera = this.camera;
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
withCacheRender(this, canvas, ctx, camera, canvas => {
|
||||
const { canvas: ca, ctx: ct, scale } = canvas;
|
||||
const mat = camera.mat;
|
||||
const a = mat[0] * scale;
|
||||
const b = mat[1];
|
||||
const c = mat[3];
|
||||
const d = mat[4] * scale;
|
||||
const e = mat[6];
|
||||
const f = mat[7];
|
||||
this.sortedChildren.forEach(v => {
|
||||
if (v.type === 'absolute') {
|
||||
ct.setTransform(scale, 0, 0, scale, 0, 0);
|
||||
} else {
|
||||
ct.setTransform(a, b, c, d, e, f);
|
||||
}
|
||||
v.render(ca, ct, camera);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新渲染,在下一个tick更新
|
||||
*/
|
||||
update(item?: RenderItem) {
|
||||
if (this.needUpdate) return;
|
||||
this.needUpdate = true;
|
||||
requestAnimationFrame(() => {
|
||||
this.writing = this.using;
|
||||
this.using = void 0;
|
||||
this.needUpdate = false;
|
||||
this.emit('beforeUpdate', item);
|
||||
this.render();
|
||||
this.emit('afterUpdate', item);
|
||||
this.using = this.writing;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 将缓存内容渲染至画面
|
||||
* @param cache 渲染缓存,是一个离屏Canvas2D对象
|
||||
*/
|
||||
renderCache(cache: MotaOffscreenCanvas2D) {
|
||||
const { canvas, ctx } = this.canvas;
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.width);
|
||||
ctx.drawImage(cache.canvas, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加至游戏画面
|
||||
*/
|
||||
mount() {
|
||||
this.target.mount();
|
||||
}
|
||||
}
|
||||
|
||||
Mota.require('var', 'hook').once('reset', () => {
|
||||
const render = new MotaRenderer();
|
||||
const con = new Container('static');
|
||||
const camera = render.camera;
|
||||
render.mount();
|
||||
|
||||
const testText = new Text();
|
||||
testText.setText('测试测试');
|
||||
testText.pos(100, 100);
|
||||
testText.setFont('32px normal');
|
||||
testText.setStyle('#fff');
|
||||
con.size(480, 480);
|
||||
const testImage = new Image(core.material.images.images['arrow.png']);
|
||||
testImage.pos(200, 200);
|
||||
|
||||
con.appendChild([testText, testImage]);
|
||||
|
||||
render.appendChild([con]);
|
||||
|
||||
render.update(render);
|
||||
|
||||
setTimeout(() => {
|
||||
testText.setFont('18px normal');
|
||||
}, 2000);
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Camera } from './camera';
|
||||
import { RenderFunction, RenderItem, withCacheRender } from './item';
|
||||
import { MotaOffscreenCanvas2D } from '../fx/canvas2d';
|
||||
|
||||
export class Sprite extends RenderItem {
|
||||
renderFn: RenderFunction;
|
||||
|
||||
canvas: MotaOffscreenCanvas2D;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.renderFn = () => {};
|
||||
this.canvas = new MotaOffscreenCanvas2D();
|
||||
this.canvas.withGameScale(true);
|
||||
}
|
||||
|
||||
render(
|
||||
canvas: HTMLCanvasElement,
|
||||
ctx: CanvasRenderingContext2D,
|
||||
camera: Camera
|
||||
): void {
|
||||
this.emit('beforeUpdate', this);
|
||||
withCacheRender(this, canvas, ctx, camera, canvas => {
|
||||
this.renderFn(canvas, camera);
|
||||
});
|
||||
this.emit('afterUpdate', this);
|
||||
}
|
||||
|
||||
size(width: number, height: number) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.canvas.size(width, height);
|
||||
this.writing = this.using;
|
||||
this.update(this);
|
||||
}
|
||||
|
||||
pos(x: number, y: number) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user