Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ruby2D::Entity #199

Merged
merged 2 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ruby2d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'ruby2d/color'
require 'ruby2d/window'
require 'ruby2d/dsl'
require 'ruby2d/entity'
require 'ruby2d/quad'
require 'ruby2d/line'
require 'ruby2d/circle'
Expand Down
17 changes: 17 additions & 0 deletions lib/ruby2d/entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Ruby2D::Entity

module Ruby2D
class Entity

# Add the entity to the window
def add
Window.add(self)
end

# Remove the entity from the window
def remove
Window.remove(self)
end

end
end
8 changes: 2 additions & 6 deletions lib/ruby2d/renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ def z=(z)

# Add the object to the window
def add
if Module.const_defined? :DSL
Window.add(self)
end
Window.add(self)
end

# Remove the object from the window
def remove
if Module.const_defined? :DSL
Window.remove(self)
end
Window.remove(self)
end

# Set the color value
Expand Down
21 changes: 19 additions & 2 deletions lib/ruby2d/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def initialize(args = {})
# Renderable objects currently in the window, like a linear scene graph
@objects = []

# Entities currently in the window
@entities = []

# Mouse X and Y position in the window
@mouse_x, @mouse_y = 0, 0

Expand Down Expand Up @@ -250,6 +253,8 @@ def add(o)
case o
when nil
raise Error, "Cannot add '#{o.class}' to window!"
when Entity
@entities.push(o)
when Array
o.each { |x| add_object(x) }
else
Expand All @@ -263,8 +268,9 @@ def remove(o)
raise Error, "Cannot remove '#{o.class}' from window!"
end

if i = @objects.index(o)
@objects.delete_at(i)
collection = o.class.ancestors.include?(Ruby2D::Entity) ? @entities : @objects
if i = collection.index(o)
collection.delete_at(i)
true
else
false
Expand All @@ -274,6 +280,7 @@ def remove(o)
# Clear all objects from the window
def clear
@objects.clear
@entities.clear
end

# Set the update callback
Expand Down Expand Up @@ -586,6 +593,11 @@ def update_callback

@update_proc.call

# Run update method on all entities
@entities.each do |e|
e.update
end

# Accept and eval commands if in console mode
if @console
if STDIN.ready?
Expand Down Expand Up @@ -613,6 +625,11 @@ def render_callback
end

@render_proc.call

# Run render method on all entities
@entities.each do |e|
e.render
end
end

# Show the window
Expand Down
51 changes: 51 additions & 0 deletions test/entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Entity

require 'ruby2d'

class Player < Ruby2D::Entity
def update
puts Time.now.to_f
end

def render
Triangle.draw(x1: 320, y1: 50, x2: 540, y2: 430, x3: 100, y3: 430,
color: [
[1.0, 0.3, 0.3, 1.0],
[0.0, 0.4, 0.8, 1.0],
[0.2, 0.8, 0.3, 1.0]
]
)
end
end

player = Player.new


# Using DSL

player.add

show


# Using class pattern

=begin
class GameWindow < Ruby2D::Window
def initialize
super
end

def update
end

def render
end
end

game_window = GameWindow.new

game_window.add(player)

game_window.show
=end