Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active Nov 29, 2021
Embed
What would you like to do?
DragonRuby Game Toolkit VR (Virtual Reality/Oculus Quest) - Let There Be Light https://www.youtube.com/watch?v=S-baJwEjUsk&ab_channel=AmirRajan
# https://www.youtube.com/watch?v=S-baJwEjUsk&ab_channel=AmirRajan
class Game
attr_gtk
def tick
grid.origin_center!
defaults
calc
render
end
def defaults
state.max_star_distance ||= 100
state.min_star_distance ||= 0.001
state.star_distance ||= 0.001
state.center.x ||= 0
state.center.y ||= 0
state.center.z ||= 30
state.max.x ||= 640
state.max.y ||= 640
state.max.z ||= 50
state.stars ||= 500.map { random_point_on_sphere }
end
def calc
if inputs.controller_one.key_down.a && !state.it_has_begun
state.it_has_begun = true
outputs.sounds << "sounds/ode-to-joy.wav"
end
state.star_distance += 0.25 ** 2 if state.it_has_begun
end
def render
outputs.background_color = [0, 0, 0]
if state.star_distance <= 1.0
text_alpha = (1 - state.star_distance) * 255
outputs.labels << { x: 0, y: 50, text: "Let there be light.", r: 255, g: 255, b: 255, size_enum: 1, alignment_enum: 1, a: text_alpha }
outputs.labels << { x: 0, y: 25, text: "(press a)", r: 255, g: 255, b: 255, size_enum: -2, alignment_enum: 1, a: text_alpha }
end
outputs.sprites << star_sprites
end
def random_point_on_sphere
r = { xr: 2.randomize(:ratio) - 1,
yr: 2.randomize(:ratio) - 1,
zr: 2.randomize(:ratio) - 1 }
if (r.xr ** 2 + r.yr ** 2 + r.zr ** 2) > 1.0
return random_point_on_sphere
else
return r
end
end
def star_sprites
state.stars.map do |s|
w = (32 * state.star_distance).clamp(1, 32)
h = (32 * state.star_distance).clamp(1, 32)
x = (state.max.x * state.star_distance) * s.xr
y = (state.max.y * state.star_distance) * s.yr
z = state.center.z + (state.max.z * state.star_distance * 10 * s.zr)
angle_x = Math.atan2(z - 600, y).to_degrees + 90
angle_y = Math.atan2(z - 600, x).to_degrees + 90
draw_x = x - w.half
draw_y = y - 40 - h.half
draw_z = z
{ x: draw_x,
y: draw_y,
z: draw_z,
w: w,
h: h,
angle_x: angle_x,
angle_y: angle_y,
path: 'sprites/star.png' }
end
end
end
$game = Game.new
def tick_game args
$game.args = args
$game.tick
end
@rubyFeedback
Copy link

rubyFeedback commented Nov 8, 2021

Could some explanation be added, e. g. what is VR? I saw the link from reddit and then youtube but I am not familiar with any of this.

@amirrajan
Copy link
Author

amirrajan commented Nov 16, 2021

@rubyFeedback VR = Virtual Reality (Oculus Quest)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment