Last active
October 25, 2020 04:53
-
-
Save mikemar10/0097c66b2da3beb763ab923af153656b to your computer and use it in GitHub Desktop.
Procedural Hexagons in DragonRuby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hexagon | |
attr_reader :x, :y, :radius, :r, :g, :b, :angle | |
def initialize args, x, y, radius, r = 0, g = 0, b = 0, angle = 0 | |
@x = x | |
@y = y | |
@radius = radius | |
@r = r | |
@g = g | |
@b = b | |
@angle = angle | |
@args = args | |
prepare_render_target | |
end | |
def width | |
@radius | |
end | |
def height | |
@height ||= @radius * Math.sqrt(3) | |
end | |
def radius= radius | |
@radius = radius | |
@height = nil | |
prepare_render_target | |
end | |
def x= x | |
@x = x | |
@output = nil | |
end | |
def y= y | |
@y = y | |
@output = nil | |
end | |
def r= r | |
@r = r | |
@output = nil | |
end | |
def g= g | |
@g = g | |
@output = nil | |
end | |
def b= b | |
@b = b | |
@output = nil | |
end | |
def angle= angle | |
@angle = angle | |
@output = nil | |
end | |
def output | |
@output ||= 3.map_with_index do |n| | |
{x: x - width / 2, | |
y: y - height / 2, | |
w: 1280, | |
h: 720, | |
path: @name, | |
angle: 60 * n + @angle, | |
r: @r, | |
g: @g, | |
b: @b, | |
a: 255, | |
angle_anchor_x: width / 2 / 1280.0, | |
angle_anchor_y: height / 2 / 720.0 | |
} | |
end | |
end | |
def prepare_render_target | |
@name = "hexagon_#{radius}".to_sym | |
@args.render_target(@name).solids << {x: 0, | |
y: 0, | |
w: width, | |
h: height, | |
r: 255, | |
g: 255, | |
b: 255, | |
a: 255} | |
end | |
end | |
def tick args | |
args.state.hexagon_radius = 44 | |
args.state.hexagons ||= [ | |
Hexagon.new(args, 640, 480, args.state.hexagon_radius, 156, 156, 156), | |
Hexagon.new(args, 0, 0, 44), | |
Hexagon.new(args, args.grid.right / 2, args.grid.top / 2, 45, 156, 156, 156) | |
] | |
args.outputs.solids << [0, 0, 1280, 720, 77, 77, 77] | |
args.outputs.sprites << args.state.hexagons.map do |h| | |
if args.tick_count % 60 == 0 | |
h.r, h.g, h.b = rand(255), rand(255), rand(255) | |
h.radius = rand(128) | |
end | |
h.angle = args.tick_count % 360 | |
h.output | |
end | |
args.outputs.labels << [1270, 710, args.gtk.current_framerate, 0, 2, 0, 255, 0] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment