哇噢星球

 找回密码
 立即注册
搜索
热搜: 地形 技能 Lua

ZinC OOP 演示代码 - 按照面向对象思维来组织脚本的例子

[复制链接]
皇家骑士 发表于 2019-1-27 19:16:49 | 显示全部楼层 |阅读模式
注:非新手向。

索引:
Zinc OOP 演示
Zinc OOP 演示2 - 在触发器Action中使用struct
Zinc OOP 演示3 - 简单路径点巡逻
Zinc TriggerAction to method 转发方案:update机制

[Jass] 纯文本查看 复制代码
// Zinc OOP 演示
// Zinc继承了vJass的struct,使得Zinc可以使用一部分OOP(面向对象编程)的程序编写方式。
// 这里是一个在Zinc中使用struct的演示,会在游戏开始时创建一个步兵,每1秒显示当前步兵的名字。

//! zinc

library Foo
{
    struct Creep
    {
        unit u;
        method showName()
        {
            BJDebugMsg(GetUnitName(this.u));
        }
        method init(unit u)
        {
            this.u = u;
        }
    }
    
    private Creep crp;
    
    function onInit()
    {
        unit u = CreateUnit(Player(0), 'hfoo', 0, 0, 0);
        trigger trig = CreateTrigger();
        crp = Creep.create();
        crp.init(u);
        TriggerRegisterTimerEventPeriodic(trig, 1);
        TriggerAddAction(trig, function()
        {
            crp.showName();
        });
    }
}

//! endzinc


[Jass] 纯文本查看 复制代码
// Zinc OOP 演示2 - 在触发器Action中使用struct
// 似乎Zinc的struct并不能通过绑定哈希表的方式来将某个对象(如unit)与其所属的struct连接起来,所以需要一种类似字典的记录来将struct和某个特定的对象绑定在一起,使得开发者可以在触发器Action中使用与这个对象相关的struct method,进一步呈现对象式编程在jass逻辑中的好处。

//! zinc

library Foo
{
    struct Creep
    {
        unit u;
        method showName()
        {
            BJDebugMsg(GetUnitName(this.u));
        }
        method init(integer uid) -> Creep
        {
            crps[crpsCount] = this;
            crpsCount += 1;
            this.u = CreateUnit(Player(0), uid, 0, 0, 0);
            return this;
        }
    }
    
    private Creep crps[];
    private integer crpsCount = 0;

    function onInit()
    {
        trigger trig;

        Creep.create().init('hfoo');
        Creep.create().init('hrif');
        Creep.create().init('hmpr');

        trig = CreateTrigger();
        TriggerRegisterTimerEventPeriodic(trig, 1);
        TriggerAddAction(trig, function()
        {
            integer i;

            for (0 <= i < crpsCount)
            {
                crps[i].showName();
            }
        });
    }
}

//! endzinc



[Jass] 纯文本查看 复制代码
// Zinc OOP 演示3 - 简单路径点巡逻

//! zinc

library Base
{
    public function DelayExecute(real time, code func) -> trigger
    {
        trigger trig = CreateTrigger();
        TriggerRegisterTimerEventSingle(trig, time);
        TriggerAddAction(trig, func);
        return trig;
    }
    public function LoopExecute(real interval, code func) -> trigger
    {
        trigger trig = CreateTrigger();
        TriggerRegisterTimerEventPeriodic(trig, interval);
        TriggerAddAction(trig, func);
        return trig;
    }
}

library Foo requires Base
{
    struct Point
    {
        real x;
        real y;
        
        static method create(real x, real y) -> Point
        {
            Point p = Point.allocate();
            p.x = x;
            p.y = y;
            return p;
        }
    }
    struct Path
    {
        Point points[10];
        integer count;

        method push(real x, real y)
        {
            integer index;

            count += 1;
            if (count > 10)
            {
                count = 10;
                BJDebugMsg("Path must contains no more than 10 points");
                return;
            }
            
            index = count - 1;
            points[index] = Point.create(x, y);
        }
        method pick(integer tick) -> Point
        {
            integer index = ModuloInteger(tick, count);
            return points[index];
        }
    }
    struct Creep
    {
        unit u;
        Path path;
        integer tick = -1;

        method showName()
        {
            BJDebugMsg(GetUnitName(this.u));
        }
        method init(integer uid, Path path) -> Creep
        {
            crps[crpsCount] = this;
            crpsCount += 1;
            this.u = CreateUnit(Player(0), uid, 0, 0, 0);
            this.path = path;
            return this;
        }
        method moveToNext()
        {
            Point p;
            tick += 1;
            p = path.pick(tick);
            IssuePointOrder(this.u, "Move", p.x, p.y);
        }
    }

    private Creep crps[];
    private integer crpsCount = 0;

    function onInit()
    {
        trigger trig;
        Creep crp;
        
        Path path = Path.create();
        path.push(0, 0);
        path.push(512, 0);
        path.push(512, 512);
        path.push(0, 512);

        Creep.create().init('hfoo', path).tick = -1;
        Creep.create().init('hrif', path).tick = 0;
        Creep.create().init('hmpr', path).tick = 1;

        trig = LoopExecute(5, function()
        {
            integer i;

            for (0 <= i < crpsCount)
            {
                crps[i].moveToNext();
            }
        });
    }
}

//! endzinc



[Jass] 纯文本查看 复制代码
// Zinc TriggerAction to method 转发方案:update机制

struct Creep
{
	method update()
	{
	  .. do whatever the things creep want ..
	}
}

onTrigger(function()
{
    crp[all].update();
});
回复

使用道具 举报

Archiver|手机版|小黑屋|哇噢星球  

GMT+8, 2019-4-22 03:12 , Processed in 0.164043 second(s), 12 queries , File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表