lua实现的状态机
2014-01-06 20:41
1541 查看
状态机就是管理在不同状态下的操作实现,这里我简单的提一下,大家看代码就懂了。最近学完了lua打算做点游戏,所有先用lua弄清楚一些常用的状态控制。
------创建状态机制类型三种状态学习,编码,休息
local state = {study="study",code="code",relax="relax"}
bobo={nowstate=state.study} ---默认的是学习状态
-----获取目前状态
function bobo.getstate()
return bobo.nowstate
end
----改变状态
function bobo.exchagestate(var)
bobo.nowstate=var
end
---是否想写代码
function bobo.iswriteabl()
if math.random(0,1) < 0.2 then ----居然只有0.2想写代码。。。
return true
end
end
-----是否想休息
function bobo.istire()
return true ------------这么干脆就说累了?
end
------编码中
function bobo.code()
print("i am codeing")
end
------休息中
function bobo.relax()
print("i am relax")
end
----学习中
function bobo.study()
print("i am study")
end
----模拟状态改变
while true do
if bobo.nowstate==state.study then
if bobo.istire() then
bobo.relax()
bobo.exchagestate(state.relax)
end
elseif bobo.nowstate==state.code then
if bobo.istire() then
bobo.relax()
bobo.exchagestate(state.relax)
end
elseif bobo.nowstate==state.relax then
if bobo.iswriteabl() then
bobo.code()
bobo.exchagestate(state.code)
else
bobo.study()
bobo.exchagestate(state.study)
end
end
end
------创建状态机制类型三种状态学习,编码,休息
local state = {study="study",code="code",relax="relax"}
bobo={nowstate=state.study} ---默认的是学习状态
-----获取目前状态
function bobo.getstate()
return bobo.nowstate
end
----改变状态
function bobo.exchagestate(var)
bobo.nowstate=var
end
---是否想写代码
function bobo.iswriteabl()
if math.random(0,1) < 0.2 then ----居然只有0.2想写代码。。。
return true
end
end
-----是否想休息
function bobo.istire()
return true ------------这么干脆就说累了?
end
------编码中
function bobo.code()
print("i am codeing")
end
------休息中
function bobo.relax()
print("i am relax")
end
----学习中
function bobo.study()
print("i am study")
end
----模拟状态改变
while true do
if bobo.nowstate==state.study then
if bobo.istire() then
bobo.relax()
bobo.exchagestate(state.relax)
end
elseif bobo.nowstate==state.code then
if bobo.istire() then
bobo.relax()
bobo.exchagestate(state.relax)
end
elseif bobo.nowstate==state.relax then
if bobo.iswriteabl() then
bobo.code()
bobo.exchagestate(state.code)
else
bobo.study()
bobo.exchagestate(state.study)
end
end
end
相关文章推荐
- lua面向对象与基于lua的状态机实现
- LUA实现状态模式
- Lua调用C函数的实现
- lua笔记 --- 一个C++调用lua函数的类的实现
- xkx mushclient 全自动walk lua实现
- lua单链表实现
- 【COCOS2DX-LUA 脚本开发之八】使用quick-cocos2d-x或cocos2dx-LuaProxy的Lua框架实现Lua-Http网络交互!
- Lua的类实现继承、多态以及setmetatable方法
- lua实现A星最短路径算法
- lua实现面向对象的特性
- 八皇后单解和全解递归算法(Lua实现)
- 解释器模式lua实现
- Redis+lua进行类似秒杀的实现
- 函数项目【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)函数项目
- C/C++用状态转移表联合函数指针数组实现状态机FSM
- 在 Lua 中实现面向对象
- cocos2dx-lua在ios上实现生成及扫描二维码
- [通用技术]在不同语言中用协程实现全排列算法(C++/Lua/Python/C#)
- Lua中类的实现
- 在LUA中使用GETTEXT实现多语言支持