Lớp Script trong LayaAir: Phát triển Logic Game

Tổng quan

Lớp Script là lớp cơ sở cho các thành phần kịch bản, kế thừa từ Component. Khác với Component, Script được thiết kế riêng cho logic game với các phương thức vòng đời và xử lý sự kiện.

Trong kiến trúc ECS (Entity-Component-System) của LayaAir, Script đóng hai vai trò:

  • Thành phần: Lưu trữ dữ liệu qua thuộc tính (sử dụng decorator @property)
  • Hệ thống: Xử lý logic qua phương thức vòng đời

So sánh Script và Component

Đặc điểmComponentScript
Mục đíchLớp cơ sở chungChuyên cho logic game
Loại ownerNodeSprite | Sprite3D
Sự kiệnKhông hỗ trợHỗ trợ chuột, bàn phím, vật lý
DecoratorCơ bảnĐầy đủ @regClass, @property

Decorator

@regClass()

Đăng ký lớp với IDE để sử dụng trong editor:

import { regClass } from "laya";

@regClass()
export class GameLogic extends Laya.Script {
    // Nội dung script
}

@property()

Hiển thị thuộc tính trong IDE:

import { regClass, property } from "laya";

@regClass()
export class PlayerLogic extends Laya.Script {
    @property(Number)
    public velocity: number = 8;

    @property(String)
    public characterName: string = "Warrior";

    @property(Laya.Sprite3D)
    public objective: Laya.Sprite3D;
}

Tham chiếu API

Thuộc tính

Thuộc tínhKiểuMô tả
ownerSprite | Sprite3DNode sở hữu script (chỉ đọc)

Phương thức vòng đời

Phương thứcThời điểm gọi
onAdded(): voidKhi thêm vào node (kể cả node chưa kích hoạt)
onReset(): voidThiết lập lại tham số mặc định
onAwake(): voidKích hoạt script (chạy một lần)
onEnable(): voidKhi node được thêm vào stage
onStart(): voidTrước onUpdate đầu tiên (chạy một lần)
onUpdate(): voidCập nhật mỗi frame
onLateUpdate(): voidCập nhật sau onUpdate
onDisable(): voidKhi node bị xóa khỏi stage
onDestroy(): voidKhi node bị hủy

Ví dụ cơ bản

Tạo script

import { regClass } from "laya";

@regClass()
export class CoreLogic extends Laya.Script {
    public rotationVelocity: number = 45;

    onEnable(): void {
        console.log("Script activated");
    }

    onUpdate(): void {
        const obj = this.owner as Laya.Sprite3D;
        obj.transform.rotate(new Laya.Vector3(0, 1, 0), false);
    }
}

Thêm script vào node

// Node 2D
const node2D = new Laya.Sprite();
node2D.addComponent(CoreLogic);

// Node 3D
const node3D = new Laya.Sprite3D();
node3D.addComponent(CoreLogic);

Xử lý sự kiện chuột

import { regClass } from "laya";

@regClass()
export class DragHandler extends Laya.Script {
    private dragOffset: Laya.Point = new Laya.Point();

    onMouseDown(evt: Laya.Event): void {
        const target = this.owner as Laya.Sprite;
        this.dragOffset.set(target.x - evt.stageX, target.y - evt.stageY);
        target.startDrag();
    }

    onMouseDrag(evt: Laya.Event): void {
        const target = this.owner as Laya.Sprite;
        target.pos(evt.stageX + this.dragOffset.x, evt.stageY + this.dragOffset.y);
    }

    onMouseUp(): void {
        (this.owner as Laya.Sprite).stopDrag();
    }
}

Quản lý đối tượng

import { regClass, property } from "laya";

@regClass()
export class ObjectManager extends Laya.Script {
    @property({ type: Laya.Prefab })
    public projectileTemplate: Laya.Prefab;

    onAwake(): void {
        if (this.projectileTemplate) {
            const projectile = this.projectileTemplate.create();
            (this.owner as Laya.Sprite3D).addChild(projectile);
        }
    }
}

Lưu ý quan trọng

  • Kiểm tra kiểu owner: const obj2D = this.owner as Laya.Sprite;
  • Tránh truy vấn component trong onUpdate: Cache component trong onAwake
  • Sự kiện chuột/bàn phím không cần đăng ký thủ công
  • 2D Physics không kích hoạt onTriggerStay ở chế độ sensor

Thẻ: LayaAir ECS typescript GameEngine decorator

Đăng vào ngày 27 tháng 5 lúc 02:37