-
Notifications
You must be signed in to change notification settings - Fork 2
/
hello_world.cr
144 lines (112 loc) · 3 KB
/
hello_world.cr
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require "../../src/entitas.cr"
CHANNEL = Channel(Char).new(1)
spoved_logger
@[Context(Game)]
class DebugMessage < Entitas::Component
prop :message, String, default: ""
def to_s(io)
io << message
end
end
# @[Context(Game, Window)]
# class Position < Entitas::Component
# prop :x, Int32, default: 0
# prop :y, Int32, default: 0
# end
class DebugMessageSystem < Entitas::ReactiveSystem
spoved_logger
def initialize(@context, @window)
super(@context)
end
def get_trigger(context : Entitas::Context) : Entitas::Collector(GameEntity)
context.create_collector(GameMatcher.debug_message)
end
def execute(entities : Array(Entitas::IEntity))
entities.each do |e|
self.logger.error { e.debug_message }
end
end
end
class HelloWorldSystem
include Entitas::Systems::InitializeSystem
getter context : GameContext
def initialize(@context : GameContext); end
def init; end
end
class InputSystem
spoved_logger
include Entitas::Systems::ExecuteSystem
getter context : GameContext
def initialize(@context : GameContext); end
def execute
logger.warn { "execute" }
char = CHANNEL.receive
case char
when '\u{4}', '\u{3}', '\e'
logger.warn { "Exiting app : #{char.inspect}" }
exit
else
# logger.unknown context.component_pools.inspect
logger.trace { context.entities.size }
context.create_entity.add_debug_message(message: char.inspect)
logger.trace { context.entities.size }
end
end
end
class CleanupSystem
spoved_logger
include Entitas::Systems::CleanupSystem
getter context : GameContext
getter debug_group : Entitas::Group(GameEntity)
def initialize(@context : GameContext)
@debug_group = context.get_group(GameMatcher.debug_message)
end
def cleanup
logger.warn { "cleanup" }
debug_group.get_entities.each do |e|
e.destroy!
end
end
end
# feature "tutorial", DebugMessageSystem, HelloWorldSystem
class TutorialSystems < Entitas::Feature
def initialize(contexts : Contexts)
@name = "Tutorial System"
ctx = contexts.game
add ::HelloWorldSystem.new(ctx)
add ::InputSystem.new(ctx)
add ::DebugMessageSystem.new(ctx)
add ::CleanupSystem.new(ctx)
end
end
class HelloWorld
getter systems : Entitas::Systems = Entitas::Systems.new
def start
# get a reference to the contexts
contexts = Contexts.shared_instance
# create the systems by creating individual features
@systems = Entitas::Feature.new("systems")
.add(TutorialSystems.new(contexts))
end
def update
# call execute on all the ExecuteSystems and
# ReactiveSystems that were triggered last frame
systems.execute
# call cleanup on all the CleanupSystems
systems.cleanup
end
end
hw = HelloWorld.new
hw.start
# game_ctx = Contexts.shared_instance.game
# e = game_ctx.create_entity.add_debug_message(message: "Hello World")
STDIN.blocking = false
spawn do
['e', 'f', 'w', '\e'].each do |char|
CHANNEL.send(char)
end
end
loop do
Fiber.yield
hw.update
end