-
Notifications
You must be signed in to change notification settings - Fork 0
/
sokoban.rb
88 lines (76 loc) · 1.7 KB
/
sokoban.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
require 'yaml'
require 'ruby2d'
require './lib/character'
require './lib/edit'
require './lib/floor'
require './lib/splash'
require './lib/hallway'
require 'active_support'
require 'active_support/core_ext/hash/keys'
FLOORS = './floors'.freeze
SPRITES_PATH = "#{Dir.pwd}/assets/sprites.png".freeze
##
# Window
##
set title: 'Sokoban',
width: 1200,
height: 768,
resizable: false
floors = []
Dir.glob(File.join(FLOORS, '**', '*')).each do |file|
File.open(file) do |file|
floors << YAML.load(File.read(file).split.join(' ')).symbolize_keys
end
end
state = :splash
level = 0
floor = Floor.new(**floors[level])
hallway = Hallway.new
score = Text.new('', x: 10, y: get(:height) - 30, color: 'red')
splash = Splash.new
edit = Edit.new
splash.play animation: :walk
keys_pressed = []
on :key_down do |event|
case state
when :floor
floor.key_down(keys_pressed, event)
end
end
on :key_up do |event|
case state
when :splash
splash.key_up
state = :hallway
hallway.show
when :edit
edit.key_up(keys_pressed, event, state)
when :hallway
hallway.key_up(keys_pressed, event, state)
when :floor
floor.key_up(keys_pressed, event)
end
end
update do
case state
when :hallway
state = hallway.state
when :edit
hallway.hide
floor.remove
edit.marker_show
when :floor
hallway.hide
unless floor.done?
floor.time += 1
score.text = "#{format('%02d', level + 1)} | moves: #{floor.character.moves} | pushes: #{floor.character.pushes} | time: #{Time.at(floor.time / 60).utc.strftime("%M:%S:%L")}"
else
level += 1
if (next_floor = floors[level])
floor.remove
floor = Floor.new(**next_floor)
end
end
end
end
show