mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7cc733dee4 | |||
| 28c4acdedf | |||
| 57313a33a6 | |||
| 5d16500edd | |||
| 51dd3803fa | |||
| c6beab3e99 | |||
| 8ce594753e | |||
| ae44083cf2 | |||
| b25f76e124 | |||
| 8f9e671340 | |||
| bb482f4463 | |||
| 788d987da1 | |||
| 6fafe98008 | |||
| 002a01abfc | |||
| 6f7bac3880 | |||
| 993e59aa94 | |||
| e30ed7c6be | |||
| a172c24d97 | |||
| 003715f685 | |||
| 1458736f7d | |||
| a2e0c07c6a | |||
| 3ead2f5daf | |||
| 8f3d9ff193 |
@@ -1,18 +1,28 @@
|
|||||||
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"
|
require_relative "cyberarm_engine/version"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/common"
|
require_relative "cyberarm_engine/common"
|
||||||
|
|
||||||
|
require_relative "cyberarm_engine/gosu_ext/circle"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/game_object"
|
require_relative "cyberarm_engine/game_object"
|
||||||
require_relative "cyberarm_engine/engine"
|
require_relative "cyberarm_engine/engine"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/lib/bounding_box"
|
require_relative "cyberarm_engine/bounding_box"
|
||||||
require_relative "cyberarm_engine/lib/vector"
|
require_relative "cyberarm_engine/vector"
|
||||||
|
require_relative "cyberarm_engine/transform"
|
||||||
|
require_relative "cyberarm_engine/ray"
|
||||||
|
require_relative "cyberarm_engine/shader" if defined?(OpenGL)
|
||||||
require_relative "cyberarm_engine/background"
|
require_relative "cyberarm_engine/background"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/objects/text"
|
require_relative "cyberarm_engine/text"
|
||||||
require_relative "cyberarm_engine/objects/timer"
|
require_relative "cyberarm_engine/timer"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/ui/theme"
|
require_relative "cyberarm_engine/ui/theme"
|
||||||
require_relative "cyberarm_engine/ui/event"
|
require_relative "cyberarm_engine/ui/event"
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
require "gosu"
|
|
||||||
|
|
||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Background
|
class Background
|
||||||
attr_accessor :x, :y, :z, :width, :height, :angle, :debug
|
attr_accessor :x, :y, :z, :width, :height, :angle, :debug
|
||||||
|
|||||||
@@ -49,11 +49,17 @@ module CyberarmEngine
|
|||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
|
|
||||||
# returns whether both bounding boxes intersect
|
# returns whether bounding box intersects other
|
||||||
def intersect?(other)
|
def intersect?(other)
|
||||||
(@min.x <= other.max.x && @max.x >= other.min.x) &&
|
if other.is_a?(Ray)
|
||||||
(@min.y <= other.max.y && @max.y >= other.min.y) &&
|
other.intersect?(self)
|
||||||
(@min.z <= other.max.z && @max.z >= other.min.z)
|
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
|
end
|
||||||
|
|
||||||
# does this bounding box envelop other bounding box? (inclusive of border)
|
# 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
|
other.max.x <= max.x && other.max.y <= max.y && other.max.z <= max.z
|
||||||
end
|
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)
|
def point?(vector)
|
||||||
vector.x.between?(@min.x, @max.x) &&
|
(vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
|
||||||
vector.y.between?(@min.y, @max.y) &&
|
(vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y))
|
||||||
vector.z.between?(@min.z, @max.z)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def volume
|
def volume
|
||||||
@@ -87,26 +99,26 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def normalize(entity)
|
def normalize(entity)
|
||||||
temp = BoundingBox.new
|
temp = BoundingBox.new
|
||||||
temp.min.x = @min.x.to_f * entity.scale
|
temp.min.x = @min.x.to_f * entity.scale.x
|
||||||
temp.min.y = @min.y.to_f * entity.scale
|
temp.min.y = @min.y.to_f * entity.scale.y
|
||||||
temp.min.z = @min.z.to_f * entity.scale
|
temp.min.z = @min.z.to_f * entity.scale.z
|
||||||
|
|
||||||
temp.max.x = @max.x.to_f * entity.scale
|
temp.max.x = @max.x.to_f * entity.scale.x
|
||||||
temp.max.y = @max.y.to_f * entity.scale
|
temp.max.y = @max.y.to_f * entity.scale.y
|
||||||
temp.max.z = @max.z.to_f * entity.scale
|
temp.max.z = @max.z.to_f * entity.scale.z
|
||||||
|
|
||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize_with_offset(entity)
|
def normalize_with_offset(entity)
|
||||||
temp = BoundingBox.new
|
temp = BoundingBox.new
|
||||||
temp.min.x = @min.x.to_f * entity.scale + entity.position.x
|
temp.min.x = @min.x.to_f * entity.scale.x + entity.position.x
|
||||||
temp.min.y = @min.y.to_f * entity.scale + entity.position.y
|
temp.min.y = @min.y.to_f * entity.scale.y + entity.position.y
|
||||||
temp.min.z = @min.z.to_f * entity.scale + entity.position.z
|
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.x = @max.x.to_f * entity.scale.x + entity.position.x
|
||||||
temp.max.y = @max.y.to_f * entity.scale + entity.position.y
|
temp.max.y = @max.y.to_f * entity.scale.y + entity.position.y
|
||||||
temp.max.z = @max.z.to_f * entity.scale + entity.position.z
|
temp.max.z = @max.z.to_f * entity.scale.z + entity.position.z
|
||||||
|
|
||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
@@ -1,50 +1,50 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
module Common
|
module Common
|
||||||
def push_state(klass, options={})
|
def push_state(klass, options={})
|
||||||
$window.push_state(klass, options)
|
window.push_state(klass, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_state
|
def current_state
|
||||||
$window.current_state
|
window.current_state
|
||||||
end
|
end
|
||||||
|
|
||||||
def previous_state
|
def previous_state
|
||||||
$window.previous_state
|
window.previous_state
|
||||||
end
|
end
|
||||||
|
|
||||||
def pop_state
|
def pop_state
|
||||||
$window.pop_state
|
window.pop_state
|
||||||
end
|
end
|
||||||
|
|
||||||
def show_cursor
|
def show_cursor
|
||||||
$window.show_cursor
|
window.show_cursor
|
||||||
end
|
end
|
||||||
|
|
||||||
def show_cursor=boolean
|
def show_cursor=boolean
|
||||||
$window.show_cursor = boolean
|
window.show_cursor = boolean
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw_rect(x, y, width, height, color, z = 0)
|
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
|
end
|
||||||
|
|
||||||
def fill(color, z = 0)
|
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
|
end
|
||||||
|
|
||||||
def lighten(color, amount = 25)
|
def lighten(color, amount = 25)
|
||||||
if defined?(color.alpha)
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def darken(color, amount = 25)
|
def darken(color, amount = 25)
|
||||||
if defined?(color.alpha)
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ module CyberarmEngine
|
|||||||
return Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
|
return Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_asset(path, hash, klass)
|
def get_asset(path, hash, klass, retro = false, tileable = false)
|
||||||
asset = nil
|
asset = nil
|
||||||
hash.detect do |_asset, instance|
|
hash.detect do |_asset, instance|
|
||||||
if _asset == path
|
if _asset == path
|
||||||
@@ -64,7 +64,12 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
unless asset
|
unless asset
|
||||||
instance = klass.new(path)
|
instance = nil
|
||||||
|
if klass == Gosu::Image
|
||||||
|
instance = klass.new(path, retro: retro, tileable: tileable)
|
||||||
|
else
|
||||||
|
instance = klass.new(path)
|
||||||
|
end
|
||||||
hash[path] = instance
|
hash[path] = instance
|
||||||
asset = instance
|
asset = instance
|
||||||
end
|
end
|
||||||
@@ -72,8 +77,8 @@ module CyberarmEngine
|
|||||||
return asset
|
return asset
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_image(path)
|
def get_image(path, retro: false, tileable: false)
|
||||||
get_asset(path, Engine::IMAGES, Gosu::Image)
|
get_asset(path, Engine::IMAGES, Gosu::Image, retro, tileable)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_sample(path)
|
def get_sample(path)
|
||||||
@@ -88,4 +93,4 @@ module CyberarmEngine
|
|||||||
$window
|
$window
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ module CyberarmEngine
|
|||||||
SONGS = {}
|
SONGS = {}
|
||||||
|
|
||||||
attr_accessor :show_cursor
|
attr_accessor :show_cursor
|
||||||
attr_reader :current_state, :last_frame_time
|
attr_reader :last_frame_time
|
||||||
|
|
||||||
def self.now
|
def self.now
|
||||||
Gosu.milliseconds
|
Gosu.milliseconds
|
||||||
@@ -58,11 +58,15 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def push_state(klass, options={})
|
def push_state(klass, options={})
|
||||||
|
options = {setup: true}.merge(options)
|
||||||
|
|
||||||
if klass.instance_of?(klass.class) && defined?(klass.options)
|
if klass.instance_of?(klass.class) && defined?(klass.options)
|
||||||
@states << klass
|
@states << klass
|
||||||
|
klass.setup if options[:setup]
|
||||||
else
|
else
|
||||||
@states << klass.new(options) if child_of?(klass, GameState)
|
@states << klass.new(options) if child_of?(klass, GameState)
|
||||||
@states << klass.new if child_of?(klass, Element::Container)
|
@states << klass.new if child_of?(klass, Element::Container)
|
||||||
|
current_state.setup if current_state.class == klass && options[:setup]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -94,4 +98,4 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ module CyberarmEngine
|
|||||||
include Common
|
include Common
|
||||||
|
|
||||||
attr_accessor :image, :angle, :position, :velocity, :center_x, :center_y, :scale_x, :scale_y,
|
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={})
|
def initialize(options={})
|
||||||
if options[:auto_manage] || options[:auto_manage] == nil
|
if options[:auto_manage] || options[:auto_manage] == nil
|
||||||
$window.current_state.add_game_object(self)
|
$window.current_state.add_game_object(self)
|
||||||
@@ -141,7 +142,7 @@ module CyberarmEngine
|
|||||||
self.angle%=360
|
self.angle%=360
|
||||||
end
|
end
|
||||||
|
|
||||||
def alpha=int # 0-255
|
def alpha=(int) # 0-255
|
||||||
@alpha = int
|
@alpha = int
|
||||||
@alpha = 255 if @alpha > 255
|
@alpha = 255 if @alpha > 255
|
||||||
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
|
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
|
||||||
@@ -154,7 +155,7 @@ module CyberarmEngine
|
|||||||
def button_up(id)
|
def button_up(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down?(id)
|
def button_down(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_closest(game_object_class)
|
def find_closest(game_object_class)
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ module CyberarmEngine
|
|||||||
$window.text_input = nil unless options[:preserve_text_input]
|
$window.text_input = nil unless options[:preserve_text_input]
|
||||||
|
|
||||||
@down_keys = {}
|
@down_keys = {}
|
||||||
|
|
||||||
setup
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
|||||||
9
lib/cyberarm_engine/gosu_ext/circle.rb
Normal file
9
lib/cyberarm_engine/gosu_ext/circle.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
module Gosu
|
||||||
|
# Sourced from https://gist.github.com/ippa/662583
|
||||||
|
def self.draw_circle(cx,cy,r, z = 9999,color = Gosu::Color::GREEN, step = 10)
|
||||||
|
0.step(360, step) do |a1|
|
||||||
|
a2 = a1 + step
|
||||||
|
draw_line(cx + Gosu.offset_x(a1, r), cy + Gosu.offset_y(a1, r), color, cx + Gosu.offset_x(a2, r), cy + Gosu.offset_y(a2, r), color, z)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
56
lib/cyberarm_engine/ray.rb
Normal file
56
lib/cyberarm_engine/ray.rb
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Ray
|
||||||
|
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
|
||||||
|
|
||||||
|
def intersect?(intersectable)
|
||||||
|
if intersectable.is_a?(BoundingBox)
|
||||||
|
intersect_bounding_box?(intersectable)
|
||||||
|
else
|
||||||
|
raise NotImplementedError, "Ray intersection test for #{intersectable.class} not implemented."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Based on: https://tavianator.com/fast-branchless-raybounding-box-intersections/
|
||||||
|
def intersect_bounding_box?(box)
|
||||||
|
tmin = -@range
|
||||||
|
tmax = @range
|
||||||
|
|
||||||
|
tx1 = (box.min.x - @origin.x) * @inverse_direction.x
|
||||||
|
tx2 = (box.max.x - @origin.x) * @inverse_direction.x
|
||||||
|
|
||||||
|
tmin = max(tmin, min(tx1, tx2))
|
||||||
|
tmax = min(tmax, max(tx1, tx2))
|
||||||
|
|
||||||
|
ty1 = (box.min.y - @origin.y) * @inverse_direction.y
|
||||||
|
ty2 = (box.max.y - @origin.y) * @inverse_direction.y
|
||||||
|
|
||||||
|
tmin = max(tmin, min(ty1, ty2))
|
||||||
|
tmax = min(tmax, max(ty1, ty2))
|
||||||
|
|
||||||
|
tz1 = (box.min.z - @origin.z) * @inverse_direction.z
|
||||||
|
tz2 = (box.max.z - @origin.z) * @inverse_direction.z
|
||||||
|
|
||||||
|
tmin = max(tmin, min(tz1, tz2))
|
||||||
|
tmax = min(tmax, max(tz1, tz2))
|
||||||
|
|
||||||
|
return tmax >= max(tmin, 0.0);
|
||||||
|
end
|
||||||
|
|
||||||
|
def min(x, y)
|
||||||
|
((x) < (y) ? (x) : (y))
|
||||||
|
end
|
||||||
|
|
||||||
|
def max(x, y)
|
||||||
|
((x) > (y) ? (x) : (y))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
205
lib/cyberarm_engine/shader.rb
Normal file
205
lib/cyberarm_engine/shader.rb
Normal file
@@ -0,0 +1,205 @@
|
|||||||
|
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[name] = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.use(name, &block)
|
||||||
|
shader = @@shaders.dig(name)
|
||||||
|
if shader
|
||||||
|
shader.use(&block)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Shader '#{name}' not found!"
|
||||||
|
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
|
||||||
|
|
||||||
|
def self.active_shader=(instance)
|
||||||
|
@active_shader = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.stop
|
||||||
|
shader = Shader.active_shader
|
||||||
|
|
||||||
|
if shader
|
||||||
|
shader.stop
|
||||||
|
else
|
||||||
|
raise ArgumentError, "No active shader to stop!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.attribute_location(variable)
|
||||||
|
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||||
|
Shader.active_shader.attribute_location(variable)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.set_uniform(variable, value)
|
||||||
|
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||||
|
Shader.active_shader.set_uniform(variable, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_reader :name, :program
|
||||||
|
def initialize(name:, vertex: "shaders/default.vert", fragment:)
|
||||||
|
@name = name
|
||||||
|
@vertex_file = vertex
|
||||||
|
@fragment_file = fragment
|
||||||
|
@compiled = false
|
||||||
|
|
||||||
|
@program = nil
|
||||||
|
|
||||||
|
@error_buffer_size = 1024
|
||||||
|
@variable_missing = {}
|
||||||
|
|
||||||
|
raise ArgumentError, "Shader files not found: #{@vertex_file} or #{@fragment_file}" unless shader_files_exist?
|
||||||
|
|
||||||
|
create_shaders
|
||||||
|
compile_shaders
|
||||||
|
|
||||||
|
# Only add shader if it successfully compiles
|
||||||
|
if @compiled
|
||||||
|
Shader.add(@name, self)
|
||||||
|
else
|
||||||
|
puts "FAILED to compile shader: #{@name}", ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def shader_files_exist?
|
||||||
|
File.exist?(@vertex_file) && File.exist?(@fragment_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_shaders
|
||||||
|
@vertex = glCreateShader(GL_VERTEX_SHADER)
|
||||||
|
@fragment = glCreateShader(GL_FRAGMENT_SHADER)
|
||||||
|
|
||||||
|
source = [File.read(@vertex_file)].pack('p')
|
||||||
|
size = [File.size(@vertex_file)].pack('I')
|
||||||
|
glShaderSource(@vertex, 1, source, size)
|
||||||
|
|
||||||
|
source = [File.read(@fragment_file)].pack('p')
|
||||||
|
size = [File.size(@fragment_file)].pack('I')
|
||||||
|
glShaderSource(@fragment, 1, source, size)
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile_shaders
|
||||||
|
return unless shader_files_exist?
|
||||||
|
|
||||||
|
glCompileShader(@vertex)
|
||||||
|
buffer = ' '
|
||||||
|
glGetShaderiv(@vertex, GL_COMPILE_STATUS, buffer)
|
||||||
|
compiled = buffer.unpack('L')[0]
|
||||||
|
|
||||||
|
if compiled == 0
|
||||||
|
log = ' ' * @error_buffer_size
|
||||||
|
glGetShaderInfoLog(@vertex, @error_buffer_size, nil, log)
|
||||||
|
puts "Shader Error: Program \"#{@name}\""
|
||||||
|
puts " Vectex Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||||
|
puts " Shader Compiled status: #{compiled}"
|
||||||
|
puts " NOTE: assignment of uniforms in shaders is illegal!"
|
||||||
|
puts
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
glCompileShader(@fragment)
|
||||||
|
buffer = ' '
|
||||||
|
glGetShaderiv(@fragment, GL_COMPILE_STATUS, buffer)
|
||||||
|
compiled = buffer.unpack('L')[0]
|
||||||
|
|
||||||
|
if compiled == 0
|
||||||
|
log = ' ' * @error_buffer_size
|
||||||
|
glGetShaderInfoLog(@fragment, @error_buffer_size, nil, log)
|
||||||
|
puts "Shader Error: Program \"#{@name}\""
|
||||||
|
puts " Fragment Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||||
|
puts " Shader Compiled status: #{compiled}"
|
||||||
|
puts " NOTE: assignment of uniforms in shader is illegal!"
|
||||||
|
puts
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
@program = glCreateProgram
|
||||||
|
glAttachShader(@program, @vertex)
|
||||||
|
glAttachShader(@program, @fragment)
|
||||||
|
glLinkProgram(@program)
|
||||||
|
|
||||||
|
buffer = ' '
|
||||||
|
glGetProgramiv(@program, GL_LINK_STATUS, buffer)
|
||||||
|
linked = buffer.unpack('L')[0]
|
||||||
|
|
||||||
|
if linked == 0
|
||||||
|
log = ' ' * @error_buffer_size
|
||||||
|
glGetProgramInfoLog(@program, @error_buffer_size, nil, log)
|
||||||
|
puts "Shader Error: Program \"#{@name}\""
|
||||||
|
puts " Program InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
@compiled = linked == 0 ? false : true
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the location of a uniform variable
|
||||||
|
def variable(variable)
|
||||||
|
loc = glGetUniformLocation(@program, variable)
|
||||||
|
if (loc == -1)
|
||||||
|
puts "Shader Error: Program \"#{@name}\" has no such uniform named \"#{variable}\"", " Is it used in the shader? GLSL may have optimized it out.", " Is it miss spelled?" unless @variable_missing[variable]
|
||||||
|
@variable_missing[variable] = true
|
||||||
|
end
|
||||||
|
return loc
|
||||||
|
end
|
||||||
|
|
||||||
|
def use(&block)
|
||||||
|
return unless compiled?
|
||||||
|
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
||||||
|
Shader.active_shader=self
|
||||||
|
|
||||||
|
glUseProgram(@program)
|
||||||
|
|
||||||
|
if block
|
||||||
|
block.call(self)
|
||||||
|
stop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop
|
||||||
|
Shader.active_shader = nil if Shader.active_shader == self
|
||||||
|
glUseProgram(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def compiled?
|
||||||
|
@compiled
|
||||||
|
end
|
||||||
|
|
||||||
|
def attribute_location(variable)
|
||||||
|
glGetUniformLocation(@program, variable)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_uniform(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
case value.class.to_s.downcase.to_sym
|
||||||
|
when :integer
|
||||||
|
glUniform1i(attr_loc, value)
|
||||||
|
when :float
|
||||||
|
glUniform1f(attr_loc, value)
|
||||||
|
when :string
|
||||||
|
when :array
|
||||||
|
else
|
||||||
|
raise NotImplementedError, "Shader support for #{value.class.inspect} not implemented."
|
||||||
|
end
|
||||||
|
|
||||||
|
Window.handle_gl_error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,8 +2,8 @@ module CyberarmEngine
|
|||||||
class Text
|
class Text
|
||||||
CACHE = {}
|
CACHE = {}
|
||||||
|
|
||||||
attr_accessor :x, :y, :z, :size, :factor_x, :factor_y, :color, :shadow, :shadow_size, :options
|
attr_accessor :x, :y, :z, :size, :options
|
||||||
attr_reader :text, :textobject
|
attr_reader :text, :textobject, :factor_x, :factor_y, :color, :shadow, :shadow_size, :shadow_alpha, :shadow_color
|
||||||
|
|
||||||
def initialize(text, options={})
|
def initialize(text, options={})
|
||||||
@text = text.to_s || ""
|
@text = text.to_s || ""
|
||||||
@@ -22,6 +22,8 @@ module CyberarmEngine
|
|||||||
@shadow = true if options[:shadow] == nil
|
@shadow = true if options[:shadow] == nil
|
||||||
@shadow_size = options[:shadow_size] ? options[:shadow_size] : 1
|
@shadow_size = options[:shadow_size] ? options[:shadow_size] : 1
|
||||||
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
||||||
|
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
||||||
|
@shadow_color= options[:shadow_color]
|
||||||
@textobject = check_cache(@size, @font)
|
@textobject = check_cache(@size, @font)
|
||||||
|
|
||||||
if @alignment
|
if @alignment
|
||||||
@@ -67,6 +69,35 @@ module CyberarmEngine
|
|||||||
@text = string
|
@text = string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def factor_x=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@factor_x = n
|
||||||
|
end
|
||||||
|
def factor_y=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@factor_y = n
|
||||||
|
end
|
||||||
|
def color=(color)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@color = color
|
||||||
|
end
|
||||||
|
def shadow=(boolean)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@shadow = boolean
|
||||||
|
end
|
||||||
|
def shadow_size=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@shadow_size = n
|
||||||
|
end
|
||||||
|
def shadow_alpha=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@shadow_alpha = n
|
||||||
|
end
|
||||||
|
def shadow_color=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@shadow_color = n
|
||||||
|
end
|
||||||
|
|
||||||
def width
|
def width
|
||||||
textobject.text_width(@text)
|
textobject.text_width(@text)
|
||||||
end
|
end
|
||||||
@@ -77,9 +108,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def draw
|
def draw
|
||||||
if @shadow && !ARGV.join.include?("--no-shadow")
|
if @shadow && !ARGV.join.include?("--no-shadow")
|
||||||
@shadow_alpha = 30 if @color.alpha > 30
|
shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha
|
||||||
@shadow_alpha = @color.alpha if @color.alpha <= 30
|
shadow_color = @shadow_color ? @shadow_color : Gosu::Color.rgba(@color.red, @color.green, @color.blue, shadow_alpha)
|
||||||
shadow_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha)
|
|
||||||
|
|
||||||
_x = @shadow_size
|
_x = @shadow_size
|
||||||
_y = @shadow_size
|
_y = @shadow_size
|
||||||
84
lib/cyberarm_engine/transform.rb
Normal file
84
lib/cyberarm_engine/transform.rb
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Transform
|
||||||
|
attr_reader :elements
|
||||||
|
def initialize(matrix)
|
||||||
|
@elements = matrix
|
||||||
|
|
||||||
|
raise "Transform is wrong size! Got #{@elements.size}, expected 16" if 16 != @elements.size
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.rotate(angle, rotate_around = nil)
|
||||||
|
double c = Math.cos(angle).degrees_to_radians
|
||||||
|
double s = Math.sin(angle).degrees_to_radians
|
||||||
|
matrix = [
|
||||||
|
+c, +s, 0, 0,
|
||||||
|
-s, +c, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1,
|
||||||
|
]
|
||||||
|
|
||||||
|
rotate_matrix = Transform.new(matrix, rows: 4, columns: 4)
|
||||||
|
|
||||||
|
if rotate_around && (rotate_around.x != 0.0 || rotate_around.y != 0.0)
|
||||||
|
negative_rotate_around = Vector.new(-rotate_around.x, -rotate_around.y, -rotate_around.z)
|
||||||
|
|
||||||
|
rotate_matrix = concat(
|
||||||
|
concat(translate(negative_rotate_around), rotate_matrix),
|
||||||
|
translate(rotate_around)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
return rotate_matrix
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.translate(vector)
|
||||||
|
x, y, z = vector.to_a[0..2]
|
||||||
|
matrix = [
|
||||||
|
1, 0, 0, 0,
|
||||||
|
0, 1, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
x, y, z, 1,
|
||||||
|
]
|
||||||
|
|
||||||
|
Transform.new(matrix)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.scale(vector, center_around = nil)
|
||||||
|
scale_x, scale_y, scale_z = vector.to_a[0..2]
|
||||||
|
matrix = [
|
||||||
|
scale_x, 0, 0, 0,
|
||||||
|
0, scale_y, 0, 0,
|
||||||
|
0, 0, scale_z, 0,
|
||||||
|
0, 0, 0, 1,
|
||||||
|
]
|
||||||
|
|
||||||
|
scale_matrix = Transform.new(matrix)
|
||||||
|
|
||||||
|
if center_around && (center_around.x != 0.0 || center_around.y != 0.0)
|
||||||
|
negative_center_around = Vector.new(-center_around.x, -center_around.y, -center_around.z)
|
||||||
|
|
||||||
|
scale_matrix = concat(
|
||||||
|
concat(translate(negative_center_around), scale_matrix),
|
||||||
|
translate(center_around)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
return scale_matrix
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.concat(left, right)
|
||||||
|
matrix = Array.new(left.elements.size)
|
||||||
|
rows = 4
|
||||||
|
|
||||||
|
matrix.size.times do |i|
|
||||||
|
matrix[i] = 0
|
||||||
|
|
||||||
|
rows.times do |j|
|
||||||
|
matrix[i] += left.elements[i / rows * rows + j] * right.elements[i % rows + j * rows]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Transform.new(matrix)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -32,6 +32,9 @@ module CyberarmEngine
|
|||||||
@style.width = default(:width) || nil
|
@style.width = default(:width) || nil
|
||||||
@style.height = default(:height) || nil
|
@style.height = default(:height) || nil
|
||||||
|
|
||||||
|
@style.background_canvas = Background.new
|
||||||
|
@style.border_canvas = BorderCanvas.new(element: self)
|
||||||
|
|
||||||
stylize
|
stylize
|
||||||
|
|
||||||
default_events
|
default_events
|
||||||
@@ -44,9 +47,6 @@ module CyberarmEngine
|
|||||||
|
|
||||||
set_margin(@style.margin)
|
set_margin(@style.margin)
|
||||||
|
|
||||||
@style.background_canvas = Background.new
|
|
||||||
@style.border_canvas = BorderCanvas.new(element: self)
|
|
||||||
|
|
||||||
set_background(@style.background)
|
set_background(@style.background)
|
||||||
set_border_color(@style.border_color)
|
set_border_color(@style.border_color)
|
||||||
end
|
end
|
||||||
@@ -162,7 +162,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def width
|
def width
|
||||||
if visible?
|
if visible?
|
||||||
(@style.border_thickness_left + @style.padding_left) + @width + (@style.padding_right + @style.border_thickness_right)
|
inner_width + @width
|
||||||
else
|
else
|
||||||
0
|
0
|
||||||
end
|
end
|
||||||
@@ -172,9 +172,13 @@ module CyberarmEngine
|
|||||||
@style.margin_left + width + @style.margin_right
|
@style.margin_left + width + @style.margin_right
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inner_width
|
||||||
|
(@style.border_thickness_left + @style.padding_left) + (@style.padding_right + @style.border_thickness_right)
|
||||||
|
end
|
||||||
|
|
||||||
def height
|
def height
|
||||||
if visible?
|
if visible?
|
||||||
(@style.border_thickness_top + @style.padding_top) + @height + (@style.padding_bottom + @style.border_thickness_bottom)
|
inner_height + @height
|
||||||
else
|
else
|
||||||
0
|
0
|
||||||
end
|
end
|
||||||
@@ -184,6 +188,10 @@ module CyberarmEngine
|
|||||||
@style.margin_top + height + @style.margin_bottom
|
@style.margin_top + height + @style.margin_bottom
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inner_height
|
||||||
|
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||||
|
end
|
||||||
|
|
||||||
private def dimensional_size(size, dimension)
|
private def dimensional_size(size, dimension)
|
||||||
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
||||||
if size && size.is_a?(Numeric)
|
if size && size.is_a?(Numeric)
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ module CyberarmEngine
|
|||||||
@style.background_canvas.background = default(:hover, :background)
|
@style.background_canvas.background = default(:hover, :background)
|
||||||
@text.color = default(:hover, :color)
|
@text.color = default(:hover, :color)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def left_mouse_button(sender, x, y)
|
def left_mouse_button(sender, x, y)
|
||||||
@@ -32,23 +34,33 @@ module CyberarmEngine
|
|||||||
@style.background_canvas.background = default(:active, :background)
|
@style.background_canvas.background = default(:active, :background)
|
||||||
window.current_state.focus = self
|
window.current_state.focus = self
|
||||||
@text.color = default(:active, :color)
|
@text.color = default(:active, :color)
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def released_left_mouse_button(sender,x, y)
|
def released_left_mouse_button(sender,x, y)
|
||||||
enter(sender)
|
enter(sender)
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def clicked_left_mouse_button(sender, x, y)
|
def clicked_left_mouse_button(sender, x, y)
|
||||||
@block.call(self) if @block
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def leave(sender)
|
def leave(sender)
|
||||||
@style.background_canvas.background = default(:background)
|
@style.background_canvas.background = default(:background)
|
||||||
@text.color = default(:color)
|
@text.color = default(:color)
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def blur(sender)
|
def blur(sender)
|
||||||
@focus = false
|
@focus = false
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -67,8 +67,11 @@ module CyberarmEngine
|
|||||||
@width = @style.width = window.width
|
@width = @style.width = window.width
|
||||||
@height = @style.height = window.height
|
@height = @style.height = window.height
|
||||||
else
|
else
|
||||||
|
@width, @height = 0, 0
|
||||||
|
|
||||||
_width = dimensional_size(@style.width, :width)
|
_width = dimensional_size(@style.width, :width)
|
||||||
_height= dimensional_size(@style.height,:height)
|
_height= dimensional_size(@style.height,:height)
|
||||||
|
|
||||||
@width = _width ? _width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
|
@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
|
@height = _height ? _height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ module CyberarmEngine
|
|||||||
def left_mouse_button(sender, x, y)
|
def left_mouse_button(sender, x, y)
|
||||||
super
|
super
|
||||||
window.text_input = @text_input
|
window.text_input = @text_input
|
||||||
|
|
||||||
|
@caret_last_interval = Gosu.milliseconds
|
||||||
|
@show_caret = true
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def enter(sender)
|
def enter(sender)
|
||||||
@@ -53,12 +58,16 @@ module CyberarmEngine
|
|||||||
@style.background_canvas.background = default(:hover, :background)
|
@style.background_canvas.background = default(:hover, :background)
|
||||||
@text.color = default(:hover, :color)
|
@text.color = default(:hover, :color)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def leave(sender)
|
def leave(sender)
|
||||||
unless @focus
|
unless @focus
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def blur(sender)
|
def blur(sender)
|
||||||
@@ -66,6 +75,8 @@ module CyberarmEngine
|
|||||||
@style.background_canvas.background = default(:background)
|
@style.background_canvas.background = default(:background)
|
||||||
@text.color = default(:color)
|
@text.color = default(:color)
|
||||||
window.text_input = nil
|
window.text_input = nil
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
|
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def clicked_left_mouse_button(sender, x, y)
|
def clicked_left_mouse_button(sender, x, y)
|
||||||
@block.call(self) if @block
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
|
|||||||
@@ -13,11 +13,16 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def clicked_left_mouse_button(sender, x, y)
|
def clicked_left_mouse_button(sender, x, y)
|
||||||
@block.call(self) if @block
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
|
@width, @height = 0, 0
|
||||||
|
|
||||||
_width = dimensional_size(@style.width, :width)
|
_width = dimensional_size(@style.width, :width)
|
||||||
_height= dimensional_size(@style.height,:height)
|
_height= dimensional_size(@style.height,:height)
|
||||||
|
|
||||||
@width = _width ? _width : @text.width.round
|
@width = _width ? _width : @text.width.round
|
||||||
@height= _height ? _height : @text.height.round
|
@height= _height ? _height : @text.height.round
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ module CyberarmEngine
|
|||||||
toggle
|
toggle
|
||||||
|
|
||||||
@block.call(self) if @block
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle
|
def toggle
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ module CyberarmEngine
|
|||||||
@mouse_down_on = {}
|
@mouse_down_on = {}
|
||||||
@mouse_down_position = {}
|
@mouse_down_position = {}
|
||||||
@pending_recalculate_request = false
|
@pending_recalculate_request = false
|
||||||
|
|
||||||
setup
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# throws :blur event to focused element and sets GuiState focused element
|
# throws :blur event to focused element and sets GuiState focused element
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ module CyberarmEngine
|
|||||||
Button: { # < Label
|
Button: { # < Label
|
||||||
margin: 1,
|
margin: 1,
|
||||||
padding: 4,
|
padding: 4,
|
||||||
border_thickness: 4,
|
border_thickness: 1,
|
||||||
border_color: ["ffd59674".hex, "ffff8746".hex],
|
border_color: ["ffd59674".hex, "ffff8746".hex],
|
||||||
border_radius: 0,
|
border_radius: 0,
|
||||||
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
|
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
|
||||||
|
|||||||
@@ -1,5 +1,29 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Vector
|
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)
|
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
||||||
@x, @y, @z, @weight = x, y, z, weight
|
@x, @y, @z, @weight = x, y, z, weight
|
||||||
end
|
end
|
||||||
@@ -16,6 +40,9 @@ module CyberarmEngine
|
|||||||
def weight; @weight; end
|
def weight; @weight; end
|
||||||
def weight=(n); @weight = n; end
|
def weight=(n); @weight = n; end
|
||||||
|
|
||||||
|
alias w weight
|
||||||
|
alias w= weight=
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
@x == other &&
|
@x == other &&
|
||||||
@@ -138,6 +165,10 @@ module CyberarmEngine
|
|||||||
@x + @y + @z
|
@x + @y + @z
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def lerp(other, factor)
|
||||||
|
(self - other) * factor.clamp(0.0, 1.0)
|
||||||
|
end
|
||||||
|
|
||||||
# 2D distance using X and Y
|
# 2D distance using X and Y
|
||||||
def distance(other)
|
def distance(other)
|
||||||
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
NAME = "InDev"
|
NAME = "InDev"
|
||||||
VERSION = "0.9.0"
|
VERSION = "0.12.1"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user