Skip to content

Commit

Permalink
Add switch and setter processing
Browse files Browse the repository at this point in the history
Add Trap and concept of health points

Location specification when changing levels - simulate multi level map
  • Loading branch information
David Henry committed Feb 12, 2012
1 parent 8b77e20 commit 78809b6
Show file tree
Hide file tree
Showing 51 changed files with 1,504 additions and 348 deletions.
16 changes: 5 additions & 11 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
require 'lib/game/modules'

require 'lib/render'
require 'lib/game/engine'
require 'lib/game/input'
require 'lib/game/player'
require 'lib/game/map'
require 'lib/game/location'
require 'lib/game/object'

require 'json'
require 'rawline'
require 'ruby-debug'

module TryHelper
def try(method, *args, &block)
return nil if self.nil?
send(method, *args, &block)
end
end

Object.send :include, TryHelper

class Game
attr_reader :engine

Expand All @@ -31,14 +25,14 @@ def initialize
def play
Thread.abort_on_exception = true
threads = []
threads << run_thread { @render.draw(@engine.map) }
threads << run_thread { @render.draw_map(@engine) }
threads << run_thread(1) { @engine.take_turn }
threads << run_thread(1) { @inputs.read }
until @engine.ended?
sleep 0.01
end
threads.each {|thr| Thread.kill(thr) }
@engine.state
@render.draw_message(@engine)
end

def run_thread(pause=0.05)
Expand Down
33 changes: 15 additions & 18 deletions lib/game/engine.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
class Game
class Engine
attr_reader :state, :player
include Game::Modules::InstanceSetter

attr_reader :state, :player, :message

def initialize
@map = Game::Map.load_map('maps/level1')
super
@state = 'Running'
@player = Player.new
@player.load_map(@map)
load_map Game::Map.load_map('maps/level1')
end

def load_map(map)
@map = map
@player.load_map @map
end

def map
@map
end

def take_turn
if @player.location.has_object?(Game::Object::Exit)
begin
@map = Game::Map.load_map("maps/#{@map.next}")
@player.load_map(@map)
rescue NoMethodError
@ended = true
end
end
end

def skip_to(name)
@map = Game::Map.load_map("maps/#{name}")
@player.load_map(@map)
rescue NoMethodError
@ended = true
end

def ended?
!!@ended
end

def end(message)
@ended = true
@message = message
end
end
end
17 changes: 13 additions & 4 deletions lib/game/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ def bind_keys
bind_key(:a) { @player.move(:left) }
bind_key(:d) { @player.move(:right) }

bind_key(:'1') { @engine.skip_to('level1') }
bind_key(:'2') { @engine.skip_to('level2') }
bind_key(:'3') { @engine.skip_to('level3') }
bind_key(:'4') { @engine.skip_to('level4') }
bind_key(:' ') { @player.take_action }

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') }

@editor.bind(:ctrl_x) { puts "Exiting..."; exit }
end
Expand All @@ -30,5 +32,12 @@ def bind_key(key, &block)
@editor.terminal.keys[key] = [key.to_s.ord]
@editor.bind(key) { block.call }
end
private :bind_key

def skip_to(name)
@exit ||= Game::Object.instance('SkipExit', 'modules' => ['Exit'])
@exit.skip_to(name)
end
private :skip_to
end
end
83 changes: 11 additions & 72 deletions lib/game/location.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
require 'lib/game/location/modules'
require 'lib/game/location/wall'
require 'lib/game/location/edge'

class Game
class Location
attr_accessor :location_type
attr_reader :objects, :x, :y
include Game::Modules::ObjectManagement
include Game::Location::Base
include Game::Location::Passible
include Game::Location::Movement

EMPTY_CELL = 0
WALL_90 = 1
WALL_CORNER_LEFT = 4
WALL_CORNER_RIGHT = 2
WALL_CORNER = 5
WALL_0 = 3

class << self
Expand All @@ -17,7 +24,8 @@ def build(location_type, x, y)

def class_for(location_type)
case location_type
when WALL_0, WALL_90, WALL_CORNER_RIGHT, WALL_CORNER_LEFT
when WALL_0, WALL_90, WALL_CORNER_RIGHT,
WALL_CORNER_LEFT, WALL_CORNER
Wall
when EMPTY_CELL
self
Expand All @@ -34,74 +42,5 @@ def at(x, y)
@board[[x, y]]
end
end

def initialize(location_type, x, y)
@location_type = location_type
@x = x
@y = y
@objects = []
end

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

def at(direction)
tile = self.class.at(@x - 1, @y) if direction == :up
tile = self.class.at(@x + 1, @y) if direction == :down
tile = self.class.at(@x, @y - 1) if direction == :left
tile = self.class.at(@x, @y + 1) if direction == :right

tile.try(:end_point) || Game::Location::Edge.instance
end

def end_point
if has_object?(Game::Object::LocationModifier)
self.class.at(*get_object(Game::Object::LocationModifier).end_point)
else
self
end
end
private :end_point

def passible?
true
end

def add(object)
@objects << object
end

def remove(object)
@objects.delete(object)
end

def has_object?(object_class)
!!@objects.detect { |object| object.is_a?(object_class) }
end

def get_object(object_class)
@objects.detect { |object| object.is_a?(object_class) }
end

class Edge < Game::Location
def self.instance
@self ||= new
end

def initialize; end

def passible?
false
end
end

class Wall < Game::Location
def passible?
false
end
end
end
end
14 changes: 14 additions & 0 deletions lib/game/location/edge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Game
class Location
class Edge
include Game::Location::Base
include Game::Location::Impassible

def self.instance
@self ||= new
end

def initialize; end
end
end
end
55 changes: 55 additions & 0 deletions lib/game/location/modules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Game
class Location
module Movement
def at(direction)
tile = Game::Location.at(@x - 1, @y) if direction == :up
tile = Game::Location.at(@x + 1, @y) if direction == :down
tile = Game::Location.at(@x, @y - 1) if direction == :left
tile = Game::Location.at(@x, @y + 1) if direction == :right

tile.try(:end_point) || Game::Location::Edge.instance
end

def end_point
if has_object?(Game::Object::LocationModifier)
Game::Location.at(*get_object(Game::Object::LocationModifier).end_point)
else
self
end
end
private :end_point
end

module Impassible
def passible?(player_objects)
false
end
end

module Passible
def passible?(player_objects)
true
end
end

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

def initialize(location_type, x, y)
@location_type = location_type
@x = x
@y = y
@objects = []
end

def ==(val)
location_type == val.location_type &&
x == val.x &&
y == val.y
end
end
end
end
18 changes: 18 additions & 0 deletions lib/game/location/wall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Game
class Location
class Wall
include Game::Modules::ObjectManagement
include Game::Location::Base
include Game::Location::Movement

def passible?(player_objects)
return false unless has_object?(Game::Object::Passage)
passage = get_object(Game::Object::Passage)
return true if passage.passible?
key = player_objects.detect{|obj| obj.id == passage.id}
passage.open if key
key.try(:use)
end
end
end
end
3 changes: 3 additions & 0 deletions lib/game/modules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'lib/game/modules/object_management'
require 'lib/game/modules/try_helper'
require 'lib/game/modules/instance_setter'
23 changes: 23 additions & 0 deletions lib/game/modules/instance_setter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Game
module Modules
module InstanceSetter
def self.included(base)
base.extend ClassMethods
end

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

module ClassMethods
def set(instance)
@instance = instance
end

def instance
@instance
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/game/modules/object_management.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Game
module Modules
module ObjectManagement
def add(object)
@objects << object
end

def remove(object)
@objects.delete(object)
end

def has_object?(object_class)
!!@objects.detect { |object| object.is_a?(object_class) }
end

def get_object(object_class)
@objects.detect { |object| object.is_a?(object_class) }
end
end
end
end
12 changes: 12 additions & 0 deletions lib/game/modules/try_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Game
module Modules
module TryHelper
def try(method, *args, &block)
return nil if self.nil?
send(method, *args, &block)
end
end
end
end

Object.send :include, Game::Modules::TryHelper
Loading

0 comments on commit 78809b6

Please sign in to comment.