13 Commits

20 changed files with 142 additions and 56 deletions

View File

@@ -1,4 +1,10 @@
require "gosu"
begin
require File.expand_path("../../ffi-gosu/lib/gosu", File.dirname(__FILE__))
rescue LoadError => e
pp e
require "gosu"
end
require_relative "cyberarm_engine/version"

View File

@@ -1,5 +1,3 @@
require "gosu"
module CyberarmEngine
class Background
attr_accessor :x, :y, :z, :width, :height, :angle, :debug

View File

@@ -1,50 +1,50 @@
module CyberarmEngine
module Common
def push_state(klass, options={})
$window.push_state(klass, options)
window.push_state(klass, options)
end
def current_state
$window.current_state
window.current_state
end
def previous_state
$window.previous_state
window.previous_state
end
def pop_state
$window.pop_state
window.pop_state
end
def show_cursor
$window.show_cursor
window.show_cursor
end
def show_cursor=boolean
$window.show_cursor = boolean
window.show_cursor = boolean
end
def draw_rect(x, y, width, height, color, z = 0)
$window.draw_rect(x,y,width,height,color,z)
Gosu.draw_rect(x, y, width, height, color, z)
end
def fill(color, z = 0)
draw_rect(0, 0, $window.width, $window.height, color, z)
draw_rect(0, 0, window.width, window.height, color, z)
end
def lighten(color, amount = 25)
if defined?(color.alpha)
return Gosu::Color.rgba(color.red+amount, color.green+amount, color.blue+amount, color.alpha)
return Gosu::Color.rgba(color.red + amount, color.green + amount, color.blue + amount, color.alpha)
else
return Gosu::Color.rgb(color.red+amount, color.green+amount, color.blue+amount)
return Gosu::Color.rgb(color.red + amount, color.green + amount, color.blue + amount)
end
end
def darken(color, amount = 25)
if defined?(color.alpha)
return Gosu::Color.rgba(color.red-amount, color.green-amount, color.blue-amount, color.alpha)
return Gosu::Color.rgba(color.red - amount, color.green - amount, color.blue - amount, color.alpha)
else
return Gosu::Color.rgb(color.red-amount, color.green-amount, color.blue-amount)
return Gosu::Color.rgb(color.red - amount, color.green - amount, color.blue - amount)
end
end
@@ -88,4 +88,4 @@ module CyberarmEngine
$window
end
end
end
end

View File

@@ -5,7 +5,7 @@ module CyberarmEngine
SONGS = {}
attr_accessor :show_cursor
attr_reader :current_state, :last_frame_time
attr_reader :last_frame_time
def self.now
Gosu.milliseconds
@@ -58,11 +58,15 @@ module CyberarmEngine
end
def push_state(klass, options={})
options = {setup: true}.merge(options)
if klass.instance_of?(klass.class) && defined?(klass.options)
@states << klass
klass.setup if options[:setup]
else
@states << klass.new(options) if child_of?(klass, GameState)
@states << klass.new if child_of?(klass, Element::Container)
current_state.setup if current_state.class == klass && options[:setup]
end
end
@@ -94,4 +98,4 @@ module CyberarmEngine
end
end
end
end
end

View File

@@ -3,7 +3,8 @@ module CyberarmEngine
include Common
attr_accessor :image, :angle, :position, :velocity, :center_x, :center_y, :scale_x, :scale_y,
:color, :alpha, :mode, :options, :paused, :radius, :last_position
:color, :mode, :options, :paused, :radius, :last_position
attr_reader :alpha
def initialize(options={})
if options[:auto_manage] || options[:auto_manage] == nil
$window.current_state.add_game_object(self)
@@ -141,7 +142,7 @@ module CyberarmEngine
self.angle%=360
end
def alpha=int # 0-255
def alpha=(int) # 0-255
@alpha = int
@alpha = 255 if @alpha > 255
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
@@ -154,7 +155,7 @@ module CyberarmEngine
def button_up(id)
end
def button_down?(id)
def button_down(id)
end
def find_closest(game_object_class)

View File

@@ -12,8 +12,6 @@ module CyberarmEngine
$window.text_input = nil unless options[:preserve_text_input]
@down_keys = {}
setup
end
def setup

View File

@@ -49,11 +49,17 @@ module CyberarmEngine
return temp
end
# returns whether both bounding boxes intersect
# returns whether bounding box intersects other
def intersect?(other)
(@min.x <= other.max.x && @max.x >= other.min.x) &&
(@min.y <= other.max.y && @max.y >= other.min.y) &&
(@min.z <= other.max.z && @max.z >= other.min.z)
if other.is_a?(Ray)
other.intersect?(self)
elsif other.is_a?(BoundingBox)
(@min.x <= other.max.x && @max.x >= other.min.x) &&
(@min.y <= other.max.y && @max.y >= other.min.y) &&
(@min.z <= other.max.z && @max.z >= other.min.z)
else
raise "Unknown collider: #{other.class}"
end
end
# does this bounding box envelop other bounding box? (inclusive of border)
@@ -62,11 +68,17 @@ module CyberarmEngine
other.max.x <= max.x && other.max.y <= max.y && other.max.z <= max.z
end
# returns whether the vector is inside of the bounding box
# returns whether the 3D vector is inside of the bounding box
def inside?(vector)
(vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
(vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y)) &&
(vector.z.between?(@min.z, @max.z) || vector.z.between?(@max.z, @min.z))
end
# returns whether the 2D vector is inside of the bounding box
def point?(vector)
vector.x.between?(@min.x, @max.x) &&
vector.y.between?(@min.y, @max.y) &&
vector.z.between?(@min.z, @max.z)
(vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
(vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y))
end
def volume
@@ -87,26 +99,26 @@ module CyberarmEngine
def normalize(entity)
temp = BoundingBox.new
temp.min.x = @min.x.to_f * entity.scale
temp.min.y = @min.y.to_f * entity.scale
temp.min.z = @min.z.to_f * entity.scale
temp.min.x = @min.x.to_f * entity.scale.x
temp.min.y = @min.y.to_f * entity.scale.y
temp.min.z = @min.z.to_f * entity.scale.z
temp.max.x = @max.x.to_f * entity.scale
temp.max.y = @max.y.to_f * entity.scale
temp.max.z = @max.z.to_f * entity.scale
temp.max.x = @max.x.to_f * entity.scale.x
temp.max.y = @max.y.to_f * entity.scale.y
temp.max.z = @max.z.to_f * entity.scale.z
return temp
end
def normalize_with_offset(entity)
temp = BoundingBox.new
temp.min.x = @min.x.to_f * entity.scale + entity.position.x
temp.min.y = @min.y.to_f * entity.scale + entity.position.y
temp.min.z = @min.z.to_f * entity.scale + entity.position.z
temp.min.x = @min.x.to_f * entity.scale.x + entity.position.x
temp.min.y = @min.y.to_f * entity.scale.y + entity.position.y
temp.min.z = @min.z.to_f * entity.scale.z + entity.position.z
temp.max.x = @max.x.to_f * entity.scale + entity.position.x
temp.max.y = @max.y.to_f * entity.scale + entity.position.y
temp.max.z = @max.z.to_f * entity.scale + entity.position.z
temp.max.x = @max.x.to_f * entity.scale.x + entity.position.x
temp.max.y = @max.y.to_f * entity.scale.y + entity.position.y
temp.max.z = @max.z.to_f * entity.scale.z + entity.position.z
return temp
end

View File

@@ -1,11 +1,12 @@
module CyberarmEngine
class Ray
def initialize(origin, direction)
def initialize(origin, direction, range = Float::INFINITY)
raise "Origin must be a Vector!" unless origin.is_a?(Vector)
raise "Direction must be a Vector!" unless direction.is_a?(Vector)
@origin = origin
@direction = direction
@range = range
@inverse_direction = @direction.inverse
end
@@ -20,8 +21,8 @@ module CyberarmEngine
# Based on: https://tavianator.com/fast-branchless-raybounding-box-intersections/
def intersect_bounding_box?(box)
tmin = -Float::INFINITY
tmax = Float::INFINITY
tmin = -@range
tmax = @range
tx1 = (box.min.x - @origin.x) * @inverse_direction.x
tx2 = (box.max.x - @origin.x) * @inverse_direction.x

View File

@@ -2,14 +2,14 @@ module CyberarmEngine
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
class Shader
include OpenGL
@@shaders = {}
def self.add(name, instance)
@shaders ||= {}
@shaders[name] = instance
@@shaders[name] = instance
end
def self.use(name, &block)
shader = @shaders.dig(name)
shader = @@shaders.dig(name)
if shader
shader.use(&block)
else
@@ -17,6 +17,14 @@ module CyberarmEngine
end
end
def self.available?(name)
@@shaders.dig(name).is_a?(Shader)
end
def self.get(name)
@@shaders.dig(name)
end
def self.active_shader
@active_shader
end

View File

@@ -1,5 +1,29 @@
module CyberarmEngine
class Vector
def self.up
Vector.new(0, 1, 0)
end
def self.down
Vector.new(0, -1, 0)
end
def self.left
Vector.new(-1, 0, 0)
end
def self.right
Vector.new(1, 0, 0)
end
def self.forward
Vector.new(0, 0, 1)
end
def self.backward
Vector.new(0, 0, -1)
end
def initialize(x = 0, y = 0, z = 0, weight = 0)
@x, @y, @z, @weight = x, y, z, weight
end
@@ -138,6 +162,10 @@ module CyberarmEngine
@x + @y + @z
end
def lerp(other, factor)
(self - other) * factor.clamp(0.0, 1.0)
end
# 2D distance using X and Y
def distance(other)
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)

View File

@@ -32,6 +32,9 @@ module CyberarmEngine
@style.width = default(:width) || nil
@style.height = default(:height) || nil
@style.background_canvas = Background.new
@style.border_canvas = BorderCanvas.new(element: self)
stylize
default_events
@@ -44,9 +47,6 @@ module CyberarmEngine
set_margin(@style.margin)
@style.background_canvas = Background.new
@style.border_canvas = BorderCanvas.new(element: self)
set_background(@style.background)
set_border_color(@style.border_color)
end

View File

@@ -25,6 +25,8 @@ module CyberarmEngine
@style.background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color)
end
return :handled
end
def left_mouse_button(sender, x, y)
@@ -32,23 +34,33 @@ module CyberarmEngine
@style.background_canvas.background = default(:active, :background)
window.current_state.focus = self
@text.color = default(:active, :color)
return :handled
end
def released_left_mouse_button(sender,x, y)
enter(sender)
return :handled
end
def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block
return :handled
end
def leave(sender)
@style.background_canvas.background = default(:background)
@text.color = default(:color)
return :handled
end
def blur(sender)
@focus = false
return :handled
end
end
end

View File

@@ -67,8 +67,11 @@ module CyberarmEngine
@width = @style.width = window.width
@height = @style.height = window.height
else
@width, @height = 0, 0
_width = dimensional_size(@style.width, :width)
_height= dimensional_size(@style.height,:height)
@width = _width ? _width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
@height = _height ? _height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
end

View File

@@ -46,6 +46,8 @@ module CyberarmEngine
@caret_last_interval = Gosu.milliseconds
@show_caret = true
return :handled
end
def enter(sender)
@@ -56,12 +58,16 @@ module CyberarmEngine
@style.background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color)
end
return :handled
end
def leave(sender)
unless @focus
super
end
return :handled
end
def blur(sender)
@@ -69,6 +75,8 @@ module CyberarmEngine
@style.background_canvas.background = default(:background)
@text.color = default(:color)
window.text_input = nil
return :handled
end
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text

View File

@@ -19,6 +19,8 @@ module CyberarmEngine
def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block
return :handled
end
def recalculate

View File

@@ -13,11 +13,16 @@ module CyberarmEngine
def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block
return :handled
end
def recalculate
@width, @height = 0, 0
_width = dimensional_size(@style.width, :width)
_height= dimensional_size(@style.height,:height)
@width = _width ? _width : @text.width.round
@height= _height ? _height : @text.height.round

View File

@@ -24,6 +24,8 @@ module CyberarmEngine
toggle
@block.call(self) if @block
return :handled
end
def toggle

View File

@@ -22,8 +22,6 @@ module CyberarmEngine
@mouse_down_on = {}
@mouse_down_position = {}
@pending_recalculate_request = false
setup
end
# throws :blur event to focused element and sets GuiState focused element

View File

@@ -65,7 +65,7 @@ module CyberarmEngine
Button: { # < Label
margin: 1,
padding: 4,
border_thickness: 4,
border_thickness: 1,
border_color: ["ffd59674".hex, "ffff8746".hex],
border_radius: 0,
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],

View File

@@ -1,4 +1,4 @@
module CyberarmEngine
NAME = "InDev"
VERSION = "0.10.0"
VERSION = "0.11.1"
end