-
Notifications
You must be signed in to change notification settings - Fork 0
/
playground.rb
80 lines (68 loc) · 1.71 KB
/
playground.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
# frozen_string_literal: true
require 'rubygems'
require 'bundler/setup'
require 'ruby2d'
Dir['./lib/*.rb'].each { |file| require file }
CELL_SIZE = 16
WIDTH = 48
HEIGHT = 32
FULL_WIDTH = WIDTH * CELL_SIZE
FULL_HEIGHT = HEIGHT * CELL_SIZE
# https://bootflat.github.io/documentation.html
BACKGROUND_COLOR = '#434A54'
GRID_COLOR = '#656D78'
COLORS = { 'A' => '#A0D468', 'B' => '#FC6E51' }.freeze
set title: 'Playground', width: FULL_WIDTH, height: FULL_HEIGHT,
background: BACKGROUND_COLOR
@world = World.new(width: WIDTH, height: HEIGHT)
@toggle = false
def draw_grid
WIDTH.times do |x|
Line.new(
x1: x * CELL_SIZE, x2: x * CELL_SIZE, y1: 0, y2: FULL_HEIGHT, color: GRID_COLOR
)
end
HEIGHT.times do |y|
Line.new(
x1: 0, x2: FULL_WIDTH, y1: y * CELL_SIZE, y2: y * CELL_SIZE, color: GRID_COLOR
)
end
end
def draw_world
state = @world.state
state.size.times do |pos|
x = pos % WIDTH
y = pos / WIDTH
if state.live?(pos)
Square.new(x: x * CELL_SIZE, y: y * CELL_SIZE, size: CELL_SIZE,
color: COLORS[state.player(pos)])
end
end
end
def redraw
clear
draw_grid
draw_world
end
def iterate
@world.iterate
end
on :mouse_up do |evt|
position = WIDTH * (evt.y / CELL_SIZE) + (evt.x / CELL_SIZE)
state = @world.state
if state.live?(position)
@world.set_player(player: ' ', pos: position)
else
@world.set_player(player: 'A', pos: position) if evt.button == :left
@world.set_player(player: 'B', pos: position) if evt.button == :right
end
end
on :key_up do |evt|
@toggle = !@toggle if evt.key == 'space'
@world.clear if evt.key == 'c'
end
update do
redraw
iterate if @toggle && (0.5 * (get :frames).to_i % (get :fps).to_i).zero?
end
show