gemthief 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8cdbee97d70a5f6d65d76822d021c6ec4e2aeb9
4
+ data.tar.gz: a8feebdf6f915d86830c23dcb80f29266688b9ce
5
+ SHA512:
6
+ metadata.gz: 36666867ffcafdd569962e63367070f8345e2ec727ef9f39daf8a5f28f5d8dd21394388894b3e23b70331f3a5dd3444d1329ad8344f962abfc180d169b23f73f
7
+ data.tar.gz: 5e60a907ede5e49cc46020fdcbea4b2d0f425f378bdbece2adeff77a5597e90241e1fe2c16d88f8d62030b7e10387a90b45799ec13e6c53623d6471c38f4f4f5
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gemthief.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Matt Hink
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Gemthief
2
+ ## A Ruby Roguelike Framework
3
+
4
+ Gemthief is a framework for building roguelike games using the Ruby programming language. Its architecture takes many cues from the Ruby on Rails web framework, in the sense that it establishes a strong set of conventions so that users can focus more on building their game. To that end, it makes heavy use of ActiveSupport, and takes many cues from Rails.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'gemthief'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install gemthief
21
+
22
+ ## Usage
23
+ There are three main 'components' of a Gemthief roguelike: Scenes, Views, and the Executive.
24
+
25
+ ### The Executive
26
+ The Executive is the "central hub" of your game- when you start your game, it initializes Curses, instantiates the `initial_scene` and its view, and then enters the main game loop.
27
+
28
+ During the main game loop, it gets input from the user and hands it off to the `active_scene` for processing. After that input is processed, it tells the scene to refresh its view, and then refreshes Curses.
29
+
30
+ ```ruby
31
+ # /game/executive.rb
32
+ # An example Executive
33
+ class Executive < Gemthief::Executive
34
+ initial_scene(:dungeon) do |scene|
35
+ scene.generate_layout!
36
+ scene.add_monsters!
37
+ end
38
+ end
39
+ ```
40
+
41
+ TODO: The Executive will also responsible for scene transitions. The planned structure for this is to have the /game/executive.rb file define a scene transition graph, which describes how the game transitions between different types of Scene. Then, after a Scene handles input, it can provide a signal (the name of an edge) to the Executive to request transitioning to another Scene.
42
+
43
+ (Roughly equivalent to `Rails::Application`)
44
+
45
+ ### Scenes
46
+ Scenes are your game's bread and butter. Scenes have two major responsibilities: handling input, and tracking their state.
47
+
48
+ Don't let the terminology fool you- while these are called Scenes, they don't just represent the 'gamelike' parts of your game- they can also represent menus (perhaps a class selection or an inventory screen). If you need a certain part of your game to handle input in a unique way AND/OR track its state independently, use a Scene.
49
+
50
+ Scenes consist of two lifecycle events:
51
+ - `handle_input(input)`
52
+ - `render!`
53
+
54
+ In the `handle_input` step, you should determine what the user wants to do and update your Scene's state accordingly. Then, in the `render!` step, you should manipulate your View to reflect that state. For example, if I received an 'h' input, the `handle_input` might update my hero's position, moving them one step to the left. Then, in `render!`, I would tell my view to update the screen with the hero's new coordinates.
55
+
56
+ Multiple instances of a particular Scene class are allowed- this is how we might, for instance, keep track of several 'floors' of a dungeon. Each floor would be an instance of some type of "FloorScene". However, only one scene *instance* is the "active scene" at any given time. Also note that Scene instances are persistent- you don't lose one Scene's state when you transition to another one.
57
+
58
+ TODO: Scenes might be responisble for too much. In the interest of not introducing too much abstraction, I'm making them responsible for both input interpretation and state tracking. While they're intentionally similar to Rails controllers, the range of allowable inputs is considerably larger and less standardized than Rails' RESTful actions. I'm not sure if I want to introduce a recommendation for state management (i.e. models) just yet. While this framework *is* sorta MVC, I haven't given the Model part much thought yet.
59
+ TODO: Read up on desktop MVC/MVVM-type architectures
60
+ TODO: Can scenes be responsible for both state management *and* input interpretation? Or should they dispatch off to persistent Models?
61
+
62
+ (Roughly equivalent to Rails controllers.)
63
+
64
+ ### Views
65
+ Views are responsible for updating the user interface so that it reflects the state of their associated Scene. View instances are always associated with a specific Scene instance.
66
+
67
+ An important note: Views should be *stateless*. During the transition into a Scene instance, the Executive creates a brand-new View instance for it. If you transition out of a Scene, the Executive will finalize it and detach it from the scene instance, creating a new View if/when the game transitions back.
68
+
69
+ (Roughly equivalent to Rails views)
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
74
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/gemthief.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gemthief/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gemthief"
8
+ spec.version = Gemthief::VERSION
9
+
10
+ spec.author = "Matt Hink"
11
+ spec.email = "mhink1103@gmail.com"
12
+
13
+ spec.summary = %q{A Ruby Roguelike Framework}
14
+ spec.homepage = "https://github.com/mhink/gemthief"
15
+ spec.license = "MIT"
16
+
17
+ if spec.respond_to? :metadata
18
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
21
+ end
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.11"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+
30
+ spec.add_dependency 'activesupport', '~> 4.2.5'
31
+ spec.add_dependency 'mono_logger', '~> 1.1.0'
32
+ spec.add_dependency 'curses', '~> 1.0.1'
33
+ end
data/lib/gemthief.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'active_support'
2
+
3
+ module Gemthief
4
+ extend ActiveSupport::Autoload
5
+
6
+ autoload :Executive
7
+ autoload :Scene
8
+ autoload :View
9
+
10
+ class << self
11
+ @game = @game_class = nil
12
+
13
+ attr_writer :game
14
+ attr_accessor :game_class
15
+
16
+ def game
17
+ @game ||= (game_class.new if game_class)
18
+ end
19
+
20
+ def config
21
+ game.config
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ module Gemthief
2
+ module Configuration
3
+ def initialize(root)
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,101 @@
1
+ require 'gemthief'
2
+ require 'gemthief/executive/callbacks'
3
+ require 'gemthief/executive/window_management'
4
+
5
+ module Gemthief
6
+ class Executive
7
+ include Callbacks
8
+ include WindowManagement
9
+
10
+ attr_accessor :active_scene
11
+
12
+ def config
13
+ @config ||= Gemthief::Config.new(self.class.root_path(self.class.called_from))
14
+ end
15
+
16
+ def start!
17
+ run_callbacks :start do
18
+ init_curses!
19
+ self.active_scene = initial_scene
20
+ end
21
+
22
+ catch(:game_end) do
23
+ loop do
24
+ active_scene.render!
25
+ refresh_curses!
26
+ active_scene.handle_input stdscr.getch
27
+ end
28
+ end
29
+ rescue SystemExit
30
+ # no-op
31
+ rescue => ex
32
+ # TODO: Move logging into the framework
33
+ # log! ex
34
+ ensure
35
+ run_callbacks :finish
36
+ end
37
+
38
+ class << self
39
+ attr_accessor :called_from
40
+
41
+ attr_accessor :initial_scene_name
42
+ attr_accessor :initial_scene_setup
43
+
44
+ def inherited(base)
45
+ super
46
+ Gemthief.game_class = base
47
+
48
+ base.called_from = begin
49
+ call_stack = if Kernel.respond_to?(:caller_locations)
50
+ caller_locations.map { |l| l.absolute_path || l.path }
51
+ else
52
+ caller.map { |p| p.sub(/:\d+.*/, '') }
53
+ end
54
+
55
+ File.dirname(callstack.detect { |p| p !~ %r[lib/gemthief] })
56
+ end
57
+ end
58
+
59
+ def find_root(from)
60
+ find_root_with_flag "lib", from
61
+ end
62
+
63
+ def find_root_with_flag(flag, root_path, default=nil)
64
+ while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
65
+ parent = File.dirname(root_path)
66
+ root_path = parent != root_path && parent
67
+ end
68
+
69
+ root = File.exist?("#{root_path}/#{flag}") ? root_path : default
70
+ raise "Could not find root path for #{self}" unless root
71
+
72
+ Pathname.new File.realpath root
73
+ end
74
+
75
+ def initial_scene(scene, &block)
76
+ @initial_scene_name = case scene
77
+ when Class then scene.name.underscore
78
+ when String then scene
79
+ when Symbol then scene
80
+ end
81
+ @initial_scene_setup = block
82
+ end
83
+ end
84
+
85
+ def initial_scene
86
+ @scene_instance ||= begin
87
+ scene_name = if self.class.initial_scene_name.nil?
88
+ "Initial"
89
+ else
90
+ self.class.initial_scene_name
91
+ end
92
+ scene_class = scene_name.to_s.camelize + "Scene"
93
+ scene_instance = scene_class.constantize.new
94
+ if self.class.initial_scene_setup.respond_to? :call
95
+ self.class.initial_scene_setup.call(scene_instance)
96
+ end
97
+ scene_instance
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/callbacks'
3
+
4
+ module Gemthief
5
+ class Executive
6
+ module Callbacks
7
+ extend ActiveSupport::Concern
8
+ include ActiveSupport::Callbacks
9
+
10
+ CALLBACKS = [:start, :finish]
11
+
12
+ included do
13
+ define_callbacks *CALLBACKS
14
+ end
15
+
16
+ module ClassMethods
17
+ [:before, :around, :after].product(CALLBACKS).each do |type, callback|
18
+ define_method("#{type}_#{callback}") do |&block|
19
+ set_callback callback, type, &block
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,85 @@
1
+ require 'active_support/concern'
2
+
3
+ module Gemthief
4
+ class Executive
5
+ module WindowManagement
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ include Curses
10
+ end
11
+
12
+ def init_curses!
13
+ init_screen
14
+ cbreak
15
+ noecho
16
+ srand
17
+ curs_set 0
18
+ trap("INT") { exit 0; }
19
+ end
20
+
21
+ def refresh_curses!
22
+ stdscr.noutrefresh
23
+ active_scene.view.refresh!
24
+ doupdate
25
+ end
26
+
27
+ def root_window
28
+ stdscr
29
+ end
30
+
31
+ class Panel
32
+ def initialize(screen_x, screen_y, screen_width, screen_height, content_width, content_height, content_x=0, content_y=0)
33
+ @screen_x = screen_x
34
+ @screen_y = screen_y
35
+ @screen_width = screen_width
36
+ @screen_height = screen_height
37
+ @content_width = content_width
38
+ @content_height = content_height
39
+ @content_x = content_x
40
+ @content_y = content_y
41
+
42
+ @outer = Curses::Pad.new(@content_height, @content_width)
43
+ @inner = @outer.subpad(@content_height, @content_width,1,1)
44
+ end
45
+
46
+ # TODO: Use Forwardable for these and other methods
47
+ def clear
48
+ @inner.clear
49
+ end
50
+ def setpos(at_line, at_col)
51
+ @inner.setpos(at_line, at_col)
52
+ end
53
+ def addstr(char)
54
+ @inner.addstr("@")
55
+ end
56
+
57
+ def noutrefresh
58
+ @outer.noutrefresh(
59
+ @content_y, @content_x,
60
+ @screen_y, @screen_x,
61
+ screen_maxrow, screen_maxcol
62
+ )
63
+ @outer.box(?|, ?-)
64
+ @inner.noutrefresh(
65
+ @content_y, @content_x,
66
+ @screen_y+1, @screen_x+1,
67
+ screen_maxrow-2, screen_maxcol-2
68
+ )
69
+ end
70
+
71
+ def dimensions
72
+ { x: @width, y: @height }
73
+ end
74
+
75
+ def screen_maxrow
76
+ @screen_y + @screen_height
77
+ end
78
+
79
+ def screen_maxcol
80
+ @screen_x + @screen_width
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,33 @@
1
+ module Gemthief
2
+ class Scene
3
+ class << self
4
+ attr_reader :view_name
5
+ def default_view(view=nil)
6
+ return @view_name if view.nil?
7
+
8
+ @view_name = case view
9
+ when Class then view.name.underscore
10
+ when String then view
11
+ when Symbol then view
12
+ end
13
+ end
14
+ end
15
+
16
+ def view
17
+ @view ||= begin
18
+ view_name = if self.class.view_name.nil?
19
+ self.class.name.gsub(/Scene$/, "")
20
+ else
21
+ self.class.view_name
22
+ end
23
+ view_class = view_name.to_s.camelize + "View"
24
+ view_class.constantize.new(Gemthief.game.root_window)
25
+ end
26
+ end
27
+
28
+ def initialize
29
+ super
30
+ view
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Gemthief
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ module Gemthief
2
+ class View
3
+ def initialize(root_window)
4
+ @root_window = root_window
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gemthief
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Hink
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.2.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.2.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: mono_logger
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: curses
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.0.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.0.1
97
+ description:
98
+ email: mhink1103@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - ".rspec"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - gemthief.gemspec
111
+ - lib/gemthief.rb
112
+ - lib/gemthief/config.rb
113
+ - lib/gemthief/executive.rb
114
+ - lib/gemthief/executive/callbacks.rb
115
+ - lib/gemthief/executive/window_management.rb
116
+ - lib/gemthief/scene.rb
117
+ - lib/gemthief/version.rb
118
+ - lib/gemthief/view.rb
119
+ homepage: https://github.com/mhink/gemthief
120
+ licenses:
121
+ - MIT
122
+ metadata:
123
+ allowed_push_host: https://rubygems.org
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.8
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: A Ruby Roguelike Framework
144
+ test_files: []