-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.gd
166 lines (131 loc) · 3.97 KB
/
Game.gd
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
extends Node2D
# Project links
const pixel_scene = preload("res://Pixel.tscn")
# Scene links
onready var camera = $Camera2D
# Configuration
const pixel_size = 10
const resolution = Vector2.ONE * 1000
const LINE_COLOR = Color(0.1, 0.1, 0.1)
const fill_rate = 2 # per second # TODO should use delta
# Members
var board
var grid_resolution
var mousepos = Vector2()
var left_mousedown = false
var right_mousedown = false
var cursor_size = 1
var paused = false
var pixels = []
func _ready():
set_process(false)
#OS.vsync_enabled = false
#Engine.target_fps = 200
resolution.x = int(resolution.x / pixel_size) * pixel_size
resolution.y = int(resolution.y / pixel_size) * pixel_size
grid_resolution = Vector2(int(resolution.x / pixel_size), int(resolution.y / pixel_size))
board = Board.new(grid_resolution)
OS.window_size = resolution
call_deferred("_start")
func _start():
camera.zoom = Vector2(resolution.x / float(grid_resolution.x), resolution.y / float(grid_resolution.y))
camera.zoom = Vector2(float(grid_resolution.x) / resolution.x, float(grid_resolution.y) / resolution.y)
for y in range(grid_resolution.y):
for x in range(grid_resolution.x):
pixels.append(null)
set_process(true)
func add_pixel(x, y):
var p = pixel_scene.instance()
p.position = Vector2(x, y)
add_child(p)
pixels[y*grid_resolution.x + x] = p
return p
func screen_pos_to_board_pos(screen_pos):
return Vector2(int(screen_pos.x / pixel_size), int(screen_pos.y / pixel_size))
var fps_sim = 1000
var sim_frame_counter = 0
var sim_frame_step = 1.0 / fps_sim
func _process(delta):
draw() # Redraw
if left_mousedown:
var board_coo = screen_pos_to_board_pos(mousepos)
brush_draw_pixels(board_coo)
if right_mousedown:
var board_coo = screen_pos_to_board_pos(mousepos)
brush_draw_pixels(board_coo, true)
if not paused:
"""
sim_frame_counter += delta
if sim_frame_counter >= sim_frame_step:
sim_frame_counter -= sim_frame_step
board.update()
"""
board.update()
# """
func brush_draw_pixels(pos, clearing=false):
var radius = cursor_size
for y in range(-radius, radius + 1):
for x in range(-radius, radius + 1):
var screen_x = pos.x + x
var screen_y = pos.y + y
if 0 <= screen_x and screen_x < grid_resolution.x and 0 <= screen_y and screen_y < grid_resolution.y:
if x*x + y*y < radius * radius: # Make round brush
if clearing:
board.clear_pixel(Vector2(screen_x, screen_y))
else:
board.set_pixel(Vector2(screen_x, screen_y), fill_rate)
func draw():
_draw_pixels()
func _draw():
#_draw_grid()
_draw_ui()
func _draw_pixels():
var filled = true
var width = 0
for y in board.resolution.y:
for x in board.resolution.x:
var pp = Vector2(x, y)
var p = board.get_pixel(pp)
var val = inverse_lerp(32, 0, p)
var col = Color(0, 0, val)
var pixel_node = pixels[y*grid_resolution.x + x]
if pixel_node == null:
pixel_node = add_pixel(x, y)
if p == 0:
pixel_node.hide()
else:
pixel_node.show()
pixel_node.set_color(col)
func _draw_ui():
draw_arc(mousepos, cursor_size * pixel_size, 0, PI * 2, 64, Color.orange, 2, false)
func _draw_grid():
for x in range(grid_resolution.x):
draw_line(Vector2(x * pixel_size, 0), Vector2(x*pixel_size, resolution.y), LINE_COLOR)
for y in range(grid_resolution.y):
draw_line(Vector2(0, y * pixel_size), Vector2(resolution.x, y*pixel_size), LINE_COLOR)
func _input(event):
if event.is_echo():
return
if event is InputEventMouseButton:
match event.button_index:
BUTTON_LEFT:
left_mousedown = event.is_pressed()
BUTTON_RIGHT:
right_mousedown = event.is_pressed()
BUTTON_WHEEL_UP:
if event.is_pressed():
cursor_size += 1
BUTTON_WHEEL_DOWN:
if event.is_pressed():
cursor_size = max(1, cursor_size - 1)
elif event is InputEventMouseMotion:
mousepos = event.position
elif event is InputEventKey:
match event.scancode:
KEY_ENTER:
if event.is_pressed():
board.update()
board.print_board()
KEY_SPACE:
if event.is_pressed():
paused = !paused