teien 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. data/README.md +7 -5
  2. data/bin/teien +36 -37
  3. data/lib/teien/{smooth_mover.rb → action_model/smooth_moving.rb} +19 -3
  4. data/lib/teien/actor/actor.rb +21 -0
  5. data/{sample/actor/app/helpers/user_event.rb → lib/teien/actor/actor_event.rb} +41 -32
  6. data/lib/teien/actor/actor_factory.rb +20 -0
  7. data/lib/teien/actor/actor_info.rb +7 -0
  8. data/lib/teien/actor/actor_manager.rb +31 -0
  9. data/lib/teien/actor/actor_manager_proxy.rb +72 -0
  10. data/lib/teien/addon/addon.rb +25 -0
  11. data/lib/teien/{animation.rb → animation/animation.rb} +1 -0
  12. data/lib/teien/animation/animation_event.rb +17 -0
  13. data/lib/teien/animation/animation_manager.rb +23 -0
  14. data/lib/teien/animation/animation_manager_proxy.rb +27 -0
  15. data/lib/teien/application/application.rb +43 -0
  16. data/lib/teien/base_object/attachment_info.rb +15 -0
  17. data/lib/teien/{garden_object.rb → base_object/base_object.rb} +43 -52
  18. data/lib/teien/{event.rb → base_object/base_object_event.rb} +9 -12
  19. data/lib/teien/base_object/base_object_manager.rb +57 -0
  20. data/lib/teien/{garden_base.rb → base_object/base_object_manager_base.rb} +22 -31
  21. data/lib/teien/{proxy_garden.rb → base_object/base_object_manager_proxy.rb} +20 -62
  22. data/lib/teien/base_object/object_info.rb +21 -0
  23. data/lib/teien/{physics.rb → base_object/physics.rb} +4 -5
  24. data/lib/teien/{physics_info.rb → base_object/physics_info.rb} +1 -0
  25. data/lib/teien/base_object/physics_object_factory.rb +23 -0
  26. data/lib/teien/base_object/std_objects/box_object_info.rb +42 -0
  27. data/lib/teien/base_object/std_objects/capsule_object_info.rb +44 -0
  28. data/lib/teien/base_object/std_objects/cone_object_info.rb +41 -0
  29. data/lib/teien/base_object/std_objects/cylinder_object_info.rb +44 -0
  30. data/lib/teien/base_object/std_objects/floor_object_info.rb +45 -0
  31. data/lib/teien/base_object/std_objects/light_object_info.rb +40 -0
  32. data/lib/teien/base_object/std_objects/mesh_bb_object_info.rb +42 -0
  33. data/lib/teien/base_object/std_objects/mesh_object_info.rb +62 -0
  34. data/lib/teien/base_object/std_objects/sphere_object_info.rb +38 -0
  35. data/lib/teien/commands/browser_command.rb +46 -0
  36. data/lib/teien/commands/local_command.rb +21 -0
  37. data/lib/teien/commands/server_command.rb +39 -0
  38. data/lib/teien/commands/std_command.rb +71 -0
  39. data/lib/teien/core/client_network.rb +26 -0
  40. data/lib/teien/core/component_manager.rb +11 -0
  41. data/lib/teien/core/core_event.rb +7 -0
  42. data/lib/teien/core/dispatcher.rb +30 -0
  43. data/lib/teien/core/event_router.rb +116 -0
  44. data/lib/teien/core/launcher.rb +31 -0
  45. data/lib/teien/core/network.rb +28 -0
  46. data/lib/teien/{remote_info.rb → core/remote_info.rb} +6 -1
  47. data/lib/teien/core/server_network.rb +23 -0
  48. data/lib/teien/{tools.rb → core/tools.rb} +2 -2
  49. data/lib/teien/{camera.rb → ui/camera.rb} +1 -1
  50. data/lib/teien/{camera_mover.rb → ui/camera_mover.rb} +1 -2
  51. data/lib/teien/ui/std_objects/box_object_info.rb +29 -0
  52. data/lib/teien/ui/std_objects/capsule_object_info.rb +28 -0
  53. data/lib/teien/ui/std_objects/cone_object_info.rb +25 -0
  54. data/lib/teien/ui/std_objects/cylinder_object_info.rb +26 -0
  55. data/lib/teien/ui/std_objects/floor_object_info.rb +34 -0
  56. data/lib/teien/ui/std_objects/light_object_info.rb +23 -0
  57. data/lib/teien/ui/std_objects/mesh_bb_object_info.rb +24 -0
  58. data/lib/teien/ui/std_objects/mesh_object_info.rb +24 -0
  59. data/lib/teien/ui/std_objects/sphere_object_info.rb +26 -0
  60. data/lib/teien/{user_interface.rb → ui/user_interface.rb} +18 -13
  61. data/lib/teien/{view.rb → ui/view.rb} +42 -20
  62. data/lib/teien/ui/view_object.rb +142 -0
  63. data/lib/teien/ui/view_object_factory.rb +23 -0
  64. data/lib/teien/version.rb +1 -1
  65. data/lib/teien.rb +3 -78
  66. data/mods/actor/addons/sinbad/lib/sinbad/sinbad_info.rb +16 -0
  67. data/{sample/actor/app/helpers → mods/actor/addons/sinbad/lib}/sinbad/sinbad_state.rb +14 -39
  68. data/mods/actor/addons/sinbad/lib/sinbad.rb +180 -0
  69. data/mods/actor/apps/browser/browser_application.rb +185 -0
  70. data/mods/actor/apps/common/browser_event.rb +68 -0
  71. data/mods/actor/apps/server/server_application.rb +101 -0
  72. data/{sample/actor/app/ais → mods/actor/commands/ai}/actor_ai.rb +8 -3
  73. data/mods/actor/commands/ai_command.rb +20 -0
  74. data/{sample/actor/config → mods/actor/configs}/plugins.cfg +4 -4
  75. data/{sample/hello_garden/app/controllers/hello_garden_controller.rb → mods/hello_garden/apps/browser/browser_application.rb} +48 -25
  76. data/mods/hello_garden/apps/common/browser_event.rb +58 -0
  77. data/{sample/hello_garden/app/models/hello_garden_model.rb → mods/hello_garden/apps/server/server_application.rb} +35 -26
  78. data/teien.gemspec +3 -1
  79. metadata +114 -55
  80. data/lib/teien/ai.rb +0 -27
  81. data/lib/teien/controller.rb +0 -30
  82. data/lib/teien/dispatcher.rb +0 -22
  83. data/lib/teien/event_router.rb +0 -118
  84. data/lib/teien/garden.rb +0 -91
  85. data/lib/teien/model.rb +0 -34
  86. data/lib/teien/network.rb +0 -108
  87. data/lib/teien/object_info.rb +0 -192
  88. data/lib/teien/physics_object_factory.rb +0 -170
  89. data/lib/teien/synchronizer.rb +0 -26
  90. data/lib/teien/view_object.rb +0 -67
  91. data/lib/teien/view_object_factory.rb +0 -210
  92. data/sample/actor/app/controllers/actor_controller.rb +0 -182
  93. data/sample/actor/app/helpers/sinbad/sinbad.rb +0 -176
  94. data/sample/actor/app/models/actor_model.rb +0 -108
  95. data/sample/hello_garden/app/helpers/user_event.rb +0 -12
  96. /data/lib/teien/{animation_operator.rb → animation/animation_operator.rb} +0 -0
  97. /data/lib/teien/{physics_object.rb → base_object/physics_object.rb} +0 -0
  98. /data/lib/teien/{sky_dome.rb → base_object/sky_dome.rb} +0 -0
  99. /data/lib/teien/{ui_listener.rb → ui/ui_listener.rb} +0 -0
  100. /data/{sample/actor/config → mods/actor/configs}/resources.cfg +0 -0
  101. /data/{sample/hello_garden/config → mods/hello_garden/configs}/plugins.cfg +0 -0
  102. /data/{sample/hello_garden/config → mods/hello_garden/configs}/resources.cfg +0 -0
  103. /data/{sample → mods}/hello_garden/run +0 -0
@@ -0,0 +1,41 @@
1
+ require_relative "../physics_object_factory"
2
+
3
+ module Teien
4
+
5
+ class ConeObjectInfo < ObjectInfo
6
+ attr_accessor :radius
7
+ attr_accessor :height
8
+ attr_accessor :num_seg_base
9
+ attr_accessor :num_seg_height
10
+ attr_accessor :u_tile
11
+ attr_accessor :v_tile
12
+ attr_accessor :material_name
13
+
14
+ def initialize(radius, height, num_seg_base = 16, num_seg_height = 1, u_tile = 1.0, v_tile = 1.0)
15
+ super()
16
+ @radius = radius
17
+ @height = height
18
+ @num_seg_base = num_seg_base
19
+ @num_seg_height = num_seg_height
20
+ @u_tile = u_tile
21
+ @v_tile = v_tile
22
+ @material_name = nil
23
+ end
24
+
25
+ def self.create_physics_object(obj, physics)
26
+ physics_object = PhysicsObject.new(physics)
27
+ cShape = Bullet::BtConeShape.new(obj.object_info.radius, obj.object_info.height)
28
+ inertia = Bullet::BtVector3.new()
29
+ cShape.calculate_local_inertia(obj.physics_info.mass, inertia)
30
+ physics_object.set_rigid_body(obj, cShape, inertia,
31
+ Vector3D.new(0, obj.object_info.height / 2, 0))
32
+
33
+ obj.object_info.use_physics = true
34
+
35
+ return physics_object
36
+ end
37
+
38
+ PhysicsObjectFactory::set_creator(self, self.method(:create_physics_object))
39
+ end
40
+
41
+ end
@@ -0,0 +1,44 @@
1
+ require_relative "../physics_object_factory"
2
+
3
+ module Teien
4
+
5
+ class CylinderObjectInfo < ObjectInfo
6
+ attr_accessor :radius
7
+ attr_accessor :height
8
+ attr_accessor :capped
9
+ attr_accessor :num_seg_base
10
+ attr_accessor :num_seg_height
11
+ attr_accessor :u_tile
12
+ attr_accessor :v_tile
13
+ attr_accessor :material_name
14
+
15
+ def initialize(radius, height, capped = true, num_seg_base = 16, num_seg_height = 1, u_tile = 1.0, v_tile = 1.0)
16
+ super()
17
+ @radius = radius
18
+ @height = height
19
+ @capped = capped
20
+ @num_seg_base = num_seg_base
21
+ @num_seg_height = num_seg_height
22
+ @u_tile = u_tile
23
+ @v_tile = v_tile
24
+ @material_name = nil
25
+ end
26
+
27
+ def self.create_physics_object(obj, physics)
28
+ physics_object = PhysicsObject.new(physics)
29
+ cShape = Bullet::BtCylinderShape.new(Bullet::BtVector3.new(obj.object_info.radius,
30
+ obj.object_info.height / 2,
31
+ obj.object_info.radius))
32
+ inertia = Bullet::BtVector3.new()
33
+ cShape.calculate_local_inertia(obj.physics_info.mass, inertia)
34
+ physics_object.set_rigid_body(obj, cShape, inertia)
35
+
36
+ obj.object_info.use_physics = true
37
+
38
+ return physics_object
39
+ end
40
+
41
+ PhysicsObjectFactory::set_creator(self, self.method(:create_physics_object))
42
+ end
43
+
44
+ end
@@ -0,0 +1,45 @@
1
+ require_relative "../physics_object_factory"
2
+
3
+ module Teien
4
+
5
+ class FloorObjectInfo < ObjectInfo
6
+ attr_accessor :width
7
+ attr_accessor :height
8
+ attr_accessor :depth
9
+ attr_accessor :num_seg_x
10
+ attr_accessor :num_seg_y
11
+ attr_accessor :u_tile
12
+ attr_accessor :v_tile
13
+ attr_accessor :material_name
14
+
15
+ def initialize(width, height, depth = 0.5, num_seg_x = 1, num_seg_y = 1, u_tile = 1.0, v_tile = 1.0)
16
+ super()
17
+ @width = width
18
+ @height = height
19
+ @depth = depth
20
+ @num_seg_x = num_seg_x
21
+ @num_seg_y = num_seg_y
22
+ @u_tile = u_tile
23
+ @v_tile = v_tile
24
+ @material_name = nil
25
+ end
26
+
27
+ # Setting a collision shape and a rigid body of Bullet.
28
+ def self.create_physics_object(obj, physics)
29
+ physics_object = PhysicsObject.new(physics)
30
+ cShape = Bullet::BtBoxShape.new(Vector3D.new(obj.object_info.width,
31
+ obj.object_info.depth,
32
+ obj.object_info.height))
33
+ physics_object.set_rigid_body(obj, cShape,
34
+ Vector3D.new(0, 0, 0),
35
+ Vector3D.new(0.0, -obj.object_info.depth, 0.0))
36
+
37
+ obj.object_info.use_physics = true
38
+
39
+ return physics_object
40
+ end
41
+
42
+ PhysicsObjectFactory::set_creator(self, self.method(:create_physics_object))
43
+ end
44
+
45
+ end
@@ -0,0 +1,40 @@
1
+ require_relative "../physics_object_factory"
2
+
3
+ module Teien
4
+
5
+ class LightObjectInfo < ObjectInfo
6
+ POINT = Ogre::Light::LT_POINT
7
+ DIRECTIONAL = Ogre::Light::LT_DIRECTIONAL
8
+ SPOTLIGHT = Ogre::Light::LT_SPOTLIGHT
9
+
10
+ attr_accessor :type
11
+ attr_accessor :diffuse_color
12
+ attr_accessor :specular_color
13
+ attr_accessor :direction
14
+
15
+ def initialize(type,
16
+ diffuse_color = Color.new(1.0, 1.0, 1.0),
17
+ specular_color = Color.new(0.25, 0.25, 0),
18
+ direction = Vector3D.new( -1, -1, -1 ))
19
+ super()
20
+ @usePhysics = false
21
+ @type = type
22
+ @diffuse_color = diffuse_color
23
+ @specular_color = specular_color
24
+ @direction = direction
25
+ end
26
+
27
+ def self.create_physics_object(obj, physics)
28
+ physics_object = PhysicsObject.new(physics)
29
+ cShape = Bullet::BtSphereShape.new(0.1)
30
+ inertia = Bullet::BtVector3.new()
31
+ cShape.calculate_local_inertia(0, inertia)
32
+ obj.physics_info = PhysicsInfo.new(0)
33
+ physics_object.set_rigid_body(obj, cShape, inertia)
34
+ return physics_object
35
+ end
36
+
37
+ PhysicsObjectFactory::set_creator(self, self.method(:create_physics_object))
38
+ end
39
+
40
+ end
@@ -0,0 +1,42 @@
1
+ require_relative "../physics_object_factory"
2
+
3
+ module Teien
4
+
5
+ class MeshBBObjectInfo < ObjectInfo
6
+ attr_accessor :mesh_path
7
+ attr_accessor :size # Loading mesh only: bounding box size
8
+ attr_accessor :scale # Loading mesh only: scales mesh
9
+ attr_accessor :view_offset # Loading mesh only: offset of Mesh
10
+ attr_accessor :view_rotation # Loading mesh only: rotation offset of Mesh
11
+ attr_accessor :physics_offset # Loading mesh only: offset of collision Box
12
+ attr_accessor :material_name
13
+
14
+ def initialize(mesh_path, size, scale = Vector3D.new(1, 1, 1),
15
+ view_offset = Vector3D.new(0, 0, 0),
16
+ view_rotation = Quaternion.new(0, 0, 0, 1.0),
17
+ physics_offset = Vector3D.new(0, 0, 0))
18
+ super()
19
+ @size = size
20
+ @mesh_path = mesh_path
21
+ @scale = scale
22
+ @view_offset = view_offset
23
+ @view_rotation = view_rotation
24
+ @physics_offset = physics_offset
25
+ @material_name = nil
26
+ end
27
+
28
+ def self.create_physics_object(obj, physics)
29
+ physics_object = PhysicsObject.new(physics)
30
+ cShape = Bullet::BtBoxShape.new(obj.object_info.size)
31
+ inertia = Bullet::BtVector3.new()
32
+ cShape.calculate_local_inertia(obj.physics_info.mass, inertia)
33
+ physics_object.set_rigid_body(obj, cShape, inertia, obj.object_info.physics_offset)
34
+
35
+ obj.object_info.use_physics = true
36
+ return physics_object
37
+ end
38
+
39
+ PhysicsObjectFactory::set_creator(self, self.method(:create_physics_object))
40
+ end
41
+
42
+ end
@@ -0,0 +1,62 @@
1
+ require_relative "../physics_object_factory"
2
+
3
+ module Teien
4
+
5
+ class MeshObjectInfo < ObjectInfo
6
+ attr_accessor :mesh_path
7
+ attr_accessor :scale # Loading mesh only: scales mesh
8
+ attr_accessor :view_offset # Loading mesh only: offset of Mesh
9
+ attr_accessor :view_rotation # Loading mesh only: rotation offset of Mesh
10
+ attr_accessor :physics_offset # Loading mesh only: offset of collision Box
11
+ attr_accessor :material_name
12
+
13
+ def initialize(mesh_path, scale = Vector3D.new(1, 1, 1),
14
+ view_offset = Vector3D.new(0, 0, 0),
15
+ view_rotation = Quaternion.new(0, 0, 0, 1.0),
16
+ physics_offset = Vector3D.new(0, 0, 0))
17
+ super()
18
+ @mesh_path = mesh_path
19
+ @scale = scale
20
+ @view_offset = view_offset
21
+ @view_rotation = view_rotation
22
+ @physics_offset = physics_offset
23
+ @material_name = nil
24
+ end
25
+
26
+ # This method needs the obj.entity to create a collision shape.
27
+ # So it's not support to use this method on server(physics only) currently.
28
+ def self.create_physics_object(obj, physics)
29
+ physics_object = PhysicsObject.new(physics)
30
+
31
+ if obj.physics_info
32
+ strider = Teienlib::MeshStrider.new(obj.entity.get_mesh().get())
33
+ cShape = Bullet::BtGImpactMeshShape.new(strider)
34
+ cShape.set_local_scaling(Bullet::BtVector3.new(obj.object_info.scale.x,
35
+ obj.object_info.scale.y,
36
+ obj.object_info.scale.z))
37
+ cShape.instance_variable_set(:@strider, strider) # prevent this from GC.
38
+ cShape.post_update()
39
+ cShape.update_bound()
40
+ inertia = Bullet::BtVector3.new()
41
+ cShape.calculate_local_inertia(obj.physics_info.mass, inertia)
42
+ physics_object.set_rigid_body(obj, cShape, inertia,
43
+ obj.object_info.physics_offset)
44
+ obj.object_info.use_physics = true
45
+ else
46
+ # use a rigid body as a position holder only(not as collision shape).
47
+ cShape = Bullet::BtSphereShape.new(0.1)
48
+ inertia = Bullet::BtVector3.new()
49
+ cShape.calculate_local_inertia(0, inertia)
50
+ obj.physics_info = PhysicsInfo.new(0) # dummy to make a rigid body.
51
+ physics_object.set_rigid_body(obj, cShape, inertia)
52
+ obj.physics_info = nil
53
+ obj.object_info.use_physics = false
54
+ end
55
+
56
+ return physics_object
57
+ end
58
+
59
+ PhysicsObjectFactory::set_creator(self, self.method(:create_physics_object))
60
+ end
61
+
62
+ end
@@ -0,0 +1,38 @@
1
+ require_relative "../physics_object_factory"
2
+
3
+ module Teien
4
+
5
+ class SphereObjectInfo < ObjectInfo
6
+ attr_accessor :radius
7
+ attr_accessor :num_rings
8
+ attr_accessor :num_segments
9
+ attr_accessor :u_tile
10
+ attr_accessor :v_tile
11
+ attr_accessor :material_name
12
+
13
+ def initialize(radius, num_rings = 16, num_segments = 16, u_tile = 1.0, v_tile = 1.0)
14
+ super()
15
+ @radius = radius
16
+ @num_rings = num_rings
17
+ @num_segments = num_segments
18
+ @u_tile = u_tile
19
+ @v_tile = v_tile
20
+ @material_name = nil
21
+ end
22
+
23
+ def self.create_physics_object(obj, physics)
24
+ physics_object = PhysicsObject.new(physics)
25
+ cShape = Bullet::BtSphereShape.new(obj.object_info.radius)
26
+ inertia = Bullet::BtVector3.new()
27
+ cShape.calculate_local_inertia(obj.physics_info.mass, inertia)
28
+ physics_object.set_rigid_body(obj, cShape, inertia)
29
+
30
+ obj.object_info.use_physics = true
31
+
32
+ return physics_object
33
+ end
34
+
35
+ PhysicsObjectFactory::set_creator(self, self.method(:create_physics_object))
36
+ end
37
+
38
+ end
@@ -0,0 +1,46 @@
1
+ require "teien/core/launcher"
2
+
3
+ module Teien
4
+
5
+ class BrowserCommand
6
+ def self.browser_command(argv)
7
+ ip = argv[1] ? argv[1] : "0.0.0.0"
8
+ port = argv[2] ? argv[2].to_i : 11922
9
+
10
+ start_browser(ip, port)
11
+ end
12
+
13
+ def self.start_browser(ip, port)
14
+ event_router = Teien::EventRouter.new()
15
+ Teien::register_component("event_router", event_router)
16
+
17
+ require "teien/base_object/base_object_manager_proxy"
18
+ base_object_manager = Teien::BaseObjectManagerProxy.new(event_router)
19
+ Teien::register_component("base_object_manager", base_object_manager)
20
+
21
+ require 'teien/animation/animation_manager_proxy'
22
+ animation_manager = Teien::AnimationManagerProxy.new(event_router)
23
+ Teien::register_component("animation_manager", animation_manager)
24
+
25
+ require 'teien/actor/actor_manager_proxy'
26
+ actor_manager = Teien::ActorManagerProxy.new()
27
+ Teien::register_component("actor_manager", actor_manager)
28
+
29
+ require 'teien/ui/user_interface'
30
+ ui = Teien::UserInterface.new(event_router, base_object_manager)
31
+ Teien::register_component("user_interface", ui )
32
+
33
+ require 'teien/addon/addon'
34
+ Teien::Addon::load()
35
+
36
+ require 'teien/application/application'
37
+ Teien::Application::load("browser")
38
+ Teien::Application::instantiate()
39
+
40
+ event_router.connect_to_server(ip, port)
41
+ end
42
+
43
+ Launcher::register_command("browser", BrowserCommand.method(:browser_command))
44
+ end
45
+
46
+ end
@@ -0,0 +1,21 @@
1
+ require "teien/core/launcher"
2
+
3
+ module Teien
4
+
5
+ class LocalCommand
6
+ def self.local_command(argv)
7
+ pid = Process.fork {
8
+ ServerCommand::start_server("0.0.0.0", 11922, 0.1)
9
+ }
10
+ begin
11
+ BrowserCommand::start_browser("0.0.0.0", 11922)
12
+ ensure
13
+ Process.kill("TERM", pid)
14
+ end
15
+
16
+ end
17
+
18
+ Launcher::register_command("local", LocalCommand.method(:local_command))
19
+ end
20
+
21
+ end
@@ -0,0 +1,39 @@
1
+ require "teien/core/launcher"
2
+
3
+ module Teien
4
+
5
+ class ServerCommand
6
+ def self.server_command(argv)
7
+ start_server("0.0.0.0", 11922)
8
+ end
9
+
10
+ def self.start_server(ip, port, sync_period = 0.3)
11
+ event_router = Teien::EventRouter.new()
12
+ Teien::register_component("event_router", event_router)
13
+
14
+ require "teien/base_object/base_object_manager"
15
+ base_object_manager = Teien::BaseObjectManager.new(event_router, sync_period)
16
+ Teien::register_component("base_object_manager", base_object_manager)
17
+
18
+ require 'teien/animation/animation_manager'
19
+ animation_manager = Teien::AnimationManager.new(event_router)
20
+ Teien::register_component("animation_manager", animation_manager)
21
+
22
+ require 'teien/actor/actor_manager'
23
+ actor_manager = Teien::ActorManager.new()
24
+ Teien::register_component("actor_manager", actor_manager)
25
+
26
+ require 'teien/addon/addon'
27
+ Teien::Addon::load()
28
+
29
+ require 'teien/application/application'
30
+ Teien::Application::load("server")
31
+ Teien::Application::instantiate()
32
+
33
+ event_router.start_server(ip, port)
34
+ end
35
+
36
+ Launcher::register_command("server", ServerCommand.method(:server_command))
37
+ end
38
+
39
+ end
@@ -0,0 +1,71 @@
1
+ module Teien
2
+
3
+ class StdCommand
4
+ def self.client_command(argv)
5
+ ip = argv[1] ? argv[1] : "0.0.0.0"
6
+ port = argv[2] ? argv[2].to_i : 11922
7
+ start_client_garden(ip, port)
8
+ end
9
+
10
+ def self.local_command(argv)
11
+ start_local_garden()
12
+ end
13
+
14
+ def self.alone_command(argv)
15
+ start_alone_garden()
16
+ end
17
+
18
+ def self.start_client_garden(ip, port)
19
+ require "teien/proxy_garden.rb"
20
+ require 'teien/user_interface'
21
+ Dir.glob("#{Dir.getwd}/app/controllers/*.rb") {|file|
22
+ require "#{file}"
23
+ }
24
+ event_router = Teien::EventRouter.new()
25
+ garden = Teien::ProxyGarden.new(event_router)
26
+ ui = Teien::UserInterface.new(event_router, garden)
27
+ Teien::Controller.load(event_router, garden)
28
+
29
+ Teien::register_component("event_router", event_router)
30
+ Teien::register_component("garden", garden)
31
+ Teien::register_component("ui", ui )
32
+ event_router.connect_to_server(ip, port)
33
+ end
34
+
35
+ def self.start_local_garden()
36
+ require 'teien/user_interface'
37
+
38
+ pid = Process.fork {
39
+ # start_server_garden("0.0.0.0", 11922, 10)
40
+ start_server_garden("0.0.0.0", 11922, 0.1)
41
+ }
42
+ begin
43
+ start_client_garden("0.0.0.0", 11922)
44
+ ensure
45
+ Process.kill("TERM", pid)
46
+ end
47
+ end
48
+
49
+ def self.start_alone_garden()
50
+ require "teien/garden.rb"
51
+ require 'teien/user_interface'
52
+ Dir.glob("#{Dir.getwd}/app/models/*.rb") {|file| require "#{file}" }
53
+ Dir.glob("#{Dir.getwd}/app/controllers/*.rb") {|file| require "#{file}" }
54
+
55
+ event_router = Teien::EventRouter.new()
56
+ garden = Teien::Garden.new(event_router)
57
+ ui =Teien::UserInterface.new(event_router, garden)
58
+ Teien::Model.load(event_router, garden)
59
+ Teien::Controller.load(event_router, garden)
60
+
61
+ Teien::register_component("event_router", event_router)
62
+ Teien::register_component("garden", garden)
63
+ Teien::register_component("ui", ui)
64
+ event_router.start_server()
65
+ end
66
+
67
+ Launcher::set_command("local", StdCommand.method(:local_command))
68
+ Launcher::set_command("alone", StdCommand.method(:alone_command))
69
+ end
70
+
71
+ end
@@ -0,0 +1,26 @@
1
+ require 'teien/core/network'
2
+
3
+ module Teien
4
+
5
+ class ClientNetwork < Teien::Network
6
+ def post_init
7
+ end
8
+
9
+ def connection_completed
10
+ puts "The connection is completed."
11
+ @@connections[self] = RemoteInfo.new(self)
12
+ @@event_router.connection_completed(self)
13
+ end
14
+
15
+ def receive_object(obj)
16
+ @@event_router.receive_event(obj, self)
17
+ end
18
+
19
+ def unbind
20
+ puts "The connection is closed."
21
+ @@event_router.connection_unbinded(self)
22
+ @@connections.delete(self)
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,11 @@
1
+ module Teien
2
+ @@components = Hash.new()
3
+
4
+ def self.register_component(name, component)
5
+ @@components[name] = component
6
+ end
7
+
8
+ def self.get_component(name)
9
+ return @@components[name]
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Teien
2
+ module Event
3
+ module Core
4
+
5
+ end
6
+ end
7
+ end # module Event
@@ -0,0 +1,30 @@
1
+ module Teien
2
+
3
+ module Dispatcher
4
+ def initialize(*args, &block)
5
+ super
6
+ @dispatch_receivers = Array.new
7
+ end
8
+
9
+ def register_receiver(recv)
10
+ @dispatch_receivers.push(recv)
11
+ end
12
+
13
+ def notify(method, *argv)
14
+ @dispatch_receivers.each {|recv|
15
+ if (recv.respond_to?(method))
16
+ recv.method(method).call(*argv)
17
+ end
18
+ }
19
+ end
20
+
21
+ def notify_reversely(method, *argv)
22
+ @dispatch_receivers.reverse_each {|recv|
23
+ if (recv.respond_to?(method))
24
+ recv.method(method).call(*argv)
25
+ end
26
+ }
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,116 @@
1
+ require "eventmachine"
2
+ require "teien/core/dispatcher"
3
+ require "teien/core/server_network"
4
+ require "teien/core/client_network"
5
+
6
+ module Teien
7
+
8
+ class EventRouter
9
+ include Dispatcher
10
+
11
+ attr_accessor :quit
12
+
13
+ def initialize()
14
+ super()
15
+ @quit = false
16
+ end
17
+
18
+ def connection_binded(from)
19
+ notify(:connection_binded, from)
20
+ end
21
+
22
+ def connection_completed(from)
23
+ notify(:connection_completed, from)
24
+ end
25
+
26
+ def connection_unbinded(from)
27
+ notify(:connection_unbinded, from)
28
+ end
29
+
30
+ def receive_event(event, from)
31
+ notify(:receive_event, event, from)
32
+ end
33
+
34
+ # send a event to receivers and connections.
35
+ # to = nil means to send the event to all connections.
36
+ def send_event(event, to = nil)
37
+ if to
38
+ to.send_object(event)
39
+ else
40
+ Network::send_event_to_all(event)
41
+ end
42
+ notify(:receive_event, event, nil)
43
+ end
44
+
45
+ def start_server(ip = nil, port = 11922, tick_period = 0.001)
46
+ notify(:setup)
47
+
48
+ @last_time = 0
49
+ EM.run do
50
+ EM.add_periodic_timer(tick_period) do
51
+ if @last_time == 0
52
+ @last_time = Time.now.to_f
53
+ if ip == nil
54
+ # dummy connection for alone
55
+ Network::connections[nil] = RemoteInfo.new(nil)
56
+ notify(:connection_binded, nil)
57
+ notify(:connection_completed, nil)
58
+ end
59
+ else
60
+ now = Time.now.to_f
61
+ delta = now - @last_time
62
+ @last_time = now
63
+
64
+ notify(:update, delta)
65
+
66
+ if @quit
67
+ EM.stop
68
+ Teien::get_component("base_object_manager").finalize()
69
+ end
70
+ end
71
+ end
72
+
73
+ Signal.trap("INT") { EM.stop; Teien::get_component("base_object_manager").finalize() }
74
+ Signal.trap("TERM") { EM.stop; Teien::get_component("base_object_manager").finalize() }
75
+
76
+ if (ip)
77
+ EM.start_server(ip, port, ServerNetwork, self)
78
+ end
79
+ end
80
+ end
81
+
82
+ # def connect_to_server(ip = nil, port = 11922, tick_period = 0.001)
83
+ def connect_to_server(ip = nil, port = 11922, tick_period = 0)
84
+ notify(:setup)
85
+
86
+ @last_time = 0
87
+ EM.run do
88
+ EM.add_periodic_timer(tick_period) do
89
+ if @last_time == 0
90
+ @last_time = Time.now.to_f
91
+ else
92
+ now = Time.now.to_f
93
+ delta = now - @last_time
94
+ @last_time = now
95
+
96
+ notify(:update, delta)
97
+
98
+ if @quit
99
+ EM.stop
100
+ notify_reversely(:finalize)
101
+ # Teien::get_component("base_object_manager").finalize()
102
+ end
103
+ end
104
+ end
105
+
106
+ Signal.trap("INT") { EM.stop; notify_reversely(:finalize) }
107
+ Signal.trap("TERM") { EM.stop; notify_reversely(:finalize) }
108
+
109
+ if (ip)
110
+ EM.connect(ip, port, ClientNetwork, self)
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ end