-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.rb
229 lines (197 loc) · 5.48 KB
/
editor.rb
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
require 'gosu'
require 'game_ecs'
require 'awesome_print'
require 'fileutils'
# require 'pry'
require_relative 'core_ext'
require_relative 'gosu_ext'
require_relative 'vec'
require_relative 'components'
require_relative 'prefab'
require_relative 'systems/systems'
require_relative 'world'
require_relative 'input_cacher'
require_relative 'level'
require_relative 'scoreboard'
Q = GameEcs::Query
class Editor < Gosu::Window
MAX_UPDATE_SIZE_IN_MILLIS = 500
def initialize
super(1024,1024,false)
level_arg = ARGV[0] || "1"
if level_arg.start_with? "http"
level_name = level_arg.split('/').last
require 'open-uri'
FileUtils.mkdir_p 'custom_levels'
@custom_level = File.join('custom_levels', "#{level_name}")
IO.copy_stream(open(level_arg), @custom_level)
else
@level_number = level_arg.to_i - 1
end
@num_levels = Dir['./levels/level*.png'].size
@music_files = Dir['./music/*.mp3']
@input_cacher = InputCacher.new
@scoreboard = Scoreboard.new
@last_millis = Gosu::milliseconds.to_f
build_world
next_level
end
def needs_cursor?
false
end
def update
self.caption = "FPS: #{Gosu.fps} ENTS: #{@entity_store.num_entities}"
update_level!
delta = relative_delta
snapshot = take_input_snapshot
if snapshot.pressed?(Gosu::KbBacktick)
# TODO pull out start_editing code
camera = @editor_entity_store.first(Camera, Position)
@editor_entity_store.remove_entity(id: camera.id) if camera
camera = @entity_store.first(Camera, Position)
@editor_entity_store.add_entity(*camera.components)
level = @editor_entity_store.first(Level)
@editor_entity_store.remove_entity(id: level.id) if level
level = @entity_store.first(Level).get(Level)
@editor_entity_store.add_entity(level)
@editor = !@editor
end
if @editor
@last_update = @editor_world.update @editor_entity_store, delta, snapshot
else
@last_update = @world.update @entity_store, delta, snapshot
end
# TODO use last_update[:global_events] for something: like level changes?
end
def draw
@render_system.draw self, @entity_store
@render_system.draw self, @editor_entity_store if @editor
end
def draw_box(x1,y1,x2,y2,c,z)
draw_line x1, y1, c, x2, y1, c, z
draw_line x2, y1, c, x2, y2, c, z
draw_line x2, y2, c, x1, y2, c, z
draw_line x1, y2, c, x1, y1, c, z
end
def button_down(id)
@input_cacher.button_down id
end
def button_up(id)
@input_cacher.button_up id
end
private
def next_level
@music.stop if @music
if @custom_level.nil?
@level_number = @level_number += 1
@level_number = 1 if @level_number > @num_levels
@filename = File.join('levels', "level#{@level_number}.png")
else
@level_number = :custom
@filename = @custom_level
end
reset_level
avg_rgb = @level.map.average_color
hue = calc_hue(rgb: avg_rgb)
index = hue * @music_files.size / 360
file_name = @music_files[index.floor]
@music = Gosu::Song.new file_name
@music.volume = 0.1
@music.play true
end
def calc_hue(rgb:)
g = rgb.green / 255.0
r = rgb.red / 255.0
b = rgb.blue / 255.0
cmax = [r,g,b].max
cmin = [r,g,b].min
delta = cmax-cmin
hue = if cmax == r
(g-b)
elsif cmax == g
2 + (b-r)
else
4 + (r-g)
end
hue /= delta
hue *= 60
hue %= 360
hue
end
def reset_level
@level = Level.load(file_name: @filename,
number: @level_number,
high_scores: @scoreboard)
@entity_store.clear! if @entity_store
@level.reset! if @level
Prefab.level entity_store: @entity_store, level: @level
Prefab.camera entity_store: @entity_store, scale: 1, x: 512, y: 512
end
def build_world
@editor = false
@entity_store = GameEcs::EntityStore.new
@editor_entity_store = GameEcs::EntityStore.new
@editor_world = World.new [
EditorSystem.new(@entity_store)
]
@world = World.new [
InputMappingSystem.new,
CameraSystem.new,
MonsterSystem.new,
RainbowSystem.new,
FadingSystem.new,
TimerSystem.new,
TimedSystem.new,
TimedLevelSystem.new,
SoundSystem.new,
ParticlesEmitterSystem.new,
ParticlesSystem.new,
BackgroundSystem.new
]
@render_system = RenderSystem.new
end
def update_level!
if @level.complete?
update_scoreboard!
next_level
end
reset_level if @level.failed?
end
def update_scoreboard!
@scoreboard.completed_level level: @level, number: @level_number
end
def relative_delta
total_millis = Gosu.milliseconds.to_f
delta = total_millis
delta -= @last_millis if total_millis > @last_millis
@last_millis = total_millis
delta = MAX_UPDATE_SIZE_IN_MILLIS if delta > MAX_UPDATE_SIZE_IN_MILLIS
delta
end
def take_input_snapshot
total_millis = Gosu.milliseconds.to_f
mouse_pos = {x: mouse_x, y: mouse_y}
input_snapshot = @input_cacher.snapshot @last_snapshot, total_millis, mouse_pos
@last_snapshot = input_snapshot
input_snapshot
end
end
if $0 == __FILE__
# require 'ruby-prof'
#
# # Profile the code
# RubyProf.start
# at_exit do
# result = RubyProf.stop
#
# # Print a flat profile to text
# printer = RubyProf::FlatPrinter.new(result)
# printer.print(STDOUT)
#
# end
# require 'stackprof'
# StackProf.run(mode: :cpu, out: './stackprof-cpu-myapp.dump') do
$window = Editor.new
$window.show
end
# end