mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 21:22:33 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 805077d232 | |||
| db1a0683e2 | |||
| f489ad162f | |||
| df2dc25e34 | |||
| 6b319935d3 | |||
| f9d551a419 | |||
| f34dd21644 | |||
| 025f54a752 | |||
| cbc27b5d5d |
@@ -48,6 +48,12 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
def opacity(color, ratio = 1.0)
|
||||
alpha = 255 * ratio
|
||||
|
||||
return Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
|
||||
end
|
||||
|
||||
def get_asset(path, hash, klass)
|
||||
asset = nil
|
||||
hash.detect do |_asset, instance|
|
||||
|
||||
@@ -15,10 +15,10 @@ module CyberarmEngine
|
||||
$window.last_frame_time/1000.0
|
||||
end
|
||||
|
||||
def initialize(width = 800, height = 600, fullscreen = false, update_interval = 1000.0/60)
|
||||
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0/60, resizable: false)
|
||||
@show_cursor = false
|
||||
|
||||
super(width, height, fullscreen, update_interval)
|
||||
super(width, height, fullscreen: fullscreen, update_interval: update_interval, resizable: resizable)
|
||||
$window = self
|
||||
@last_frame_time = Gosu.milliseconds-1
|
||||
@current_frame_time = Gosu.milliseconds
|
||||
|
||||
@@ -2,9 +2,23 @@ module CyberarmEngine
|
||||
class BoundingBox
|
||||
attr_accessor :min, :max
|
||||
|
||||
def initialize(minx = 0, miny = 0, minz = 0, maxx = 0, maxy = 0, maxz = 0)
|
||||
@min = Vector.new(minx, miny, minz)
|
||||
@max = Vector.new(maxx, maxy, maxz)
|
||||
def initialize(*args)
|
||||
case args.size
|
||||
when 0
|
||||
@min = Vector.new(0, 0, 0)
|
||||
@max = Vector.new(0, 0, 0)
|
||||
when 2
|
||||
@min = args.first.clone
|
||||
@max = args.last.clone
|
||||
when 4
|
||||
@min = Vector.new(args[0], args[1], 0)
|
||||
@max = Vector.new(args[2], args[3], 0)
|
||||
when 6
|
||||
@min = Vector.new(args[0], args[1], args[2])
|
||||
@max = Vector.new(args[3], args[4], args[5])
|
||||
else
|
||||
raise "Invalid number of arguments! Got: #{args.size}, expected: 0, 2, 4, or 6."
|
||||
end
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
module CyberarmEngine
|
||||
class Vector
|
||||
|
||||
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
||||
@x, @y, @z, @weight = x, y, z, weight
|
||||
end
|
||||
@@ -17,10 +16,6 @@ module CyberarmEngine
|
||||
def weight; @weight; end
|
||||
def weight=(n); @weight = n; end
|
||||
|
||||
# def xy=(nx, ny); @x = nx; @y = ny; end
|
||||
# def xyz=(nx, ny, nz); @x = nx; @y = ny; @z = nz; end
|
||||
# def xyzw=(nx, ny, nz, nw); @x = nx; @y = ny; @z = nz; @weight = nw; end
|
||||
|
||||
def ==(other)
|
||||
if other.is_a?(Numeric)
|
||||
@x == other &&
|
||||
@@ -35,41 +30,83 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
# Performs math operation, excluding @weight
|
||||
private def operator(function, other)
|
||||
if other.is_a?(Numeric)
|
||||
Vector.new(
|
||||
@x.send(:"#{function}", other),
|
||||
@y.send(:"#{function}", other),
|
||||
@z.send(:"#{function}", other)
|
||||
)
|
||||
else
|
||||
Vector.new(
|
||||
@x.send(:"#{function}", other.x),
|
||||
@y.send(:"#{function}", other.y),
|
||||
@z.send(:"#{function}", other.z)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# Adds Vector and Numberic or Vector and Vector, excluding @weight
|
||||
def +(other)
|
||||
Vector.new(
|
||||
@x + other.x,
|
||||
@y + other.y,
|
||||
@z + other.z,
|
||||
@weight + other.weight
|
||||
)
|
||||
operator("+", other)
|
||||
end
|
||||
|
||||
# Subtracts Vector and Numberic or Vector and Vector, excluding @weight
|
||||
def -(other)
|
||||
Vector.new(
|
||||
@x - other.x,
|
||||
@y - other.y,
|
||||
@z - other.z,
|
||||
@weight - other.weight
|
||||
)
|
||||
operator("-", other)
|
||||
end
|
||||
|
||||
# Multiplies Vector and Numberic or Vector and Vector, excluding @weight
|
||||
def *(other)
|
||||
operator("*", other)
|
||||
end
|
||||
|
||||
# Divides Vector and Numberic or Vector and Vector, excluding @weight
|
||||
def /(other)
|
||||
# Duplicated to protect from DivideByZero
|
||||
if other.is_a?(Numeric)
|
||||
Vector.new(
|
||||
@x * other.x,
|
||||
@y * other.y,
|
||||
@z * other.z,
|
||||
@weight * other.weight
|
||||
(@x == 0 ? 0 : @x / other),
|
||||
(@y == 0 ? 0 : @y / other),
|
||||
(@z == 0 ? 0 : @z / other)
|
||||
)
|
||||
else
|
||||
Vector.new(
|
||||
(@x == 0 ? 0 : @x / other.x),
|
||||
(@y == 0 ? 0 : @y / other.y),
|
||||
(@z == 0 ? 0 : @z / other.z)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def dot(other)
|
||||
product = 0
|
||||
|
||||
a = self.to_a
|
||||
b = other.to_a
|
||||
|
||||
3.times do |i|
|
||||
product = product + (a[i] * b[i])
|
||||
end
|
||||
|
||||
return product
|
||||
end
|
||||
|
||||
def cross(other)
|
||||
a = self.to_a
|
||||
b = other.to_a
|
||||
|
||||
Vector.new(
|
||||
b[2] * a[1] - b[1] * a[2],
|
||||
b[0] * a[2] - b[2] * a[0],
|
||||
b[1] * a[0] - b[0] * a[1]
|
||||
)
|
||||
end
|
||||
|
||||
def /(other)
|
||||
# Endeavors to prevent division by zero
|
||||
Vector.new(
|
||||
@x == 0 || other.x == 0 ? 0 : @x / other.x,
|
||||
@y == 0 || other.y == 0 ? 0 : @y / other.y,
|
||||
@z == 0 || other.z == 0 ? 0 : @z / other.z,
|
||||
@weight == 0 || other.weight == 0 ? 0 : @weight / other.weight
|
||||
)
|
||||
# returns degrees
|
||||
def angle(other)
|
||||
Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
|
||||
end
|
||||
|
||||
# returns magnitude of Vector, ignoring #weight
|
||||
@@ -82,8 +119,38 @@ module CyberarmEngine
|
||||
self / Vector.new(mag, mag, mag)
|
||||
end
|
||||
|
||||
def direction
|
||||
# z is pitch
|
||||
# y is yaw
|
||||
# x is roll
|
||||
_x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||
_y = Math.sin(@z.degrees_to_radians)
|
||||
_z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||
|
||||
Vector.new(_x, _y, _z)
|
||||
end
|
||||
|
||||
def inverse
|
||||
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
||||
end
|
||||
|
||||
def sum
|
||||
@x + @y + @z + @weight
|
||||
@x + @y + @z
|
||||
end
|
||||
|
||||
# 2D distance using X and Y
|
||||
def distance(other)
|
||||
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
||||
end
|
||||
|
||||
# 2D distance using X and Z
|
||||
def gl_distance2d(other)
|
||||
Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
|
||||
end
|
||||
|
||||
# 3D distance using X, Y, and Z
|
||||
def distance3d(other)
|
||||
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
|
||||
end
|
||||
|
||||
def to_a
|
||||
@@ -93,5 +160,9 @@ module CyberarmEngine
|
||||
def to_s
|
||||
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
||||
end
|
||||
|
||||
def to_h
|
||||
{x: @x, y: @y, z: @z, weight: @weight}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,11 +2,11 @@ module CyberarmEngine
|
||||
class Text
|
||||
CACHE = {}
|
||||
|
||||
attr_accessor :text, :x, :y, :z, :size, :factor_x, :factor_y, :color, :shadow, :shadow_size, :options
|
||||
attr_reader :textobject
|
||||
attr_accessor :x, :y, :z, :size, :factor_x, :factor_y, :color, :shadow, :shadow_size, :options
|
||||
attr_reader :text, :textobject
|
||||
|
||||
def initialize(text, options={})
|
||||
@text = text || ""
|
||||
@text = text.to_s || ""
|
||||
@options = options
|
||||
@size = options[:size] || 18
|
||||
@font = options[:font] || "sans-serif"#Gosu.default_font_name
|
||||
@@ -62,37 +62,49 @@ module CyberarmEngine
|
||||
return font
|
||||
end
|
||||
|
||||
def text=(string)
|
||||
@rendered_shadow = nil
|
||||
@text = string
|
||||
end
|
||||
|
||||
def width
|
||||
textobject.text_width(@text)
|
||||
end
|
||||
|
||||
def height
|
||||
@text.lines.count * textobject.height
|
||||
(@text.lines.count) * textobject.height
|
||||
end
|
||||
|
||||
def draw
|
||||
if @shadow && !ARGV.join.include?("--no-shadow")
|
||||
_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha) if @shadow_alpha <= @color.alpha
|
||||
_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @color.alpha) unless @shadow_alpha <= @color.alpha
|
||||
@shadow_alpha = 30 if @color.alpha > 30
|
||||
@shadow_alpha = @color.alpha if @color.alpha <= 30
|
||||
shadow_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha)
|
||||
|
||||
@textobject.draw_markup(@text, @x-@shadow_size, @y, @z, @factor_x, @factor_y, _color)
|
||||
@textobject.draw_markup(@text, @x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
|
||||
_x = @shadow_size
|
||||
_y = @shadow_size
|
||||
|
||||
@textobject.draw_markup(@text, @x, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
|
||||
@textobject.draw_markup(@text, @x+@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
|
||||
@rendered_shadow ||= Gosu.render((self.width+(shadow_size*2)).ceil, (self.height+(@shadow_size*2)).ceil) do
|
||||
@textobject.draw_markup(@text, _x-@shadow_size, _y, @z)
|
||||
@textobject.draw_markup(@text, _x-@shadow_size, _y-@shadow_size, @z)
|
||||
|
||||
@textobject.draw_markup(@text, @x, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
|
||||
@textobject.draw_markup(@text, @x-@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
|
||||
@textobject.draw_markup(@text, _x, _y-@shadow_size, @z, @factor_x)
|
||||
@textobject.draw_markup(@text, _x+@shadow_size, _y-@shadow_size, @z)
|
||||
|
||||
@textobject.draw_markup(@text, @x+@shadow_size, @y, @z, @factor_x, @factor_y, _color)
|
||||
@textobject.draw_markup(@text, @x+@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
|
||||
@textobject.draw_markup(@text, _x, _y+@shadow_size, @z)
|
||||
@textobject.draw_markup(@text, _x-@shadow_size, _y+@shadow_size, @z)
|
||||
|
||||
@textobject.draw_markup(@text, _x+@shadow_size, _y, @z)
|
||||
@textobject.draw_markup(@text, _x+@shadow_size, _y+@shadow_size, @z)
|
||||
end
|
||||
@rendered_shadow.draw(@x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, shadow_color)
|
||||
end
|
||||
|
||||
@textobject.draw_markup(@text, @x, @y, @z, @factor_x, @factor_y, @color)
|
||||
end
|
||||
|
||||
def alpha=(n)
|
||||
@color = Gosu::Color.new(@color.red, @color.green, @color.blue, n)
|
||||
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, n)
|
||||
end
|
||||
|
||||
def alpha
|
||||
|
||||
@@ -186,6 +186,22 @@ module CyberarmEngine
|
||||
@border_canvas.update
|
||||
end
|
||||
|
||||
def root
|
||||
unless @root && @root.parent.nil?
|
||||
@root = parent
|
||||
|
||||
loop do
|
||||
if @root.parent.nil?
|
||||
break
|
||||
else
|
||||
@root = @root.parent
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@root
|
||||
end
|
||||
|
||||
def recalculate
|
||||
raise "#{self.class}#recalculate was not overridden!"
|
||||
end
|
||||
@@ -193,5 +209,9 @@ module CyberarmEngine
|
||||
def value
|
||||
raise "#{self.class}#value was not overridden!"
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
raise "#{self.class}#value= was not overridden!"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -30,5 +30,14 @@ module CyberarmEngine
|
||||
def value
|
||||
@text.text
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
@text.text = value
|
||||
|
||||
old_width, old_height = width, height
|
||||
recalculate
|
||||
|
||||
root.recalculate if old_width != width || old_height != height
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
module CyberarmEngine
|
||||
NAME = "InDev"
|
||||
VERSION = "0.2.0"
|
||||
VERSION = "0.5.0"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user