【ulua入门】(3)Demo实训,一个简陋的射击游戏

在这里下载ulua : https://github.com/jarjin/LuaFramework_UGUI

参考链接:http://www.manew.com/forum-47-396-1.html


项目的github地址 : https://github.com/Eagle-Lai/A_SimpleShootGameWith_uLua

就直接上代码了:

1、用于加载lua的c#代码

using UnityEngine;
using System.Collections;
using LuaFramework;
using LuaInterface;

namespace LuaFramework
{
    public class GameLuaComponent : MonoBehaviour
    {
        public LuaTable table;
        public static LuaTable Add(GameObject go, LuaTable tableClass)
        {
            LuaFunction fun = tableClass.GetLuaFunction("New");

            if (fun == null) return null;

            object[] rets = fun.Call(tableClass);
            if (rets.Length != 1) return null;

            GameLuaComponent cmp = go.AddComponent<GameLuaComponent>();
            cmp.table = (LuaTable)rets[0];
            print("???");
            cmp.CallAwake();
            return cmp.table;
        }

        public static LuaTable Get(GameObject go, LuaTable table)
        {
            GameLuaComponent[] cmps = go.GetComponents<GameLuaComponent>();
            foreach (GameLuaComponent item in cmps)
            {
                string mat1 = table.ToString();
                string mat2 = item.table.GetMetaTable().ToString();

                if (mat1 == mat2)
                {
                    return item.table;
                }
            }
           
            return null;
        }

        void CallAwake()
        {
            LuaFunction fun = table.GetLuaFunction("Awake");
            if (fun != null)
                fun.Call(table, gameObject);
        }

        void Start()
        {
            
            LuaFunction fun = table.GetLuaFunction("Start");
            if (fun != null)
                fun.Call(table, gameObject);
        }

        void Update()
        {
            LuaFunction fun = table.GetLuaFunction("Update");
            if (fun != null)
                fun.Call(table, gameObject);
        }
    }
}

lua代码:

local BgMove = {}

local bg
local bg2

 function BgMove:Awake()
	LuaHelper = LuaFramework.LuaHelper
	resMgr = LuaHelper.GetResManager()
	resMgr:LoadPrefab('bg', { 'bg' }, BgMove.OnLoadFinish)
end

function BgMove.OnLoadFinish(objs)
	bg = UnityEngine.GameObject.Instantiate(objs[0])
	bg2 = UnityEngine.GameObject.Instantiate(objs[0])

	bg2.transform.position = Vector3.New(-20.3, 0, 0)
	FixedUpdateBeat:Add(BgMove.FixedUpdate, bg)
end 



 function BgMove.FixedUpdate()
	if(bg.transform.position.x < 20.3) then
		local x = bg.transform.position.x + 0.05
		bg.transform.position = Vector3.New(x, 0,0)
	else
		bg.transform.position = Vector3.New(0, 0, 0)
	end

	if(bg2.transform.position.x < 0) then
		local x = bg2.transform.position.x + 0.05
		bg2.transform.position = Vector3.New(x, 0,0)
	else
		bg2.transform.position = Vector3.New(-20.3, 0, 0)
	end
end

function BgMove:New()
	local o = {}
	setmetatable(o, self)
	self.__index = self
	return o
end 

return BgMove;

local ObstacleSpawner = {}

local _obstacleSpawner
local _obstacle
local intervalTime = 5.5


function ObstacleSpawner:Awake()
	resMgr:LoadPrefab('bg', { 'ObstacleSpawner','Obstacle'}, ObstacleSpawner.OnLoadFinish)
end

function ObstacleSpawner.OnLoadFinish(objs)
	print("loadFinish")
	_obstacleSpawner = UnityEngine.GameObject.Instantiate(objs[0])
	_obstacle = objs[1]

	UpdateBeat:Add(ObstacleSpawner.Update, _obstacleSpawner)
end

function ObstacleSpawner.Update()
	intervalTime = intervalTime - UnityEngine.Time.deltaTime
	if(intervalTime < 0) then
		local pos = Vector3.New(_obstacleSpawner.transform.position.x,math.random(-3.5, 5.0), 0)
		UnityEngine.GameObject.Instantiate(_obstacle,pos, _obstacleSpawner.transform.rotation)
		intervalTime = 1.2
	end
end 

function ObstacleSpawner:New()
	local o = {}
	setmetatable(o, self)
	self.__index = self
	return o
end

return ObstacleSpawner

	

local PlayerController = {}

local player
local bullet
local Input = UnityEngine.Input
local intervalTime = 0.8
--local fireStep = 0.5

function PlayerController:Awake()
	resMgr:LoadPrefab('bg', { 'player', 'bullet'}, PlayerController.OnPlayerLoadFinish)
end

function PlayerController.OnPlayerLoadFinish(objs)
	player = UnityEngine.GameObject.Instantiate(objs[0])
	bullet = objs[1]
	FixedUpdateBeat:Add(PlayerController.FixedUpdate, player)
	UpdateBeat:Add(PlayerController.Update, player)
end



function PlayerController.Update(objs)
	intervalTime = intervalTime - UnityEngine.Time.deltaTime 
	if(Input.GetMouseButton(0) and intervalTime < 0 ) then
		UnityEngine.GameObject.Instantiate(bullet, player.transform.position, player.transform.rotation)	
		intervalTime = 0.8
	end
	
end



function PlayerController.FixedUpdate()

	local h = Input.GetAxis("Horizontal")
	local v = Input.GetAxis("Vertical")

	local x = player.transform.position.x + h / 5
	local y = player.transform.position.y + v / 5

	if y > 5.5 then y = 5.5 end
	if y < -3.5 then y = -3.5 end

	if x > 8 then x = 8 end
	if x < -8 then x = -8 end

	player.transform.position = Vector3.New(x, y, 0)
end

function PlayerController:New()
	local o = {}
	setmetatable(o, self)
	self.__index = self
	return o
end


return PlayerController

--主入口函数。从这里开始lua逻辑
function Main()		
	local go = UnityEngine.GameObject("bg")
	local BgMove = require "_Game/BgMove"
	LuaFramework.GameLuaComponent.Add(go, BgMove)

	local player = UnityEngine.GameObject("player")
	local PlayerController = require "_Game/PlayerController"
	LuaFramework.GameLuaComponent.Add(go, PlayerController)

	local Spawner = UnityEngine.GameObject("Spawner")
	local ObstacleSpawner = require "_Game/ObstacleSpawner"
	LuaFramework.GameLuaComponent.Add(Spawner, ObstacleSpawner)
end

--场景切换通知
function OnLevelWasLoaded(level)
	Time.timeSinceLevelLoad = 0
end
 辅助用的C#代码 :

using UnityEngine;
using System.Collections;

public class Obstacle : MonoBehaviour {
    public float speed;

	// Use this for initialization
	void Start () {
        Destroy(this.gameObject, 2f);
	}
	
	// Update is called once per frame
	void Update () {
        transform.Translate(speed, 0, 0);
	}

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "bullet" || other.gameObject.tag == "Player")
        {
            Destroy(other.gameObject);
            Destroy(this.gameObject);
        }
    }
}


using UnityEngine;
using System.Collections;

public class BulletMove : MonoBehaviour {

    public float speed;

	// Use this for initialization
	void Start () {
        Destroy(this.gameObject, 2f);
	}
	
	// Update is called once per frame
	void Update () {

        transform.Translate(speed, 0, 0);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值