-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplay.gd
105 lines (67 loc) · 2.18 KB
/
Display.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
extends Node2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var ascii_texture = preload("res://sprites/tilemap.png")
var invader_texture = preload("res://sprites/invaders.png")
var invader_data = null
var used_count = 0
# Called when the node enters the scene tree for the first time.
func _ready():
var file = File.new()
file.open("res://sprites/invaders.json", File.READ)
var content = file.get_as_text()
file.close()
invader_data = JSON.parse(content).result
used_count = 0
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func clear_sprites():
used_count = 0
for n in get_children():
n.visible = false
func create_invader_sprite(f:int, x:float, y:float, sx:float, sy:float):
if used_count >= get_child_count():
var ns = Sprite.new()
ns.region_enabled = true
add_child(ns)
var sprite = get_child(used_count)
used_count += 1
sprite.texture = invader_texture
sprite.visible = true
sprite.position = Vector2( x, y )
var data = invader_data[f]
sprite.scale = Vector2( sx, sy )
# based on integer f
sprite.region_rect = Rect2( data.x, 512 - data.y - data.height - 1, data.width, data.height )
return sprite
func create_ascii_sprite(f:int, x:float, y:float, sx:float, sy:float):
if used_count >= get_child_count():
var ns = Sprite.new()
ns.region_enabled = true
add_child(ns)
var sprite = get_child(used_count)
used_count += 1
sprite.texture = ascii_texture
sprite.visible = true
sprite.position = Vector2( x, y )
sprite.scale = Vector2( sx, sy )
# compute rect from f
var fx:int = f % 16
var fy:int = f / 16
sprite.region_rect = Rect2( fx * 8, fy * 8, 8, 8 )
return sprite
func create_invader(f:int, x:float, y:float, sx:float, sy:float):
var sprite = create_invader_sprite(f, x, y, sx, sy)
return sprite
func create_ascii(f:int, x:float, y:float, sx:float, sy:float):
var sprite = create_ascii_sprite(f, x, y, sx, sy)
return sprite
func create_text(x:float, y:float, w:String):
var ch:int
for i in w.length():
ch = w.substr(i, 1).to_ascii()[0]
create_ascii(ch + (15 * 256), x, y, 2.0, 2.0)
x += 16