Class: Ein::World

Inherits:
Object
  • Object
show all
Defined in:
lib/ein/world.rb

Instance Method Summary collapse

Constructor Details

#initializeWorld

Returns a new instance of World.



9
10
11
12
13
# File 'lib/ein/world.rb', line 9

def initialize
  @entities = []
  read_world
  self
end

Instance Method Details

#collision_with?(object) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ein/world.rb', line 43

def collision_with?(object)

  @boundaries.each do |boundary|
    return true if boundary.distance_to(object) <= 0
  end

  @obstacles.each do |obstacle|
    return true if obstacle.distance_to(object) <= 0
  end
  false
end

#renderObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ein/world.rb', line 15

def render
  world_ui = {}
  world_ui['boundaries'] = @boundaries
  world_ui['obstacles'] = @obstacles
  @entities.each do |entity|
    if entity.respond_to?(:render)
      world_ui['entity'] = entity.render
    end
  end
  world_ui.to_json
end

#spawn(*entities) ⇒ Object



27
28
29
30
31
# File 'lib/ein/world.rb', line 27

def spawn(*entities)
  entities.each do |entity|
    @entities.push(entity)
  end
end

#step(time_step) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ein/world.rb', line 33

def step (time_step)
  @entities.each do |entity|
    entity.step(time_step)
    if collision_with?(entity)
      entity.step_back
    end
  end
end