gaminator 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +1 -0
- data/gaminator.gemspec +19 -0
- data/lib/gaminator.rb +5 -0
- data/lib/gaminator/runner.rb +139 -0
- data/lib/gaminator/version.rb +3 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Tomasz Werbicki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Gaminator
|
2
|
+
|
3
|
+
A simple wrapper around Curses intended for writing ASCII games
|
4
|
+
|
5
|
+
Post game submissions here: https://gist.github.com/4684252
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
The Gaminator::Runner class implements the base methods used to
|
10
|
+
run the game, the event loop being the most important. Initialize
|
11
|
+
it like so:
|
12
|
+
|
13
|
+
```
|
14
|
+
# If you want the game to fill the whole screen
|
15
|
+
Gaminator::Runner.new(SkiGame).run
|
16
|
+
|
17
|
+
# If you want the game to have a set size (in rows/cols)
|
18
|
+
Gaminator::Runner.new(SkiGame, :rows => 30, :cols => 80).run
|
19
|
+
```
|
20
|
+
|
21
|
+
The Game that is run by the GameRunner has to implement the
|
22
|
+
following methods:
|
23
|
+
|
24
|
+
* objects - the array of objects that are displayed on the screen
|
25
|
+
* input_map - the mapping between the keyboard keys and game actions
|
26
|
+
* tick - the method that is called for every loop cycle
|
27
|
+
* exit_message - the message displayed when the game is finished
|
28
|
+
* textbox_content - the message displayed at the bottom of the game window
|
29
|
+
* wait? - determine whether to wait for input before next tick
|
30
|
+
* sleep_time - the time interval beteen two event loop cycles
|
31
|
+
|
32
|
+
The objects that are displayed on the screen have to implement the following
|
33
|
+
interface:
|
34
|
+
|
35
|
+
* x - the x position of the object
|
36
|
+
* y - the y position of the object
|
37
|
+
* char (optional) - the text representation of the object
|
38
|
+
* color (optional) - the color of the object
|
39
|
+
* texture (optional) - an array of string representing a row in a bigger shape
|
40
|
+
* colors (optional) - an array of arrays (rows) of Curses color constants,
|
41
|
+
the texture will be colored accordingly
|
42
|
+
|
43
|
+
You have to define either char or texture method on each object.
|
44
|
+
|
45
|
+
Available colors:
|
46
|
+
|
47
|
+
```
|
48
|
+
Curses::COLOR_WHITE
|
49
|
+
Curses::COLOR_RED
|
50
|
+
Curses::COLOR_BLUE
|
51
|
+
Curses::COLOR_GREEN
|
52
|
+
Curses::COLOR_CYAN
|
53
|
+
Curses::COLOR_MAGENTA
|
54
|
+
Curses::COLOR_YELLOW
|
55
|
+
```
|
56
|
+
|
57
|
+
## Installation
|
58
|
+
|
59
|
+
Add this line to your application's Gemfile:
|
60
|
+
|
61
|
+
gem "gaminator", :git => "git://github.com/futuresimple/gaminator.git"
|
62
|
+
|
63
|
+
Execute:
|
64
|
+
|
65
|
+
$ bundle
|
66
|
+
|
67
|
+
And put following lines on top of your script file:
|
68
|
+
|
69
|
+
require "bundler/setup"
|
70
|
+
require "gaminator"
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/gaminator.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gaminator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "gaminator"
|
8
|
+
gem.version = Gaminator::VERSION
|
9
|
+
gem.authors = ["Tomasz Werbicki", "Pawel Obrok"]
|
10
|
+
gem.email = ["tomasz@werbicki.net", "pawel.obrok@gmail.com"]
|
11
|
+
gem.description = "A simple wrapper around Curses for writing ASCII-art games"
|
12
|
+
gem.summary = "See https://github.com/futuresimple/gaminator/blob/master/README.md"
|
13
|
+
gem.homepage = "https://github.com/futuresimple/gaminator"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/gaminator.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "curses"
|
4
|
+
|
5
|
+
module Gaminator
|
6
|
+
class Runner
|
7
|
+
include Curses
|
8
|
+
|
9
|
+
def initialize(game_class, options = {})
|
10
|
+
if options[:rows] && options[:cols]
|
11
|
+
resizeterm(options[:rows], options[:cols])
|
12
|
+
end
|
13
|
+
|
14
|
+
init_screen
|
15
|
+
start_color
|
16
|
+
cbreak
|
17
|
+
noecho
|
18
|
+
stdscr.nodelay = 1
|
19
|
+
curs_set(0)
|
20
|
+
|
21
|
+
[
|
22
|
+
COLOR_WHITE, COLOR_RED, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
|
23
|
+
COLOR_MAGENTA, COLOR_YELLOW
|
24
|
+
].each do |color|
|
25
|
+
init_pair(color, color, COLOR_BLACK)
|
26
|
+
end
|
27
|
+
|
28
|
+
@plane_width = cols
|
29
|
+
@plane_height = lines - 5
|
30
|
+
@plane = Window.new(@plane_height, @plane_width, 0, 0)
|
31
|
+
@plane.box("|", "-")
|
32
|
+
|
33
|
+
@textbox_width = cols
|
34
|
+
@textbox_height = 5
|
35
|
+
@textbox = Window.new(@textbox_height, @textbox_width, @plane_height, 0)
|
36
|
+
@textbox.box("|", "-")
|
37
|
+
|
38
|
+
@game = game_class.new(@plane_width - 2, @plane_height - 2)
|
39
|
+
end
|
40
|
+
|
41
|
+
def run
|
42
|
+
begin
|
43
|
+
loop do
|
44
|
+
tick_game
|
45
|
+
|
46
|
+
render_objects
|
47
|
+
render_textbox
|
48
|
+
|
49
|
+
@plane.refresh
|
50
|
+
@textbox.refresh
|
51
|
+
|
52
|
+
handle_input
|
53
|
+
|
54
|
+
clear_plane
|
55
|
+
clear_textbox
|
56
|
+
|
57
|
+
sleep(@game.sleep_time)
|
58
|
+
end
|
59
|
+
ensure
|
60
|
+
close_screen
|
61
|
+
puts
|
62
|
+
puts @game.exit_message
|
63
|
+
puts
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def handle_input
|
70
|
+
if @game.wait?
|
71
|
+
@textbox.timeout = -1
|
72
|
+
else
|
73
|
+
@textbox.timeout = 0
|
74
|
+
end
|
75
|
+
char = @textbox.getch
|
76
|
+
action = @game.input_map[char]
|
77
|
+
if action && @game.respond_to?(action)
|
78
|
+
@game.send(action)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def tick_game
|
83
|
+
@game.tick
|
84
|
+
end
|
85
|
+
|
86
|
+
def render_objects
|
87
|
+
@game.objects.each do |object|
|
88
|
+
render_object(object)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def render_object(object)
|
93
|
+
color = object.respond_to?(:color) ? object.color : COLOR_WHITE
|
94
|
+
texture = object.texture if object.respond_to?(:texture)
|
95
|
+
texture ||= [object.char]
|
96
|
+
|
97
|
+
texture.each.with_index do |row,row_index|
|
98
|
+
row.each_char.with_index do |pixel,pixel_index|
|
99
|
+
x = object.x + 1 + pixel_index
|
100
|
+
y = object.y + 1 + row_index
|
101
|
+
next if x < 1 || x >= @plane_width
|
102
|
+
next if y < 1 || y >= @plane_height
|
103
|
+
|
104
|
+
if object.respond_to?(:colors) && object.colors
|
105
|
+
color = object.colors[row_index][pixel_index]
|
106
|
+
end
|
107
|
+
|
108
|
+
@plane.setpos(y,x)
|
109
|
+
@plane.attron(color_pair(color) | A_NORMAL) do
|
110
|
+
@plane.addstr(pixel)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def render_textbox
|
117
|
+
@textbox.setpos(2, 3)
|
118
|
+
@textbox.addstr(@game.textbox_content)
|
119
|
+
end
|
120
|
+
|
121
|
+
def clear_plane
|
122
|
+
1.upto(@plane_height - 2) do |y|
|
123
|
+
1.upto(@plane_width - 2) do |x|
|
124
|
+
@plane.setpos(y, x)
|
125
|
+
@plane.addstr(" ")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def clear_textbox
|
131
|
+
1.upto(@textbox_height - 2) do |y|
|
132
|
+
1.upto(@textbox_width - 2) do |x|
|
133
|
+
@textbox.setpos(y, x)
|
134
|
+
@textbox.addstr(" ")
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gaminator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tomasz Werbicki
|
9
|
+
- Pawel Obrok
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-03-23 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: A simple wrapper around Curses for writing ASCII-art games
|
16
|
+
email:
|
17
|
+
- tomasz@werbicki.net
|
18
|
+
- pawel.obrok@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- gaminator.gemspec
|
29
|
+
- lib/gaminator.rb
|
30
|
+
- lib/gaminator/runner.rb
|
31
|
+
- lib/gaminator/version.rb
|
32
|
+
homepage: https://github.com/futuresimple/gaminator
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.24
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: See https://github.com/futuresimple/gaminator/blob/master/README.md
|
56
|
+
test_files: []
|
57
|
+
has_rdoc:
|