Created
February 6, 2017 07:11
-
-
Save yazgoo/0c84f38f82a9dd0bc080f0437a228430 to your computer and use it in GitHub Desktop.
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
require 'mittsu' | |
class Game | |
def bar | |
left_bar_geometry = Mittsu::BoxGeometry.new(0.2, 0.05, 1.0) | |
left_bar = Mittsu::Mesh.new(left_bar_geometry, Mittsu::MeshPhongMaterial.new(color: 0x888888)) | |
@camera.add left_bar | |
left_bar.position.y -= 0.4 | |
left_bar.position.z -= 1 | |
left_bar | |
end | |
def on_button_pressed button | |
if button == 3 | |
@shot_direction = @camera.get_world_direction | |
@light_object.position.x = @camera.position.x | |
@light_object.position.y = @camera.position.y | |
@light_object.position.z = @camera.position.z | |
elsif button == 2 | |
@jump = 0 | |
end | |
if @forward.nil? | |
if button == 7 | |
@forward = 1 | |
elsif button == 6 | |
@forward = -1 | |
end | |
end | |
end | |
def on_button_released button | |
if @forward | |
if button == 7 | |
@forward = nil if @forward == 1 | |
elsif button == 6 | |
@forward = nil if @forward == -1 | |
end | |
end | |
end | |
def get_axes renderer | |
left_stick = Mittsu::Vector2.new | |
right_stick = Mittsu::Vector2.new | |
axes = renderer.window.joystick_axes.map do |axis| | |
axis.abs < 0.15 ? 0.0 : axis | |
end | |
if axes.empty? | |
axes = [0, 0, 0, 0] | |
axes[0] -= 1 if renderer.window.key_down?(GLFW_KEY_LEFT) | |
axes[0] += 1 if renderer.window.key_down?(GLFW_KEY_RIGHT) | |
else | |
left_stick.set(axes[0], axes[1]) | |
right_stick.set(axes[2], axes[3]) | |
end | |
axes | |
end | |
def get_rays | |
directions = [[0, 0, 1], [1, 0, 1], [1, 0, 0], [1, 0, -1], [0, 0, -1], [-1, 0, -1], [-1, 0, 0]] | |
directions.map { |direction| | |
Mittsu::Vector3.new *direction | |
} | |
end | |
def initialize | |
@screen_width = 800 | |
@screen_height = 600 | |
aspect = @screen_width.to_f / @screen_height.to_f | |
@scene = Mittsu::Scene.new | |
@camera = Mittsu::PerspectiveCamera.new(75.0, aspect, 0.1, 1000.0) | |
box_geometry = Mittsu::BoxGeometry.new(1.0, 1.0, 1.0) | |
material = Mittsu::MeshPhongMaterial.new(color: 0xffff00) | |
@boxes = [] | |
(0..10).each do |z| | |
[-2, 2].each do |x| | |
box = Mittsu::Mesh.new(box_geometry, material) | |
box.position.x += x | |
box.position.z -= 2 * z | |
@boxes << box | |
@scene.add(box) | |
end | |
end | |
light = Mittsu::PointLight.new(0xffffff, 1.0, 10.0, 1.0) | |
sphere_geometry = Mittsu::SphereGeometry.new(1.0, 64, 64) | |
@dot = Mittsu::Mesh.new(sphere_geometry, Mittsu::MeshPhongMaterial.new(color: 0xffffff)) | |
@dot.scale.set(0.1, 0.1, 0.1) | |
light.add(@dot) | |
light.position.set(0.0, 0.0, 0.0) | |
@light_object = Mittsu::Object3D.new | |
@light_object.add(light) | |
@scene.add(@light_object) | |
plane_geometry = Mittsu::BoxGeometry.new(100, 0.1, 100) | |
plane = Mittsu::Mesh.new(plane_geometry, Mittsu::MeshPhongMaterial.new(color: 0x00ffff)) | |
plane.position.y -= 0.5 | |
@scene.add plane | |
left_bar = bar | |
left_bar.position.x -= 0.4 | |
right_bar = bar | |
right_bar.position.x += 0.4 | |
@scene.add @camera | |
light = Mittsu::AmbientLight.new(0x303030) | |
@scene.add(light) | |
@camera.position.z = 5.0 | |
camera_light = Mittsu::PointLight.new(0xffffff, 0.5, 10, 1) | |
camera_light_object = Mittsu::Object3D.new | |
light.position.set(0.0, 0.0, 0.0) | |
camera_light_object.add(camera_light) | |
@scene.add(camera_light_object) | |
camera_light_object.position = @camera.position | |
@shot_direction = @camera.get_world_direction | |
@jump = nil | |
@forward = nil | |
@rays = get_rays | |
@caster = Mittsu::Raycaster.new Mittsu::Vector3.new, Mittsu::Vector3.new | |
end | |
def detect_collision | |
@rays.each do |ray| | |
@caster.set @light_object.position, ray | |
collisions = @caster.intersect_objects @boxes, false | |
if not collisions.empty? | |
collision = collisions.first | |
if collision[:distance] < 0.1 | |
#@scene.remove() | |
collision[:object].position.y -= 2 | |
puts "collision #{collision[:object]} #{collision[:face]} #{collision[:face].normal}" | |
n = collision[:face].normal | |
shot_direction_normalized = Mittsu::Vector3.new | |
shot_direction_normalized.copy(@shot_direction).normalize | |
n_2in = Mittsu::Vector3.new | |
n_2in.copy(n) | |
n_2in.multiply_scalar((shot_direction_normalized.dot(n)*(-2))) | |
sub = n_2in.add(shot_direction_normalized) | |
@shot_direction = sub.normalize | |
end | |
end | |
end | |
end | |
def run | |
renderer = Mittsu::OpenGLRenderer.new width: @screen_width, height: @screen_height, title: 'first' | |
renderer.window.on_joystick_button_pressed do |joystick, button| | |
on_button_pressed button | |
end | |
renderer.window.on_joystick_button_released do |joystick, button| | |
on_button_released button | |
end | |
key_to_button = {GLFW_KEY_UP => 7, GLFW_KEY_DOWN => 6, GLFW_KEY_SPACE => 2, GLFW_KEY_A => 3} | |
renderer.window.on_key_pressed do |key| | |
on_button_pressed key_to_button[key] | |
end | |
renderer.window.on_key_released do |key| | |
on_button_released key_to_button[key] | |
end | |
@light_object.position.z = @camera.position.z | |
renderer.window.run do | |
renderer.render(@scene, @camera) | |
axes = get_axes renderer | |
detect_collision | |
@light_object.translate_on_axis(@shot_direction, 0.1) | |
@camera.rotate_on_axis Mittsu::Vector3.new(0, 1, 0), -0.02 * axes[0] | |
@camera.translate_on_axis Mittsu::Vector3.new(0, 0, 1), -0.04 * @forward if @forward | |
if @jump | |
@jump += 0.1 | |
sj = Math::sin @jump | |
@camera.position.y = sj | |
if @jump >= 3.14 | |
@camera.position.y = 0 | |
@jump = nil | |
end | |
end | |
end | |
end | |
end | |
Game.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment