mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 21:22:33 +00:00
Compare commits
10 Commits
c2330ed9a9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 1f57dfd38c | |||
| 76a8bf95c7 | |||
| 1c25eeb32b | |||
| eabad4abd4 | |||
| b3561f02c1 | |||
| 9694cc2270 | |||
| a7df9a660d | |||
| d2f757eb23 | |||
| 0d70177de7 | |||
| dcefaeae4d |
24
Gemfile.lock
Normal file
24
Gemfile.lock
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
PATH
|
||||||
|
remote: .
|
||||||
|
specs:
|
||||||
|
cyberarm_engine (0.24.4)
|
||||||
|
gosu (~> 1.1)
|
||||||
|
|
||||||
|
GEM
|
||||||
|
remote: https://rubygems.org/
|
||||||
|
specs:
|
||||||
|
gosu (1.4.6)
|
||||||
|
minitest (5.25.5)
|
||||||
|
rake (13.2.1)
|
||||||
|
|
||||||
|
PLATFORMS
|
||||||
|
x64-mingw-ucrt
|
||||||
|
|
||||||
|
DEPENDENCIES
|
||||||
|
bundler (~> 2.2)
|
||||||
|
cyberarm_engine!
|
||||||
|
minitest (~> 5.0)
|
||||||
|
rake (~> 13.0)
|
||||||
|
|
||||||
|
BUNDLED WITH
|
||||||
|
2.5.3
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
@include "light_struct"
|
||||||
|
|
||||||
out vec4 frag_color;
|
out vec4 frag_color;
|
||||||
|
|
||||||
@include "light_struct"
|
|
||||||
const int DIRECTIONAL = 0;
|
const int DIRECTIONAL = 0;
|
||||||
const int POINT = 1;
|
const int POINT = 1;
|
||||||
const int SPOT = 2;
|
const int SPOT = 2;
|
||||||
|
|
||||||
flat in Light out_lights[7];
|
|
||||||
in vec2 out_tex_coords;
|
in vec2 out_tex_coords;
|
||||||
flat in int out_light_count;
|
flat in int out_light_count;
|
||||||
|
flat in Light out_lights[7];
|
||||||
|
|
||||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
|
|
||||||
@@ -27,43 +28,88 @@ vec4 directionalLight(Light light) {
|
|||||||
return vec4(_diffuse + _ambient + _specular, 1.0);
|
return vec4(_diffuse + _ambient + _specular, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
vec4 pointLight(Light light) {
|
|
||||||
return vec4(0.25, 0.25, 0.25, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec4 spotLight(Light light) {
|
|
||||||
return vec4(0.5, 0.5, 0.5, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec4 calculateLighting(Light light) {
|
|
||||||
vec4 result;
|
|
||||||
|
|
||||||
// switch(light.type) {
|
|
||||||
// case DIRECTIONAL: {
|
|
||||||
// result = directionalLight(light);
|
|
||||||
// }
|
|
||||||
// case SPOT: {
|
|
||||||
// result = spotLight(light);
|
|
||||||
// }
|
|
||||||
// default: {
|
|
||||||
// result = pointLight(light);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (light.type == DIRECTIONAL) {
|
|
||||||
result = directionalLight(light);
|
|
||||||
} else {
|
|
||||||
result = pointLight(light);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
frag_color = vec4(0.0);
|
Light light;
|
||||||
|
light.type = DIRECTIONAL;
|
||||||
|
|
||||||
for(int i = 0; i < out_light_count; i++)
|
light.position = vec3(100, 100, 100);
|
||||||
{
|
|
||||||
frag_color += texture(diffuse, out_tex_coords) * calculateLighting(out_lights[i]);
|
light.diffuse = vec3(0.5, 0.5, 0.5);
|
||||||
}
|
light.ambient = vec3(0.8, 0.8, 0.8);
|
||||||
|
light.specular = vec3(0.2, 0.2, 0.2);
|
||||||
|
|
||||||
|
light.intensity = 1.0;
|
||||||
|
|
||||||
|
frag_color = texture(diffuse, out_tex_coords) * directionalLight(light);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #version 330 core
|
||||||
|
// @include "light_struct"
|
||||||
|
|
||||||
|
// out vec4 frag_color;
|
||||||
|
|
||||||
|
// const int DIRECTIONAL = 0;
|
||||||
|
// const int POINT = 1;
|
||||||
|
// const int SPOT = 2;
|
||||||
|
|
||||||
|
// in vec2 out_tex_coords;
|
||||||
|
// flat in int out_light_count;
|
||||||
|
// flat in Light out_lights[7];
|
||||||
|
|
||||||
|
// uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
|
|
||||||
|
// vec4 directionalLight(Light light) {
|
||||||
|
// vec3 norm = normalize(texture(normal, out_tex_coords).rgb);
|
||||||
|
// vec3 diffuse_color = texture(diffuse, out_tex_coords).rgb;
|
||||||
|
// vec3 frag_pos = texture(position, out_tex_coords).rgb;
|
||||||
|
|
||||||
|
// vec3 lightDir = normalize(light.position - frag_pos);
|
||||||
|
// float diff = max(dot(norm, lightDir), 0);
|
||||||
|
|
||||||
|
// vec3 _ambient = light.ambient;
|
||||||
|
// vec3 _diffuse = light.diffuse * diff;
|
||||||
|
// vec3 _specular = light.specular;
|
||||||
|
|
||||||
|
// return vec4(_diffuse + _ambient + _specular, 1.0);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// vec4 pointLight(Light light) {
|
||||||
|
// return vec4(0.25, 0.25, 0.25, 1);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// vec4 spotLight(Light light) {
|
||||||
|
// return vec4(0.5, 0.5, 0.5, 1);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// vec4 calculateLighting(Light light) {
|
||||||
|
// vec4 result;
|
||||||
|
|
||||||
|
// // switch(light.type) {
|
||||||
|
// // case DIRECTIONAL: {
|
||||||
|
// // result = directionalLight(light);
|
||||||
|
// // }
|
||||||
|
// // case SPOT: {
|
||||||
|
// // result = spotLight(light);
|
||||||
|
// // }
|
||||||
|
// // default: {
|
||||||
|
// // result = pointLight(light);
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// if (light.type == DIRECTIONAL) {
|
||||||
|
// result = directionalLight(light);
|
||||||
|
// } else {
|
||||||
|
// result = pointLight(light);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void main() {
|
||||||
|
// frag_color = vec4(0.0);
|
||||||
|
|
||||||
|
// for(int i = 0; i < out_light_count; i++)
|
||||||
|
// {
|
||||||
|
// frag_color += texture(diffuse, out_tex_coords) * calculateLighting(out_lights[i]);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# version 330 core
|
#version 330 core
|
||||||
|
|
||||||
layout(location = 0) in vec3 in_position;
|
layout(location = 0) in vec3 in_position;
|
||||||
layout(location = 1) in vec3 in_color;
|
layout(location = 1) in vec3 in_color;
|
||||||
|
|||||||
@@ -67,12 +67,4 @@ require_relative "cyberarm_engine/ui/elements/menu_item"
|
|||||||
require_relative "cyberarm_engine/game_state"
|
require_relative "cyberarm_engine/game_state"
|
||||||
require_relative "cyberarm_engine/ui/gui_state"
|
require_relative "cyberarm_engine/ui/gui_state"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/model"
|
|
||||||
require_relative "cyberarm_engine/model_cache"
|
|
||||||
require_relative "cyberarm_engine/model/material"
|
|
||||||
require_relative "cyberarm_engine/model/model_object"
|
|
||||||
require_relative "cyberarm_engine/model/parser"
|
|
||||||
require_relative "cyberarm_engine/model/parsers/wavefront_parser"
|
|
||||||
require_relative "cyberarm_engine/model/parsers/collada_parser" if RUBY_ENGINE != "mruby" && defined?(Nokogiri)
|
|
||||||
|
|
||||||
require_relative "cyberarm_engine/builtin/intro_state"
|
require_relative "cyberarm_engine/builtin/intro_state"
|
||||||
|
|||||||
@@ -170,10 +170,11 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Add <=> method to support Range based gradients
|
# Add <=> method to support Range based gradients
|
||||||
module Gosu
|
# NOTE: Disabled, causes stack overflow 🙃
|
||||||
class Color
|
# module Gosu
|
||||||
def <=>(_other)
|
# class Color
|
||||||
self
|
# def <=>(_other)
|
||||||
end
|
# self
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
|
# end
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
module Common
|
module Common
|
||||||
|
ImageBlob = Data.define(:to_blob, :columns, :rows)
|
||||||
|
|
||||||
def push_state(klass, options = {})
|
def push_state(klass, options = {})
|
||||||
window.push_state(klass, options)
|
window.push_state(klass, options)
|
||||||
end
|
end
|
||||||
@@ -85,7 +87,8 @@ module CyberarmEngine
|
|||||||
unless asset
|
unless asset
|
||||||
instance = nil
|
instance = nil
|
||||||
instance = if klass == Gosu::Image
|
instance = if klass == Gosu::Image
|
||||||
klass.new(path, retro: retro, tileable: tileable)
|
path_or_blob = path.is_a?(String) ? path : ImageBlob.new(path.to_blob, path.width, path.height)
|
||||||
|
klass.new(path_or_blob, retro: retro, tileable: tileable)
|
||||||
else
|
else
|
||||||
klass.new(path)
|
klass.new(path)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -33,12 +33,23 @@ module Gosu
|
|||||||
#
|
#
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def self.draw_arc(x, y, radius, percentage = 1.0, segments = 128, thickness = 4, color = Gosu::Color::WHITE, z = 0, mode = :default)
|
def self.draw_arc(x, y, radius, percentage = 1.0, segments = 128, thickness = 4, color = Gosu::Color::WHITE, z = 0, mode = :default)
|
||||||
segments = 360.0 / segments
|
|
||||||
|
|
||||||
return if percentage == 0.0
|
return if percentage == 0.0
|
||||||
|
|
||||||
0.step((359 * percentage), percentage > 0 ? segments : -segments) do |angle|
|
angle_per_segment = 360.0 / segments
|
||||||
angle2 = angle + segments
|
arc_completion = 360 * percentage
|
||||||
|
next_segment_angle = angle_per_segment
|
||||||
|
|
||||||
|
angle = 0
|
||||||
|
loop do
|
||||||
|
break if angle >= arc_completion
|
||||||
|
|
||||||
|
if angle + angle_per_segment > arc_completion
|
||||||
|
next_segment_angle = arc_completion - angle
|
||||||
|
else
|
||||||
|
next_segment_angle = angle_per_segment
|
||||||
|
end
|
||||||
|
|
||||||
|
angle2 = angle + next_segment_angle
|
||||||
|
|
||||||
point_a_left_x = x + Gosu.offset_x(angle, radius - thickness)
|
point_a_left_x = x + Gosu.offset_x(angle, radius - thickness)
|
||||||
point_a_left_y = y + Gosu.offset_y(angle, radius - thickness)
|
point_a_left_y = y + Gosu.offset_y(angle, radius - thickness)
|
||||||
@@ -93,6 +104,8 @@ module Gosu
|
|||||||
z, mode
|
z, mode
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
angle += next_segment_angle
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Model
|
class Model
|
||||||
|
include OpenGL
|
||||||
|
include CyberarmEngine
|
||||||
|
|
||||||
attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones, :material_file,
|
attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones, :material_file,
|
||||||
:current_material, :current_object, :vertex_count, :smoothing
|
:current_material, :current_object, :vertex_count, :smoothing
|
||||||
attr_reader :position, :bounding_box, :textured_material, :file_path, :positions_buffer_id, :colors_buffer_id,
|
attr_reader :position, :bounding_box, :textured_material, :file_path, :positions_buffer_id, :colors_buffer_id,
|
||||||
@@ -49,9 +52,9 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@objects.each { |o| @vertex_count += o.vertices.size }
|
@objects.each { |o| @vertex_count += o.vertices.size }
|
||||||
|
|
||||||
# start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
||||||
# build_collision_tree
|
build_collision_tree
|
||||||
# puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
|
puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(parser)
|
def parse(parser)
|
||||||
@@ -178,7 +181,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def build_collision_tree
|
def build_collision_tree
|
||||||
@aabb_tree = IMICFPS::AABBTree.new
|
@aabb_tree = AABBTree.new
|
||||||
|
|
||||||
@faces.each do |face|
|
@faces.each do |face|
|
||||||
box = BoundingBox.new
|
box = BoundingBox.new
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Model
|
class Model
|
||||||
class ModelObject
|
class Mesh
|
||||||
attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color
|
attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color
|
||||||
attr_accessor :faces, :scale
|
attr_accessor :faces, :scale
|
||||||
|
|
||||||
@@ -48,12 +48,12 @@ module CyberarmEngine
|
|||||||
if _model
|
if _model
|
||||||
@model.current_object = _model
|
@model.current_object = _model
|
||||||
else
|
else
|
||||||
raise "Couldn't find ModelObject!"
|
raise "Couldn't find Mesh!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def change_object(id, name)
|
def change_object(id, name)
|
||||||
@model.objects << Model::ModelObject.new(id, name)
|
@model.objects << Model::Mesh.new(id, name)
|
||||||
@model.current_object = @model.objects.last
|
@model.current_object = @model.objects.last
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
begin
|
begin
|
||||||
require "opengl"
|
require "opengl"
|
||||||
|
require "glu"
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
puts "Required gem is not installed, please install 'opengl-bindings' and try again."
|
puts "Required gem is not installed, please install 'opengl-bindings' and try again."
|
||||||
exit(1)
|
exit(1)
|
||||||
@@ -8,7 +9,7 @@ end
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
def gl_error?
|
def gl_error?
|
||||||
e = glGetError
|
e = glGetError
|
||||||
if e != GL_NO_ERROR
|
if e != OpenGL::GL_NO_ERROR
|
||||||
warn "OpenGL error detected by handler at: #{caller[0]}"
|
warn "OpenGL error detected by handler at: #{caller[0]}"
|
||||||
warn " #{gluErrorString(e)} (#{e})\n"
|
warn " #{gluErrorString(e)} (#{e})\n"
|
||||||
exit if Window.instance&.exit_on_opengl_error?
|
exit if Window.instance&.exit_on_opengl_error?
|
||||||
@@ -38,3 +39,15 @@ require_relative "opengl/renderer/g_buffer"
|
|||||||
require_relative "opengl/renderer/bounding_box_renderer"
|
require_relative "opengl/renderer/bounding_box_renderer"
|
||||||
require_relative "opengl/renderer/opengl_renderer"
|
require_relative "opengl/renderer/opengl_renderer"
|
||||||
require_relative "opengl/renderer/renderer"
|
require_relative "opengl/renderer/renderer"
|
||||||
|
|
||||||
|
require_relative "trees/aabb_tree_debug"
|
||||||
|
require_relative "trees/aabb_node"
|
||||||
|
require_relative "trees/aabb_tree"
|
||||||
|
|
||||||
|
require_relative "model"
|
||||||
|
require_relative "model_cache"
|
||||||
|
require_relative "model/material"
|
||||||
|
require_relative "model/mesh"
|
||||||
|
require_relative "model/parser"
|
||||||
|
require_relative "model/parsers/wavefront_parser"
|
||||||
|
require_relative "model/parsers/collada_parser" if RUBY_ENGINE != "mruby" && defined?(Nokogiri)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Light
|
class Light
|
||||||
|
include OpenGL
|
||||||
|
|
||||||
DIRECTIONAL = 0
|
DIRECTIONAL = 0
|
||||||
POINT = 1
|
POINT = 1
|
||||||
SPOT = 2
|
SPOT = 2
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class PerspectiveCamera
|
class PerspectiveCamera
|
||||||
|
include OpenGL
|
||||||
|
include GLU
|
||||||
|
|
||||||
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
|
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
|
||||||
:min_view_distance, :max_view_distance
|
:min_view_distance, :max_view_distance
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class GBuffer
|
class GBuffer
|
||||||
|
include OpenGL
|
||||||
|
|
||||||
attr_reader :screen_vbo, :vertices, :uvs
|
attr_reader :screen_vbo, :vertices, :uvs
|
||||||
attr_reader :width, :height
|
attr_reader :width, :height
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class OpenGLRenderer
|
class OpenGLRenderer
|
||||||
|
include OpenGL
|
||||||
|
include CyberarmEngine
|
||||||
|
|
||||||
@@immediate_mode_warning = false
|
@@immediate_mode_warning = false
|
||||||
|
|
||||||
attr_accessor :show_wireframe
|
attr_accessor :show_wireframe
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ module CyberarmEngine
|
|||||||
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
||||||
class Shader
|
class Shader
|
||||||
include OpenGL
|
include OpenGL
|
||||||
|
|
||||||
@@shaders = {} # Cache for {Shader} instances
|
@@shaders = {} # Cache for {Shader} instances
|
||||||
PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation
|
PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation
|
||||||
|
|
||||||
@@ -298,6 +299,7 @@ module CyberarmEngine
|
|||||||
# @see Shader.use Shader.use
|
# @see Shader.use Shader.use
|
||||||
def use(&block)
|
def use(&block)
|
||||||
return unless compiled?
|
return unless compiled?
|
||||||
|
|
||||||
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
||||||
|
|
||||||
Shader.active_shader = self
|
Shader.active_shader = self
|
||||||
|
|||||||
126
lib/cyberarm_engine/trees/aabb_node.rb
Normal file
126
lib/cyberarm_engine/trees/aabb_node.rb
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module CyberarmEngine
|
||||||
|
class AABBTree
|
||||||
|
class AABBNode
|
||||||
|
attr_accessor :bounding_box, :parent, :object
|
||||||
|
attr_reader :a, :b
|
||||||
|
|
||||||
|
def initialize(parent:, object:, bounding_box:)
|
||||||
|
@parent = parent
|
||||||
|
@object = object
|
||||||
|
@bounding_box = bounding_box
|
||||||
|
|
||||||
|
@a = nil
|
||||||
|
@b = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def a=(leaf)
|
||||||
|
@a = leaf
|
||||||
|
@a.parent = self
|
||||||
|
end
|
||||||
|
|
||||||
|
def b=(leaf)
|
||||||
|
@b = leaf
|
||||||
|
@b.parent = self
|
||||||
|
end
|
||||||
|
|
||||||
|
def leaf?
|
||||||
|
@object
|
||||||
|
end
|
||||||
|
|
||||||
|
def insert_subtree(leaf)
|
||||||
|
if leaf?
|
||||||
|
new_node = AABBNode.new(parent: nil, object: nil, bounding_box: @bounding_box.union(leaf.bounding_box))
|
||||||
|
|
||||||
|
new_node.a = self
|
||||||
|
new_node.b = leaf
|
||||||
|
|
||||||
|
new_node
|
||||||
|
else
|
||||||
|
cost_a = @a.bounding_box.volume + @b.bounding_box.union(leaf.bounding_box).volume
|
||||||
|
cost_b = @b.bounding_box.volume + @a.bounding_box.union(leaf.bounding_box).volume
|
||||||
|
|
||||||
|
if cost_a == cost_b
|
||||||
|
cost_a = @a.proximity(leaf)
|
||||||
|
cost_b = @b.proximity(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
if cost_b < cost_a
|
||||||
|
self.b = @b.insert_subtree(leaf)
|
||||||
|
else
|
||||||
|
self.a = @a.insert_subtree(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
@bounding_box = @bounding_box.union(leaf.bounding_box)
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_subtree(collider, items = [])
|
||||||
|
if @bounding_box.intersect?(collider)
|
||||||
|
if leaf?
|
||||||
|
items << self
|
||||||
|
else
|
||||||
|
@a.search_subtree(collider, items)
|
||||||
|
@b.search_subtree(collider, items)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
items
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_subtree(leaf)
|
||||||
|
if leaf
|
||||||
|
self
|
||||||
|
elsif leaf.parent == self
|
||||||
|
other_child = other(leaf)
|
||||||
|
other_child.parent = @parent
|
||||||
|
other_child
|
||||||
|
else
|
||||||
|
leaf.parent.disown_child(leaf)
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def other(leaf)
|
||||||
|
@a == leaf ? @b : @a
|
||||||
|
end
|
||||||
|
|
||||||
|
def disown_child(leaf)
|
||||||
|
value = other(leaf)
|
||||||
|
raise "Can not replace child of a leaf!" if @parent.leaf?
|
||||||
|
raise "Node is not a child of parent!" unless leaf.child_of?(@parent)
|
||||||
|
|
||||||
|
if @parent.a == self
|
||||||
|
@parent.a = value
|
||||||
|
else
|
||||||
|
@parent.b = value
|
||||||
|
end
|
||||||
|
|
||||||
|
@parent.update_bounding_box
|
||||||
|
end
|
||||||
|
|
||||||
|
def child_of?(leaf)
|
||||||
|
self == leaf.a || self == leaf.b
|
||||||
|
end
|
||||||
|
|
||||||
|
def proximity(leaf)
|
||||||
|
(@bounding_box - leaf.bounding_box).sum.abs
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_bounding_box
|
||||||
|
node = self
|
||||||
|
|
||||||
|
unless node.leaf?
|
||||||
|
node.bounding_box = node.a.bounding_box.union(node.b.bounding_box)
|
||||||
|
|
||||||
|
while (node = node.parent)
|
||||||
|
node.bounding_box = node.a.bounding_box.union(node.b.bounding_box)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
55
lib/cyberarm_engine/trees/aabb_tree.rb
Normal file
55
lib/cyberarm_engine/trees/aabb_tree.rb
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module CyberarmEngine
|
||||||
|
class AABBTree
|
||||||
|
include AABBTreeDebug
|
||||||
|
|
||||||
|
attr_reader :root, :objects, :branches, :leaves
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@objects = {}
|
||||||
|
@root = nil
|
||||||
|
@branches = 0
|
||||||
|
@leaves = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def insert(object, bounding_box)
|
||||||
|
raise "BoundingBox can't be nil!" unless bounding_box
|
||||||
|
raise "Object can't be nil!" unless object
|
||||||
|
# raise "Object already in tree!" if @objects[object] # FIXME
|
||||||
|
|
||||||
|
leaf = AABBNode.new(parent: nil, object: object, bounding_box: bounding_box.dup)
|
||||||
|
@objects[object] = leaf
|
||||||
|
|
||||||
|
insert_leaf(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
def insert_leaf(leaf)
|
||||||
|
@root = @root ? @root.insert_subtree(leaf) : leaf
|
||||||
|
end
|
||||||
|
|
||||||
|
def update(object, bounding_box)
|
||||||
|
leaf = remove(object)
|
||||||
|
leaf.bounding_box = bounding_box
|
||||||
|
insert_leaf(leaf)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a list of all objects that collided with collider
|
||||||
|
def search(collider, return_nodes = false)
|
||||||
|
items = []
|
||||||
|
if @root
|
||||||
|
items = @root.search_subtree(collider)
|
||||||
|
items.map!(&:object) unless return_nodes
|
||||||
|
end
|
||||||
|
|
||||||
|
items
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove(object)
|
||||||
|
leaf = @objects.delete(object)
|
||||||
|
@root = @root.remove_subtree(leaf) if leaf
|
||||||
|
|
||||||
|
leaf
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
29
lib/cyberarm_engine/trees/aabb_tree_debug.rb
Normal file
29
lib/cyberarm_engine/trees/aabb_tree_debug.rb
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module CyberarmEngine
|
||||||
|
# Gets included into AABBTree
|
||||||
|
module AABBTreeDebug
|
||||||
|
def inspect
|
||||||
|
@branches = 0
|
||||||
|
@leaves = 0
|
||||||
|
if @root
|
||||||
|
node = @root
|
||||||
|
|
||||||
|
debug_search(node.a)
|
||||||
|
debug_search(node.b)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "<#{self.class}:#{object_id}> has #{@branches} branches and #{@leaves} leaves"
|
||||||
|
end
|
||||||
|
|
||||||
|
def debug_search(node)
|
||||||
|
if node.leaf?
|
||||||
|
@leaves += 1
|
||||||
|
else
|
||||||
|
@branches += 1
|
||||||
|
debug_search(node.a)
|
||||||
|
debug_search(node.b)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -331,8 +331,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def debug_draw
|
def debug_draw
|
||||||
# FIXME
|
return if CyberarmEngine.const_defined?("GUI_DEBUG_ONLY_ELEMENT") && self.class == GUI_DEBUG_ONLY_ELEMENT
|
||||||
return# if const_defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT
|
|
||||||
|
|
||||||
Gosu.draw_line(
|
Gosu.draw_line(
|
||||||
x, y, @debug_color,
|
x, y, @debug_color,
|
||||||
@@ -350,7 +349,7 @@ module CyberarmEngine
|
|||||||
Float::INFINITY
|
Float::INFINITY
|
||||||
)
|
)
|
||||||
Gosu.draw_line(
|
Gosu.draw_line(
|
||||||
x, outer_height, @debug_color,
|
x, y + outer_height, @debug_color,
|
||||||
x, y, @debug_color,
|
x, y, @debug_color,
|
||||||
Float::INFINITY
|
Float::INFINITY
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -151,10 +151,6 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
return if @in_recalculate
|
|
||||||
|
|
||||||
@in_recalculate = true
|
|
||||||
|
|
||||||
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||||
|
|
||||||
return unless visible?
|
return unless visible?
|
||||||
@@ -243,8 +239,6 @@ module CyberarmEngine
|
|||||||
recalculate_if_size_changed
|
recalculate_if_size_changed
|
||||||
|
|
||||||
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
|
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
|
||||||
|
|
||||||
@in_recalculate = false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def layout
|
def layout
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ module CyberarmEngine
|
|||||||
@last_text = @text.text
|
@last_text = @text.text
|
||||||
@last_pos = caret_pos
|
@last_pos = caret_pos
|
||||||
|
|
||||||
if caret_pos.between?(@offset_x, @width + @offset_x)
|
if caret_pos.between?(@offset_x + 1, @width + @offset_x)
|
||||||
# Do nothing
|
# Do nothing
|
||||||
|
|
||||||
elsif caret_pos < @offset_x
|
elsif caret_pos < @offset_x
|
||||||
@@ -197,7 +197,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def text_input_position_for(method)
|
def text_input_position_for(method)
|
||||||
if @type == :password
|
if @type == :password
|
||||||
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length)
|
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length) - @style.border_thickness_left
|
||||||
else
|
else
|
||||||
@text.x + @text.width(@text_input.text[0...@text_input.send(method)]) - @style.border_thickness_left
|
@text.x + @text.width(@text_input.text[0...@text_input.send(method)]) - @style.border_thickness_left
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :step_size, :value
|
attr_reader :value
|
||||||
attr_accessor :range, :step_size
|
attr_accessor :range, :step_size
|
||||||
|
|
||||||
def initialize(options = {}, block = nil)
|
def initialize(options = {}, block = nil)
|
||||||
@@ -47,7 +47,7 @@ module CyberarmEngine
|
|||||||
add(@handle)
|
add(@handle)
|
||||||
end
|
end
|
||||||
|
|
||||||
def layout
|
def recalculate
|
||||||
_width = dimensional_size(@style.width, :width)
|
_width = dimensional_size(@style.width, :width)
|
||||||
_height = dimensional_size(@style.height, :height)
|
_height = dimensional_size(@style.height, :height)
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ module CyberarmEngine
|
|||||||
@height = _height
|
@height = _height
|
||||||
|
|
||||||
position_handle
|
position_handle
|
||||||
@handle.layout
|
@handle.recalculate
|
||||||
@handle.update_background
|
@handle.update_background
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
|
|||||||
@@ -85,8 +85,7 @@ module CyberarmEngine
|
|||||||
@tip.draw
|
@tip.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME
|
if CyberarmEngine.const_defined?("GUI_DEBUG")
|
||||||
if false# defined?(GUI_DEBUG)
|
|
||||||
Gosu.flush
|
Gosu.flush
|
||||||
|
|
||||||
@root_container.debug_draw
|
@root_container.debug_draw
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
NAME = "InDev".freeze
|
NAME = "InDev".freeze
|
||||||
VERSION = "0.24.2".freeze
|
VERSION = "0.24.5".freeze
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user