Skip to content

Commit

Permalink
move life force processing out of player class
Browse files Browse the repository at this point in the history
  • Loading branch information
David Henry committed Feb 15, 2012
1 parent 30ea64e commit 030f1de
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 26 deletions.
9 changes: 9 additions & 0 deletions lib/game/modules/object_management.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
class Game
module Modules
module ObjectManagement
def self.included(base)
base.send :attr_reader, :objects
end

def initialize(*args)
super
@objects = []
end

def add(object)
if self.is_a?(Game::Player)
object.player = self if object.respond_to?(:player)
Expand Down
3 changes: 1 addition & 2 deletions lib/game/modules/try_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ class Game
module Modules
module TryHelper
def try(method, *args, &block)
return nil if self.nil?
send(method, *args, &block)
self && send(method, *args, &block)
end
end
end
Expand Down
25 changes: 2 additions & 23 deletions lib/game/player.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
require 'lib/game/player/actions'
require 'lib/game/player/life_force'
require 'lib/game/player/movements'

class Game
class Player
include Game::Modules::ObjectManagement
include Game::Player::Actions
include Game::Player::Movements
include Game::Player::LifeForce
include Game::Modules::InstanceSetter

attr_accessor :tile
attr_reader :objects
attr_reader :hp

def initialize(*args)
super(*args)
@objects = []
@hp = 100
end

def damage(hp)
if hp >= @hp
@hp = 0
Game::Engine.instance.end('Unfortunately you died.')
else
@hp -= hp
end
end

def heal(hp)
@hp += hp
return hp if @hp <= 100
used = hp - (@hp - 100)
@hp = 100
used
end
end
end
32 changes: 32 additions & 0 deletions lib/game/player/life_force.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Game
class Player
module LifeForce

def self.included(base)
base.send :attr_reader, :hp
end

def initialize(*args)
super
@hp = 100
end

def damage(hp)
if hp >= @hp
@hp = 0
Game::Engine.instance.end('Unfortunately you died.')
else
@hp -= hp
end
end

def heal(hp)
@hp += hp
return hp if @hp <= 100
used = hp - (@hp - 100)
@hp = 100
used
end
end
end
end
2 changes: 1 addition & 1 deletion lib/game/player/movements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.included(base)
end

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

Expand Down
24 changes: 24 additions & 0 deletions lib/game/tile/modules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Game
class Tile
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
@objects = []
end

def ==(val)
tile_type == val.tile_type &&
x == val.x &&
y == val.y
end
end
end
end

0 comments on commit 030f1de

Please sign in to comment.