Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. //
  2. // sandbox.swift
  3. // NewStar
  4. //
  5. // Created by schala1 on 2/25/15.
  6. // Copyright (c) 2015 StartStop. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import Darwin
  11.  
  12.  
  13. class test
  14. {
  15. var val = 0
  16. init()
  17. {
  18.  
  19. }
  20.  
  21. func incVal(var v: Int = 1)
  22. {
  23. self.val += v
  24. }
  25.  
  26. func devVal(var v: Int = 1)
  27. {
  28. self.val -= v
  29. }
  30.  
  31. func getVal() -> Int
  32. {
  33. return self.val
  34. }
  35. }
  36.  
  37. class readyState : GState
  38. {
  39. var n : Int = 0;
  40.  
  41. override init()
  42. {
  43. println("Entering ")
  44. }
  45.  
  46. override func entryCb<T>(fsmCtx: GenericFSM<T>, dt: CFTimeInterval)
  47. {
  48. println("readyState::entryCb")
  49. }
  50.  
  51. override func updateCb<T>(fsmCtx: GenericFSM<T>, dt: CFTimeInterval)
  52. {
  53. while(fsmCtx.nEvents() > 0)
  54. {
  55. var event = fsmCtx.popEvent()
  56.  
  57. if(event.getEventBaseType() != EventType.Empty)
  58. {
  59. if(event.getEventBaseType() == EventType.Time)
  60. {
  61. self.n += 1
  62.  
  63. println("readyState::updateCb = \(n)");
  64.  
  65. if(self.n % 5 == 0)
  66. {
  67. fsmCtx.setState("printState")
  68. }
  69. }
  70. }
  71. // TODO not sure but finish this
  72. }
  73.  
  74. if(self.age > 5.0)
  75. {
  76.  
  77. fsmCtx.addEvent(SSTimeDeltaReached())
  78. }
  79. }
  80.  
  81. override func exitCb<T>(fsmCtx: GenericFSM<T>, dt: CFTimeInterval)
  82. {
  83. println("readyState::exitCb")
  84. }
  85. }
  86.  
  87. class printState : GState
  88. {
  89. override init()
  90. {
  91. println("Entering printState::init")
  92. }
  93.  
  94. override func entryCb<T>(fsmCtx: GenericFSM<T>, dt: CFTimeInterval)
  95. {
  96. println("printState::entryCb")
  97. }
  98.  
  99. override func updateCb<T>(fsmCtx: GenericFSM<T>, dt: CFTimeInterval)
  100. {
  101. var ctxObj = fsmCtx.m_ctx as test
  102.  
  103. ctxObj.incVal()
  104.  
  105. println("printState::updateCb = \(ctxObj.getVal())")
  106.  
  107. // We have printed the value, no move back to the ready state
  108. fsmCtx.setState("readyState")
  109. }
  110.  
  111. override func exitCb<T>(fsmCtx: GenericFSM<T>, dt: CFTimeInterval)
  112. {
  113. println("printState::exitCb")
  114. }
  115. }
  116.  
  117. class sandbox
  118. {
  119. var ctx = test()
  120. var fsm = GenericFSM<test>()
  121.  
  122. init()
  123. {
  124. println("sandbox init")
  125.  
  126. fsm.setContext(&ctx)
  127. fsm.addState("readyState", stateProtocol: readyState())
  128. fsm.setState("readyState")
  129.  
  130. fsm.addState("printState", stateProtocol: printState())
  131.  
  132. println("time = \(CFAbsoluteTimeGetCurrent())")
  133. }
  134.  
  135. func update(currentTime: CFTimeInterval)
  136. {
  137. fsm.update(currentTime)
  138. sleep(1)
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement