This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 525
/
StateModule.lua
70 lines (59 loc) · 1.78 KB
/
StateModule.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
StateModule = CpObject()
function StateModule:init(debugClass,debugFunc, vehicle)
self.states = {}
self.debugClass = debugClass
self.debugFunc = debugFunc
self.vehicle = vehicle
end
--- Aggregation of states from this and all descendant classes
function StateModule:initStates(states)
for key, state in pairs(states) do
self.states[key] = {name = tostring(key), properties = state}
end
end
function StateModule:debug(lastState,newState)
if self.vehicle then
self.debugClass[self.debugFunc](self.debugClass,self.vehicle,"lastState: %s => newState: %s",lastState.name,newState.name)
else
self.debugClass[self.debugFunc](self.debugClass,"lastState: %s => newState: %s",lastState.name,newState.name)
end
end
function StateModule:changeState(newStateName)
local newState = self.states[newStateName]
if self.state ~= newState then
self:debug(self.state,newState)
self.state = newState
end
end
function StateModule:get()
return self.state
end
function StateModule:getName()
return self.state.name
end
function StateModule:is(state)
return self.state == self.states[state]
end
function StateModule:onReadStream(streamId)
local nameState = streamReadString(streamId)
self.state = self.states[nameState]
end
function StateModule:onWriteStream(streamId)
streamWriteString(streamId,self.state.name)
self.lastStateSent = self.state
end
function StateModule:onWriteUpdateStream(streamId)
if self.lastStateSent == nil or self.lastStateSent ~= self.state then
streamWriteBool(streamId,true)
streamWriteString(streamId,self.state.name)
self.lastStateSent = self.state
else
streamWriteBool(streamId,false)
end
end
function StateModule:onReadUpdateStream(streamId)
if streamReadBool(streamId) then
local nameState = streamReadString(streamId)
self.state = self.states[nameState]
end
end