Skip to content

Commit

Permalink
general cleanup of render
Browse files Browse the repository at this point in the history
start to work on fighting the enemy unit
  • Loading branch information
David Henry committed Feb 15, 2012
1 parent d0b7a5c commit 30ea64e
Show file tree
Hide file tree
Showing 27 changed files with 355 additions and 87 deletions.
2 changes: 1 addition & 1 deletion lib/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def play
@render.draw_message(@engine)
end

def run_thread(pause=0.05)
def run_thread(pause=0.03)
Thread.new do
loop do
yield
Expand Down
2 changes: 2 additions & 0 deletions lib/game/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def map
end

def take_turn
active_objects = Game::Object.engine_objects(map.name)[Game::Object::ACTIVE]
active_objects.each {|obj| obj.active_turn }
end

def ended?
Expand Down
5 changes: 5 additions & 0 deletions lib/game/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def bind_keys
bind_key(:a) { @player.move(Game::Map::EAST) }
bind_key(:d) { @player.move(Game::Map::WEST) }

bind_key(:W) { @player.move(Game::Map::NORTH) }
bind_key(:S) { @player.move(Game::Map::SOUTH) }
bind_key(:A) { @player.move(Game::Map::EAST) }
bind_key(:D) { @player.move(Game::Map::WEST) }

bind_key(:' ') { @player.take_action }

bind_key(:'1') { skip_to('level5') }
Expand Down
3 changes: 2 additions & 1 deletion lib/game/modules.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'lib/game/modules/object_management'
require 'lib/game/modules/try_helper'
require 'lib/game/modules/instance_setter'
require 'lib/game/modules/instance_setter'
require 'lib/game/modules/impassible'
9 changes: 9 additions & 0 deletions lib/game/modules/impassible.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Game
module Modules
module Impassible
def passible?(player_objects=[])
false
end
end
end
end
1 change: 1 addition & 0 deletions lib/game/modules/instance_setter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def self.included(base)
end

def initialize(*args)
super(*args)
self.class.set(self)
end

Expand Down
10 changes: 9 additions & 1 deletion lib/game/modules/object_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ class Game
module Modules
module ObjectManagement
def add(object)
object.tile = self
if self.is_a?(Game::Player)
object.player = self if object.respond_to?(:player)
object.tile = nil
else
object.player = nil if object.respond_to?(:player)
object.tile = self
end
@objects << object
end

def remove(object)
object.player = nil if object.respond_to?(:player)
object.tile = nil
@objects.delete(object)
end

Expand Down
5 changes: 5 additions & 0 deletions lib/game/object.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'lib/game/object/selector'
require 'lib/game/object/status'

require 'lib/game/object/default'
require 'lib/game/object/enemy'
Expand Down Expand Up @@ -52,6 +53,10 @@ def add(level, status, obj)
engine_objects(level)[status] << obj
end

def remove(level, status, obj)
engine_objects(level)[status].delete(obj)
end

def clear
@game_objects = {}
@objects = []
Expand Down
8 changes: 6 additions & 2 deletions lib/game/object/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ class Game
class Object
module Default
def self.included(base)
base.send :attr_accessor, :tile
base.send :attr_accessor, :tile, :player
end

def initialize(options)
super
@options = options
super
end

def id
Expand All @@ -19,6 +19,10 @@ def method_missing(method, *args, &blk)
super
end
end

def respond_to?(method)
@options.has_key?(method.to_s) || super
end
end
end
end
36 changes: 21 additions & 15 deletions lib/game/object/enemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@ class Game
class Object
module Enemy
def self.included(base)
base.send :attr_reader, :direction
base.send :include, Game::Object::Status
base.send :include, Game::Modules::Impassible

base.send :attr_reader, :direction, :hp, :status
end

def initialize(*args)
super
@direction = Game::Map::NORTH
@hp = @options['health'] || 100
@status ||= Game::Object::IDLE
Game::Object.add(Game::Engine.instance.map.name, status, self)
end

def passible?
false
end
def damage(hp)
activate if status == Game::Object::IDLE

def range
@options['range'] || 4
if hp >= @hp
@hp = 0
expire
else
@hp -= hp
end
end

def status
@status ||= Game::Object::IDLE
def move_to(tile)
@tile.remove(self)
@tile = tile
@tile.add(self)
end

def active_turn
Expand All @@ -30,18 +40,14 @@ def active_turn
if player_in_front?
Game::Player.instance.damage(attack)
elsif !player_in_range?
Game::Object.remove(Game::Engine.instance.map.name, status, self)
@status = Game::Object::IDLE
Game::Object.add(Game::Engine.instance.map.name, status, self)
deactivate
elsif (tile = @tile.at(@direction)).passible?
move_to(tile)
end
end

def move_to(tile)
@tile.remove(self)
@tile = tile
@tile.add(self)
def range
@options['range'] || 4
end

def player_in_range?
Expand Down
2 changes: 1 addition & 1 deletion lib/game/object/inventry_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def id
end

def name
@options['name'] || self.class.to_s
@options['name'] || self.class.to_s.split('::').last
end

def use
Expand Down
27 changes: 27 additions & 0 deletions lib/game/object/status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Game
class Object
module Status
def status
@status ||= Game::Object::IDLE
end

def expire
tile.remove(self)
Game::Object.remove(Game::Engine.instance.map.name, status, self)
Game::Object.objects.delete(self)
end

def activate
Game::Object.remove(Game::Engine.instance.map.name, status, self)
@status = Game::Object::ACTIVE
Game::Object.add(Game::Engine.instance.map.name, status, self)
end

def deactivate
Game::Object.remove(Game::Engine.instance.map.name, status, self)
@status = Game::Object::IDLE
Game::Object.add(Game::Engine.instance.map.name, status, self)
end
end
end
end
7 changes: 5 additions & 2 deletions lib/game/object/weapon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ def equip
Game::Player.instance.equip('weapon', self)
end

def attack

def use
tile = player.tile.at(Game::Player.instance.direction)
if object = tile.objects.detect {|obj| obj.respond_to?(:damage) }
object.damage(attack)
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/game/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Player
attr_reader :objects
attr_reader :hp

def initialize
super
def initialize(*args)
super(*args)
@objects = []
@hp = 100
end
Expand Down
10 changes: 7 additions & 3 deletions lib/game/player/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ def take_action
tile.remove(obj)
add(obj)
end
objects_to_responding_to(:process).each do |obj|
obj.process
end
objects_to_responding_to(:process).each(&:process)
weapons.each(&:use)
end

def take_auto_action
Expand All @@ -26,6 +25,11 @@ def objects_to_responding_to(method)
tile.objects.select {|obj| obj.respond_to?(method) }
end
private :objects_to_responding_to

def weapons
objects.select {|obj| obj.is_a?(Game::Object::Weapon) }
end
private :weapons
end
end
end
5 changes: 5 additions & 0 deletions lib/game/player/movements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ def self.included(base)
base.send :attr_reader, :direction
end

def initialize(*args)
super(*args)
@direction = Game::Map::NORTH
end

def load_map(map)
@map = map
@tile = @map.start_tile
Expand Down
2 changes: 1 addition & 1 deletion lib/game/tile.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'lib/game/tile/modules'
require 'lib/game/tile/base'
require 'lib/game/tile/movement'
require 'lib/game/tile/passible'

Expand Down
13 changes: 1 addition & 12 deletions lib/game/tile/modules.rb → lib/game/tile/base.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
class Game
class Tile
module Impassible
def passible?(player_objects)
false
end
end

module Passible
def passible?(player_objects)
objects.all?(&:passible?)
end
end

module Base
def self.included(base)
base.send :attr_accessor, :tile_type
base.send :attr_reader, :objects, :x, :y
end

def initialize(tile_type, x, y)
super
@tile_type = tile_type
@x = x
@y = y
Expand Down
13 changes: 11 additions & 2 deletions lib/game/tile/edge.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
class Game
class Tile
class Edge
include Game::Modules::ObjectManagement
include Game::Tile::Base
include Game::Tile::Impassible
include Game::Modules::Impassible

def self.instance
@self ||= new
end

def initialize; end
def initialize
@objects = []
end

# over write the object management implementation as
# the edge should never have objects
def add(*args)
raise 'Attempt to add object off the edge of the map'
end
end
end
end
2 changes: 2 additions & 0 deletions lib/render/console.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'lib/render/console/draw_map'
require 'lib/render/console/draw_tile'
require 'lib/render/console/draw_message'
require 'lib/render/console/player_inventry'

module Render
class Console
include Render::Console::DrawMap
include Render::Console::DrawMessage
include Render::Console::PlayerInventry

def initialize(io=STDOUT)
@io = io
Expand Down
Loading

0 comments on commit 30ea64e

Please sign in to comment.