-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
129 lines (123 loc) · 3.56 KB
/
main.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
class Ground_Generate
def initialize args
@ground = generate_ground
ground_render args
@last = 'generate_ground'
@next = 'generate_ground_lines'
@x = 0
@vx = 1
end
def generate_ground(width=2560, max_h=180, min_h = 5, max_change=5)
arr = []
w = 1
x = -1
h = min_h
counter = 0
target_y = rand(max_h)
while x < width
x += 1
if (target_y - h).abs <= 1
counter = rand(100)
end
if counter > 0
counter -=1
if counter <= 0
target_y = [rand(max_h), min_h].max
counter = 0
end
end
if width - x <= [h, 50].max
target_y = min_h
end
if h < target_y
h += rand(max_change)
elsif h > target_y
h -= rand(max_change)
end
arr << {x:x, y:0, w:w, h:h, r:64, g:64, b:64}.solid!
arr << {x:x, y:h, w:w, h:1, r:0, g:255, b:0}.solid!
arr << {x:x+2560, y:0, w:w, h:h, r:64, g:64, b:64}.solid!
arr << {x:x+2560, y:h, w:w, h:1, r:0, g:255, b:0}.solid!
end
arr
end
def generate_ground_lines(width=2560, max_h=180, min_h = 5, max_change=50)
arr = []
x = 0
w = 5
y = min_h
target_y = min_h
while x + w < width
arr << {x:x, y:y, x2:x+w, y2:target_y, r:128, g:128, b:128}.line!
x += w
w = [rand(100), (width - x)].min
y = target_y
target_y += rand(max_change + max_change) - max_change
if target_y < min_h
target_y = min_h
end
if target_y > max_h
target_y = max_h
end
if x <= 1280
arr << {x:x+2560, y:y, x2:x+2560+w, y2:target_y, r:128, g:128, b:128}.line!
if 1280 - x <= 100 and x < 1280
arr << {x:x, y:y, x2:1280, y2:min_h, r:128, g:128, b:128}.line!
arr << {x:x+2560, y:y, x2:3840, y2:min_h, r:128, g:128, b:128}.line!
x = 1280
y = min_h
target_y = min_h
end
end
if width - x <= 100
arr << {x:x, y:y, x2:width, y2:min_h, r:128, g:128, b:128}.line!
#arr << {x:x+2560, y:y, x2:5120, y2:min_h, r:128, g:128, b:128}.line!
break
end
end
arr
end
def ground_render args
args.outputs[:ground].w = 5120
args.outputs[:ground].h = 180
args.outputs[:ground].primitives <<{x:0, y:0, w:1280, h:720, r:0, g:0, b:0}.solid!
args.outputs[:ground].primitives << @ground.map do |g|
g
end
end
def tick args
if args.inputs.keyboard.key_down.space
@ground = send(@next)
ground_render args
tmp = @last
@last = @next
@next = tmp
end
if args.inputs.keyboard.key_down.right
@vx += 1
end
if args.inputs.keyboard.key_down.left
@vx -= 1
end
@x = (@x + @vx) % 2560
args.outputs.primitives <<{x:0, y:0, w:1280, h:720, r:0, g:0, b:0}.solid!
#args.outputs.primitives << @ground.map do |g|
# g[:x] = (g[:x] - @vx) % 2560
# g[:x2] = (g[:x2] - @vx) % 2560
# g
#end
args.outputs.primitives << {x: 320, y: 629, w: 640, h: 90,
path: :ground,
source_x: 0, source_y: 0,
source_w: 2560, source_h: 180}.sprite!
args.outputs.primitives <<{x:319, y:628, w:641, h:91, r:0, g:128, b:0}.border!
args.outputs.primitives << {x: 0, y: 0, w: 1280, h: 180,
path: :ground,
source_x: @x, source_y: 0,
source_w: 1280, source_h: 180}.sprite!
end
end
def tick args
args.state.gt ||= Ground_Generate.new(args)
args.state.gt.tick(args)
end