iterating all objects and applying force
This commit is contained in:
parent
99ccf51507
commit
2e4d083094
@ -1,9 +1,11 @@
|
|||||||
require 'opal/pixi'
|
require 'opal/pixi'
|
||||||
require 'native'
|
require 'native'
|
||||||
require "main/lib/registers_view"
|
|
||||||
require "main/lib/space_view"
|
|
||||||
require "salama"
|
require "salama"
|
||||||
|
|
||||||
|
require_relative "registers_view"
|
||||||
|
require_relative "object_view"
|
||||||
|
require_relative "space_view"
|
||||||
|
|
||||||
class MainView
|
class MainView
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@ -26,13 +28,13 @@ class MainView
|
|||||||
end.fail do |error|
|
end.fail do |error|
|
||||||
raise "Error: #{error}"
|
raise "Error: #{error}"
|
||||||
end
|
end
|
||||||
space = SpaceView.new
|
space = SpaceView.new
|
||||||
@container.add_child space
|
@container.add_child space
|
||||||
|
|
||||||
animate = Proc.new do
|
animate = Proc.new do
|
||||||
`requestAnimationFrame(animate)`
|
`requestAnimationFrame(animate)`
|
||||||
registers.update
|
registers.draw_me
|
||||||
space.update
|
space.draw_me
|
||||||
renderer.render @container
|
renderer.render @container
|
||||||
end
|
end
|
||||||
animate.call
|
animate.call
|
||||||
|
29
app/main/lib/object_view.rb
Normal file
29
app/main/lib/object_view.rb
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
class ObjectView
|
||||||
|
|
||||||
|
attr_accessor :text , :object , :attributes
|
||||||
|
|
||||||
|
def initialize o
|
||||||
|
super()
|
||||||
|
self.text = PIXI::Text.new("no")
|
||||||
|
puts "NO O " unless o
|
||||||
|
self.object = o
|
||||||
|
self.text.text = o.object_id.to_s
|
||||||
|
@attributes = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def set name , val
|
||||||
|
@attributes[name] = val
|
||||||
|
end
|
||||||
|
def get(name)
|
||||||
|
@attributes[name]
|
||||||
|
end
|
||||||
|
def position
|
||||||
|
#raise "NONAME" unless self.text
|
||||||
|
self.text.position
|
||||||
|
end
|
||||||
|
|
||||||
|
def distance to
|
||||||
|
self.position - to.position
|
||||||
|
end
|
||||||
|
end
|
@ -14,7 +14,7 @@ class RegisterView < PIXI::Container
|
|||||||
@registers[name] = reg
|
@registers[name] = reg
|
||||||
self.add_child reg
|
self.add_child reg
|
||||||
end
|
end
|
||||||
def update
|
def draw_me
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,19 @@
|
|||||||
|
|
||||||
|
PIXI::Point.class_eval do
|
||||||
|
alias_native :y=
|
||||||
|
|
||||||
|
def add point
|
||||||
|
self.x += point.x
|
||||||
|
self.y += point.y
|
||||||
|
end
|
||||||
|
def scale_by num
|
||||||
|
min = 0.001
|
||||||
|
num = min if num <= min
|
||||||
|
self.x = self.x / num
|
||||||
|
self.y = self.y / num
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class SpaceView < PIXI::Graphics
|
class SpaceView < PIXI::Graphics
|
||||||
include Sof::Util
|
include Sof::Util
|
||||||
|
|
||||||
@ -13,64 +28,96 @@ class SpaceView < PIXI::Graphics
|
|||||||
@view_objects = {}
|
@view_objects = {}
|
||||||
|
|
||||||
@objects.each do |i , o|
|
@objects.each do |i , o|
|
||||||
view = Volt::Model.new
|
next unless o.object
|
||||||
view._object = o
|
view = ObjectView.new o.object
|
||||||
@view_objects[i] = view
|
@view_objects[i] = view
|
||||||
at = PIXI::Point.new 100 , 100
|
add_child view.text
|
||||||
name = PIXI::Text.new i.to_s
|
|
||||||
view._name = name
|
|
||||||
add_child name
|
|
||||||
end
|
end
|
||||||
|
fill_attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
# should almost be called draw by now
|
# should almost be called draw by now
|
||||||
def update
|
def draw_me
|
||||||
update_positions
|
update_positions
|
||||||
self.clear
|
self.clear
|
||||||
prev = nil
|
prev = nil
|
||||||
@view_objects.each do |i , view|
|
@view_objects.each do |i , view|
|
||||||
if prev
|
if prev
|
||||||
self.lineStyle(4, 0xffd900, 2)
|
self.lineStyle(4, 0xffd900, 2)
|
||||||
self.moveTo( prev._name.position.x , prev._name.position.y )
|
puts "p" if prev.nil?
|
||||||
self.lineTo( view._name.position.x , view._name.position.y )
|
puts "v" if view.nil?
|
||||||
|
self.moveTo( prev.position.x , prev.position.y )
|
||||||
|
self.lineTo( view.position.x , view.position.y )
|
||||||
end
|
end
|
||||||
prev = view
|
prev = view
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def force from , to
|
||||||
|
puts "force"
|
||||||
|
dir_x = from.x - to.x
|
||||||
|
dir_x2 = dir_x * dir_x
|
||||||
|
dir_y = from.y - to.y
|
||||||
|
dir_y2 = dir_y * dir_y
|
||||||
|
if( dir_x2 < 0.1 and dir_y2 < 0.1 )
|
||||||
|
puts "Were close"
|
||||||
|
dir_x = rand(10)
|
||||||
|
dir_y = rand(10)
|
||||||
|
end
|
||||||
|
f = dir_x * dir_x + dir_y * dir_y
|
||||||
|
f = 0.1 if f < 0.1
|
||||||
|
PIXI::Point.new( dir_x / f , dir_y / f)
|
||||||
|
end
|
||||||
|
|
||||||
def update_positions
|
def update_positions
|
||||||
@view_objects.each do |i , view|
|
@view_objects.each do |i , view|
|
||||||
view._name.position.x += 1
|
view.each do |n , v |
|
||||||
|
next if n == :id
|
||||||
|
next unless o = @view_objects[v]
|
||||||
|
puts "v2" if view.nil?
|
||||||
|
puts "0" if o.nil?
|
||||||
|
view.position.add force( view.position , o.position )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def fill_attributes
|
def fill_attributes
|
||||||
@view_objects.each do |i , view|
|
@view_objects.each do |i , view|
|
||||||
object = view._object
|
ob = view._object
|
||||||
case object.class.name
|
next if is_value?(ob)
|
||||||
|
case ob.class.name
|
||||||
when "Array" , "Parfait::List"
|
when "Array" , "Parfait::List"
|
||||||
fill_array view
|
fill_array view
|
||||||
when "Hash" , "Parfait::Dictionary"
|
when "Hash" , "Parfait::Dictionary"
|
||||||
fill_hash view
|
fill_hash view
|
||||||
else
|
else
|
||||||
# and recursively add attributes
|
next if ob.class.name.include?("::") and !ob.class.name.include?("Parfait")
|
||||||
attributes = attributes_for(object)
|
next if ob.class.name == "Proc"
|
||||||
|
next if ob.class.name == "String"
|
||||||
|
next if ob.class.name == "Numeric"
|
||||||
|
next if ob.class.name == "Class"
|
||||||
|
#puts "object #{ob.class.name}"
|
||||||
|
|
||||||
|
attributes = attributes_for(ob)
|
||||||
attributes.each do |a|
|
attributes.each do |a|
|
||||||
val = get_value( object , a)
|
next if a == "html_safe"
|
||||||
if( @view_objects[vol.object_id])
|
next if a == "constructor"
|
||||||
|
next if a == "toString"
|
||||||
|
next if a == "position"
|
||||||
|
val = get_value( ob , a)
|
||||||
|
if( @view_objects[val.object_id])
|
||||||
#ref
|
#ref
|
||||||
end
|
end
|
||||||
|
#puts "set #{a}"
|
||||||
view.set(a , val )
|
view.set(a , val )
|
||||||
end
|
end
|
||||||
superclasses = [object.class.superclass.name]
|
superclasses = [ob.class.superclass.name]
|
||||||
if superclasses.include?( "Array") or superclasses.include?( "Parfait::List")
|
if superclasses.include?( "Array") or superclasses.include?( "Parfait::List")
|
||||||
fill_array view
|
fill_array view
|
||||||
end
|
end
|
||||||
if superclasses.include?( "Hash") or superclasses.include?( "Parfait::Dictionary")
|
if superclasses.include?( "Hash") or superclasses.include?( "Parfait::Dictionary")
|
||||||
fill_hash view
|
fill_hash view
|
||||||
end
|
end
|
||||||
view._object = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user