Skip to content

Commit

Permalink
Start work on healer, weapons and enemy..
Browse files Browse the repository at this point in the history
  • Loading branch information
David Henry committed Feb 13, 2012
1 parent 78809b6 commit 7470530
Show file tree
Hide file tree
Showing 25 changed files with 536 additions and 242 deletions.
2 changes: 1 addition & 1 deletion lib/game/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def bind_keys
bind_key(:'1') { skip_to('level5') }
bind_key(:'2') { skip_to('level6') }
bind_key(:'3') { skip_to('level7_a') }
bind_key(:'4') { skip_to('level4') }
bind_key(:'4') { skip_to('level8') }

@editor.bind(:ctrl_x) { puts "Exiting..."; exit }
end
Expand Down
1 change: 1 addition & 0 deletions lib/game/modules/object_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Game
module Modules
module ObjectManagement
def add(object)
object.location = self
@objects << object
end

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

require 'lib/game/object/default'
require 'lib/game/object/Exit'
require 'lib/game/object/enemy'
require 'lib/game/object/exit'
require 'lib/game/object/healer'
require 'lib/game/object/inventry_item'
require 'lib/game/object/location_modifier'
require 'lib/game/object/passage'
require 'lib/game/object/switcher'
require 'lib/game/object/setter'
require 'lib/game/object/trap'
require 'lib/game/object/weapon'

class Game
class Object
Expand Down
4 changes: 4 additions & 0 deletions lib/game/object/default.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Game
class Object
module Default
def self.included(base)
base.send :attr_accessor, :location
end

def initialize(options)
@options = options
end
Expand Down
6 changes: 6 additions & 0 deletions lib/game/object/enemy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Game
class Object
module Enemy
end
end
end
6 changes: 6 additions & 0 deletions lib/game/object/exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def auto_process
end
end

def next_map_name
return 'Game Over' unless @options['next_map']

@options['next_map'].gsub(/((^| |_)[a-z])/) {|v| v.upcase.gsub('_', ' ')}
end

def skip_to(name)
Game::Engine.instance.load_map Game::Map.load_map("maps/#{name}")
end
Expand Down
18 changes: 18 additions & 0 deletions lib/game/object/healer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Game
class Object
module Healer
def auto_process
self.health -= Game::Player.instance.heal(health)
self.location.remove(self) if self.health == 0
end

def health
@health ||= @options['health']
end

def health=(val)
@health = val
end
end
end
end
13 changes: 13 additions & 0 deletions lib/game/object/weapon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Game
class Object
module Weapon
def equip
Game::Player.instance.equip('weapon', self)
end

def attack

end
end
end
end
10 changes: 9 additions & 1 deletion lib/game/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Player
include Game::Player::Movements
include Game::Modules::InstanceSetter

attr_reader :location
attr_accessor :location
attr_reader :objects
attr_reader :hp

Expand All @@ -26,5 +26,13 @@ def damage(hp)
@hp -= hp
end
end

def heal(hp)
@hp += hp
return hp if @hp <= 100
used = hp - (@hp - 100)
@hp = 100
used
end
end
end
5 changes: 5 additions & 0 deletions lib/game/player/movements.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
class Game
class Player
module Movements
def self.included(base)
base.send :attr_reader, :direction
end

def load_map(map)
@map = map
@location = @map.start_location
@location.add(self)
end

def move(direction)
@direction = direction
new_location = @location.at(direction)
if new_location.passible?(objects)
move_to new_location
Expand Down
1 change: 1 addition & 0 deletions lib/render/console.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'lib/render/console/draw_map'
require 'lib/render/console/draw_tile'
require 'lib/render/console/draw_message'

module Render
Expand Down
123 changes: 59 additions & 64 deletions lib/render/console/draw_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,96 @@ module Render
class Console
module DrawMap
def draw_map(engine)
string = ''
string << draw_refresh
string << draw_title(engine.map)
string << draw_board(engine.map.data)
string << draw_stats(engine.player)
string << draw_player(engine.player)
@output = ''
draw_refresh
draw_title(engine.map)
draw_board(engine.map.data)
draw_stats(engine.player)
draw_player(engine.player)
draw_tile(engine.player.location)

system('clear')
@io.puts string
@io.puts @output
end

private
def append(*strings)
@output << strings.join
@output << "\n\r"
end

def draw_stats(player)
string = 'Player Stats:'
string << "\n\r"
string << " Health: " << ("%3.f" % player.hp) << "\n\r"
string << "\n\r"
string
append 'Player Stats:'
append " Health: ", ("%3.f" % player.hp)
append
end

def draw_refresh
string = "Refresh Rate: "
if @last
string << ("%3f" % (1 / (Time.now - @last)))
end
rate = @last ? ("%3f" % (1 / (Time.now - @last))) : ''
@last = Time.now
string

append "Refresh Rate: ", rate
append
end


def draw_title(map)
string = "\n\r\n\r"
string << "Level: " << map.name << "\n\r"
string << "Goal: " << map.goal
string << "\n\r\n\r"
string
append "Level: ", map.name
append "Goal: ", map.goal
append
end

def draw_board(board)
string = "\r+"
board.each { string << '---'}
string << "+\n\r"
edge = ["+", '---' * board.size, "+"]
append *edge
board.each do |row|
string << "|"
row.each { |location| string << draw_location(location) }
string << "|\n\r"
append "|",
*row.map { |tile| Tile.new(tile).draw },
"|"
end
string << "+"
board.each { string << '---'}
string << "+\n\r\n\r"
string
append *edge
end

def draw_player(player)
string = "Inventry:\n\r"
append "Inventry:"
if player.objects.empty?
string << "(EMPTY)"
append "(EMPTY)"
else
player.objects.each_with_index do |object, i|
string << "%3.f" % (i + 1) << ' - ' << object.name << "\n\r"
append "%3.f" % (i + 1), ' - ', object.name
end
end
string << "\n\r\n\r"
string
append
end

def draw_location(location)
cell = case location.location_type
when Game::Location::EMPTY_CELL
' '
when Game::Location::WALL_90
' | '
when Game::Location::WALL_CORNER_RIGHT
' +-'
when Game::Location::WALL_CORNER_LEFT
'-+ '
when Game::Location::WALL_0
'---'
when Game::Location::WALL_CORNER
'-+-'
else
(" #{location.location_type}")[-2..-1]
def draw_tile(tile)
objects = tile.objects.dup
append "Location: "
append " ", tile.class
append "Items: "
objects.each do |obj|
draw_object(obj)
end
cell = 'SSS' if location.has_object?(Game::Object::Switcher)
cell = 'SSS' if location.has_object?(Game::Object::Setter)
cell = 'EEE' if location.has_object?(Game::Object::Exit)
cell = 'TTT' if location.has_object?(Game::Object::LocationModifier)
cell = '###' if location.has_object?(Game::Object::Trap)
if location.has_object?(Game::Object::Passage)
cell = 'DDD'
cell[1] = ' ' if location.passible?([])
end

def draw_object(obj)
append " Healer: ", obj.health if obj.is_a?(Game::Object::Healer)
append " Trap: ", obj.damage if obj.is_a?(Game::Object::Trap)
append " Exit: ", obj.next_map_name if obj.is_a?(Game::Object::Exit)
append " Door: ", 'OPEN' if obj.is_a?(Game::Object::Passage)
append " Door Switch" if obj.is_a?(Game::Object::Switcher)
append " Door Setter" if obj.is_a?(Game::Object::Setter)
if obj.is_a?(Game::Object::InventryItem)
if obj.is_a?(Game::Object::Weapon)
append " Weapon: ", classname_for(obj), '(', obj.attack, ')'
else
append " Item: ", classname_for(obj)
end
end
cell = 'KKK' if defined?(Game::Object::Key) && location.has_object?(Game::Object::Key)
cell[1] = '*' if location.has_object?(Game::Player)
cell
end

def classname_for(obj)
obj.class.to_s.split('::').last
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/render/console/draw_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ def draw_message(engine)
string = "\n\r\n\r"
string << ' +----------------------------------------+' << "\n\r"
string << ' + +' << "\n\r"
string << ' + Game Over +' << "\n\r"
string << ' + Game Over +' << "\n\r"
string << ' + '
string << ((' ' * 26) + engine.message)[-36..-1]
len = ((36 - engine.message.length) / 2).to_i + 1
string << ((' ' * len) + engine.message + (' ' * len))[0..36]
string << ' +' << "\n\r"
string << ' + +' << "\n\r"
string << ' +----------------------------------------+' << "\n\r"
Expand Down
74 changes: 74 additions & 0 deletions lib/render/console/draw_tile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module Render
class Console
module DrawMap
class Tile
attr_reader :tile

def initialize(tile)
@tile = tile
end

def draw
if tile.is_a?(Game::Location::Wall)
draw_player(draw_wall)
else
draw_player(draw_empty)
end
end

def draw_player(base)
return base unless tile.has_object?(Game::Player)
base[0] +
case Game::Player.instance.direction
when :up
'^'
when :down
'v'
when :left
'<'
when :right
'>'
else
'*'
end +
base[2]
end

def draw_empty
{
Game::Object::Switcher => 'SSS',
Game::Object::Setter => 'SSS',
Game::Object::Exit => 'EEE',
Game::Object::LocationModifier => 'TTT',
Game::Object::Trap => '###',

Game::Object::Enemy => '|:P',
Game::Object::Healer => '+++',
Game::Object::Weapon => '+--'
}.each do |module_name, tile_piece|

return tile_piece if tile.has_object?(module_name)
end

return 'KKK' if defined?(Game::Object::Key) && tile.has_object?(Game::Object::Key)
' '
end

def draw_wall
if tile.has_object?(Game::Object::Passage)
return 'D D' if tile.passible?([])
return 'DDD'
end

base = {
Game::Location::WALL_90 => ' | ',
Game::Location::WALL_CORNER_RIGHT => ' +-',
Game::Location::WALL_CORNER_LEFT => '-+ ',
Game::Location::WALL_0 => '---',
Game::Location::WALL_CORNER => '-+-'
}[tile.location_type] || (debugger)
end
end
end
end
end
Loading

0 comments on commit 7470530

Please sign in to comment.