Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Feature/imutable state #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/scorched_earth/event_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ def subscribe(klass, &block)
subscribers << [klass, block]
end

def run(event)
subscribers.each do |klass, block|
block.call event if event.is_a? klass
end
def run(state, event)
subscribers
.select { |klass, block| event.is_a? klass }
.inject(state) { |state, (klass, block)| block.call state, event }
end

def process!
def process!(state)
processing = Array.new(queue.size) { queue.pop }

processing.each do |event|
run event
processing.inject(state) do |state, event|
run state, event
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/scorched_earth/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def setup

def update(delta)
event_runner.publish Events::GameUpdate.new delta
event_runner.process!
event_runner.process! nil
end

def render(graphics)
event_runner.run Events::GameRender.new graphics
event_runner.run nil, Events::GameRender.new(graphics)
end

def publish(event)
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/game_over/timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Timeout
def setup
super

event_runner.subscribe(Events::GameOver) do |event|
event_runner.subscribe(Events::GameOver) do |state, event|
event.time < Time.now ? setup : event_runner.publish(event)
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/scorched_earth/subscribers/game_render.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module ScorchedEarth
module Subscribers
module GameRender


def setup
super

event_runner.subscribe(Events::GameRender) do |event|
event_runner.subscribe(Events::GameRender) do |state, event|
graphics = event.graphics

graphics.set_color color_palette.get('sky')
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/game_update/collisions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Collisions
def setup
super

event_runner.subscribe(Events::GameUpdate) do
event_runner.subscribe(Events::GameUpdate) do |state, event|
objects
.select { |object| object.is_a? Shot }
.select { |object| array.fetch(object.x, 0) > object.y }
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/game_update/delta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Delta
def setup
super

event_runner.subscribe(Events::GameUpdate) do |event|
event_runner.subscribe(Events::GameUpdate) do |state, event|
@objects = objects.map { |object| object.update event.delta }.compact
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/hit/deform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Deform
def setup
super

event_runner.subscribe(Events::Hit) do |event|
event_runner.subscribe(Events::Hit) do |state, event|
if event.object.x < array.size && event.object.x > 0
@array = Services::Deform.new(array).call(event.object.x, event.radius)
@players = players.map { |player| Player.new player.x, array[player.x] }
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/hit/effect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Effect
def setup
super

event_runner.subscribe(Events::Hit) do |event|
event_runner.subscribe(Events::Hit) do |state, event|
objects << Explosion.new(event.object.x, event.object.y)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/hit/radius.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Radius
def setup
super

event_runner.subscribe(Events::Hit) do |event|
event_runner.subscribe(Events::Hit) do |state, event|
if players.any? { |player| inside_radius? event.object.x - player.x, 0, event.radius }
event_runner.publish Events::GameOver.new(Time.now + 0.25)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/mouse_moved.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module MouseMoved
def setup
super

event_runner.subscribe(Events::MouseMoved) do |event|
event_runner.subscribe(Events::MouseMoved) do |state, event|
@mouse = Mouse.new event.x, event.y, mouse.pressed_at
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/mouse_pressed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module MousePressed
def setup
super

event_runner.subscribe(Events::MousePressed) do |_event|
event_runner.subscribe(Events::MousePressed) do |state, event|
@mouse = Mouse.new mouse.x, mouse.y, Time.now
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/scorched_earth/subscribers/mouse_released/fire.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Fire
def setup
super

event_runner.subscribe(Events::MouseReleased) do |_event|
event_runner.subscribe(Events::MouseReleased) do |state, event|
if mouse.pressed_at && mouse.x && mouse.y
shot = Shot.new current_player.x, array[current_player.x], velocity_x, velocity_y

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module NextPlayer
def setup
super

event_runner.subscribe(Events::MouseReleased) do |_event|
event_runner.subscribe(Events::MouseReleased) do |state, event|
@players = players.rotate!
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/event_runner_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

describe ScorchedEarth::EventRunner do
let(:runner) { ScorchedEarth::EventRunner.new }
let(:event) { TestEvent }

describe "#run" do
specify "calls matching subscribers" do
yielded = []
func = Proc.new { |e| yielded << e }

runner.subscribe event, &func
runner.run 42, event.new

assert_equal [42], yielded
end
end
end
1 change: 1 addition & 0 deletions test/support/test_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class TestEvent; end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
require 'bundler/setup'
require 'support/simplecov'
require 'support/null_object'
require 'support/test_event'
require 'scorched_earth'
require 'minitest/autorun'