tardvig 0.2.0

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: 724c04628100a4d2d3fe3c4f246af597a26807c3
4
+ data.tar.gz: db371a8e466d8b3de396119bbeca10581b335f1b
5
+ SHA512:
6
+ metadata.gz: d9139f6e0c19bad867a5310718aecc2139f20e1009b8a75494eb3232eeca23fe9b2248ac9e2e04443c04308d3ba0cbde4f5b294a40cb8d08b607e46759713d46
7
+ data.tar.gz: 982a8884b2c6dfe3bf10172d4d2ada79634b3824a0f8ee60adbc61fe720550b1b12c49d9ed0e1f1cf02ed18b63cb472f768bab22ca7299290081c0a46471e817
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2016 Vadim Saprykin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Tardvig [![Gem Version](https://badge.fury.io/rb/tardvig.svg)](https://badge.fury.io/rb/tardvig)
2
+
3
+ [RubyGem](https://rubygems.org/gems/tardvig) |
4
+ [RubyDoc](http://www.rubydoc.info/gems/tardvig) |
5
+
6
+ **Warning!** I do **not** recommend to use it.
7
+ This gem is in the alpha stage of development now and it is very badly written
8
+ and it does almost nothing.
9
+
10
+ My English is very bad. Please, do not be mad at me. You can correct my errors
11
+ if you want.
12
+
13
+ ## Description
14
+ Lightweight structure for Ruby-based game engines.
15
+
16
+ In fact, this is just a bundle of several simple helpful classes and modules
17
+ which can be integrated and extended to make a game engine.
18
+
19
+ ### What is it for?
20
+ It was developed for my single-player browser game which is divided into levels,
21
+ but probably this gem can be used for any other types of games with any IO, I
22
+ tried to make it as flexible as possible.
23
+ For example, I used a similar structure for my text-based game with console IO
24
+ earlier.
25
+
26
+ *Important!* I recommend to familiarize with this gem before using it because
27
+ it may be not applicable to your game.
28
+
29
+ ## Getting Started
30
+ Tardvig consists of some modules and classes (i will call them "parts"). In this
31
+ guide I am not going to describe them, because you already have docs as their
32
+ description and specs as their examples. I'll tell you what you should pay
33
+ attention to.
34
+ I recommend you to read the description of parts immediately
35
+ after you noticed their names so you can understand further guide (however, the
36
+ guide is short).
37
+
38
+ `Events` mixin and `Command` class are base parts which are not related directly
39
+ to the game structure, but are very useful for parts which are related to it.
40
+
41
+ The main part of a Tardvig-based game is `Act` class. Game is a sequence of
42
+ acts. Acts can have different types.
43
+ Example: Tardvig-based 3D FPS would be a sequence of acts of types `shooting`
44
+ (user can run and shoot; each location is an act) and `cutscene` (user watches
45
+ video), finished with a `credits` act (user watches authors' names).
46
+
47
+ Important fact is that acts have attribute `subject`.
48
+
49
+ Acts have attribute `io` which should contain an instance of any class which
50
+ gives you ability to communicate with something that will dislay your game.
51
+ I recommend you to communicate via `GameIO`.
52
+
53
+ Well, now you know the basic parts. Final thing you should to do before you can
54
+ use this gem is reading the example. It takes place in the
55
+ `spec/integration_spec.rb` file.
56
+
57
+ ## You can also see...
58
+ `Rakefile` - Rake tasks for contribution
59
+
60
+ `tardvig.gemspec` - here you can see my name and email and this gem's depencies
61
+
62
+ `LICENSE` - this gem's license
@@ -0,0 +1,69 @@
1
+ module Tardvig
2
+ # @abstract Represents the abstract part of your game.
3
+ # It may be anything, depends on your realization: level, location,
4
+ # cut-scene, credits, etc.
5
+ class Act < Command
6
+ class << self
7
+ # @overload act_type(type)
8
+ # @param type [String, Symbol] set your custom act type. This is needed
9
+ # for your display to differ acts and correspondingly intepret them.
10
+ # @overload act_type
11
+ # @return type of your act
12
+ def act_type(type = nil)
13
+ if type
14
+ @type = type
15
+ elsif @type
16
+ @type
17
+ elsif superclass.respond_to? :act_type
18
+ superclass.act_type
19
+ end
20
+ end
21
+
22
+ # @overload subject(value)
23
+ # If there was MVC, I would
24
+ # say act is a model, subject is its data and other part of act is
25
+ # business logic.
26
+ # But there is no MVC, so *subject is the content of
27
+ # your act and the methods of the act should process it*.
28
+ #
29
+ # For example, if your act is a location of FPS, subject is the location
30
+ # file and methods of your act should control player's enemies. If act
31
+ # is a cut-scene, subject is the video file (probably name of the file).
32
+ # @param value [Object] subject of your act.
33
+ # @overload subject
34
+ # @return [Object] subject of your act
35
+ def subject(value = nil)
36
+ if value
37
+ @subject = value
38
+ else
39
+ @subject
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def process
47
+ end
48
+
49
+ def execute
50
+ notify_display
51
+ process
52
+ end
53
+
54
+ # This method notifies your display via GameIO when the act is executed.
55
+ # You can redefine it if you want to send another message or do not want to
56
+ # send anything.
57
+ # @!visibility public
58
+ def notify_display
59
+ io.happen :act_start, type: self.class.act_type, subject: display_format
60
+ end
61
+
62
+ # This method should return the object which will be sent to the display.
63
+ # By default it returns the subject. But you can redefine it.
64
+ # @!visibility public
65
+ def display_format
66
+ self.class.subject
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,58 @@
1
+ module Tardvig
2
+ # @abstract The class represents abstract algorithm.
3
+ # It is something like Proc, but your code is stored in the class instead of
4
+ # object so you can use instance variables and divide your code into
5
+ # methods.
6
+ #
7
+ # See the `spec` directory for example.
8
+ class Command
9
+ # @param params [Hash] these key => value pairs will be instance variables
10
+ # with reader methods
11
+ def initialize(params = {})
12
+ handle_params(params)
13
+ end
14
+
15
+ # Executes your process (you should redefine the `process` method)
16
+ # @param (see #initialize)
17
+ # @return [Command] self
18
+ def call(params = {})
19
+ handle_params(params)
20
+ execute
21
+ self
22
+ end
23
+
24
+ # Creates a new instance and executes it.
25
+ # @param (see #call)
26
+ # @return self
27
+ def self.call(params = {})
28
+ new.call(params)
29
+ end
30
+
31
+ private
32
+
33
+ # The method is used for be redefined in the situation when you need to
34
+ # change the execution order. By default it executes `process` only.
35
+ # See the specs for example.
36
+ # @!visibility public
37
+ def execute
38
+ process
39
+ end
40
+
41
+ # @abstract Redefine this method and it will do whatever you want whilst
42
+ # execution
43
+ # @!visibility public
44
+ def process
45
+ raise NoMethodError, 'You should define #process for your Command'
46
+ end
47
+
48
+ def handle_params(params)
49
+ params.each_pair do |name, value|
50
+ var_name = :"@#{name}"
51
+ instance_variable_set var_name, value
52
+ define_singleton_method name do
53
+ instance_variable_get var_name
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,52 @@
1
+ module Tardvig
2
+ # The mixin gives ability to have events
3
+ # @see https://en.wikipedia.org/wiki/Event_%28computing%29
4
+ module Events
5
+ # Binds given listener (handler/callback) to given event name
6
+ # @param event [Object] any custom identificator. Your listener will be
7
+ # executed only when you trigger event with this identificator.
8
+ # @param listener [#call] this object will be executed (through the #call
9
+ # method) when you trigger the given event.
10
+ def on(event, &listener)
11
+ listeners(event) << listener
12
+ end
13
+
14
+ # Does the same as {#on}, but the listener will be executed only once, then
15
+ # it will be deleted.
16
+ # @param (see #on)
17
+ def on_first(event, &listener)
18
+ throwaway_callback = proc do |*args|
19
+ remove_listener event, throwaway_callback
20
+ listener.call(*args)
21
+ end
22
+ listeners(event) << throwaway_callback
23
+ end
24
+
25
+ # Unbinds given listener from the given event name
26
+ def remove_listener(event, listener = nil)
27
+ if listener.nil?
28
+ listeners(event).clear
29
+ else
30
+ listeners(event).delete listener
31
+ end
32
+ end
33
+
34
+ # Executes all the listeners which are bound to the given event name
35
+ # @param data [Object] the object will be passed as an argument to the
36
+ # listeners
37
+ def happen(event, data = nil)
38
+ listeners(event).clone.each do |listener|
39
+ listener.call(data)
40
+ end
41
+ end
42
+
43
+ alias trigger happen
44
+
45
+ # @return [Array] array with all the listeners which are bound to the given
46
+ # event name
47
+ def listeners(event)
48
+ @listeners ||= {}
49
+ @listeners[event] ||= []
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ module Tardvig
2
+ # Interface which represents a connection to something that will display your
3
+ # game (I will call something that will display your game "display").
4
+ # Every display has its GameIO so if your game has a server and many people
5
+ # connected then you will have many GameIO instances.
6
+ #
7
+ # Communication should be realized via events.
8
+ # @abstract
9
+ class GameIO
10
+ include Events
11
+ end
12
+ end
@@ -0,0 +1,51 @@
1
+ module Tardvig
2
+ # This is a hash which can be saved to a file (via YAML). It also has some
3
+ # events.
4
+ #
5
+ # It is useful for progress saving, configuration and settings files,
6
+ # localization files and many other things.
7
+ #
8
+ # Events:
9
+ # * `save` happen before saving data to a file. Arguments: the hash itself
10
+ # * `load` happen after loading data from a file. Arguments: the hash itself
11
+ class SavedHash < Hash
12
+ include Events
13
+
14
+ def initialize(io = nil)
15
+ load io unless io.nil?
16
+ end
17
+
18
+ # Save the hash to the file through YAML
19
+ # @param io [IO, String] the IO instance or the name of the file.
20
+ # The data will be saved here.
21
+ def save(io)
22
+ open_file io, 'w' do |f|
23
+ trigger :save, self
24
+ f.write YAML.dump(self)
25
+ end
26
+ end
27
+
28
+ # Loads the data from the YAML file to itself. If there are old data in the
29
+ # hash, it overrides them.
30
+ # @param io [IO, String] the IO instance or the name of the file.
31
+ # The data will be loaded from here.
32
+ def load(io)
33
+ open_file io, 'r' do |f|
34
+ merge! YAML.load(f.read)
35
+ trigger :load, self
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def open_file(io, mode)
42
+ if io.respond_to?(:read) && io.respond_to?(:write)
43
+ yield io
44
+ elsif io.is_a? String
45
+ File.open(io, mode) { |f| yield f }
46
+ else
47
+ raise ArgumentError
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,53 @@
1
+ module Tardvig
2
+ # Container which initializes and gives you access to its tools (objects).
3
+ # Useful when you need to do a DSL
4
+ class Toolkit
5
+ class << self
6
+ # @return [Hash] your tools
7
+ def tools
8
+ @tools ||= {}
9
+ end
10
+
11
+ # Adds a new tool (object).
12
+ #
13
+ # If the given object has either `call` or `new` methods then result of
14
+ # the method execution will be used as a tool instead. When the `call`
15
+ # method of your tool is executed, the toolkit will be given as the first
16
+ # argument and the `params` (see {#initialize}) as the second.
17
+ # @param name [Symbol] you can access your tool via this method name
18
+ # @param tool_itself [#call, #new, Object] your tool.
19
+ def tool(name, tool_itself = nil)
20
+ tools[name] = tool_itself || Proc.new
21
+ end
22
+ end
23
+
24
+ # Creates a new toolbox instance and initializes its tools.
25
+ # @param params [Hash] it will be available as the second argument passed to
26
+ # the `call` methods of your tools.
27
+ def initialize(params = {})
28
+ @params = params
29
+ create_tools_readers
30
+ end
31
+
32
+ private
33
+
34
+ def tool_initialize(tool)
35
+ if tool.respond_to? :call
36
+ tool.call self, @params
37
+ elsif tool.respond_to? :new
38
+ tool.new
39
+ else
40
+ tool
41
+ end
42
+ end
43
+
44
+ def create_tools_readers
45
+ self.class.tools.each do |name, tool|
46
+ instance_variable_set "@#{name}", tool_initialize(tool)
47
+ define_singleton_method name do
48
+ instance_variable_get "@#{name}"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Tardvig
2
+ VERSION = '0.2.0'.freeze
3
+ end
data/lib/tardvig.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'yaml'
2
+
3
+ require_relative 'tardvig/version.rb'
4
+ require_relative 'tardvig/events.rb'
5
+ require_relative 'tardvig/command.rb'
6
+ require_relative 'tardvig/saved_hash.rb'
7
+ require_relative 'tardvig/toolkit.rb'
8
+ require_relative 'tardvig/gameio.rb'
9
+ require_relative 'tardvig/act.rb'
10
+
11
+ # The main namespace for the gem
12
+ module Tardvig
13
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tardvig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Vadim Saprykin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '11.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '11.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ description: 'Tardvig is a bundle of classes and modules which can be integrated and
70
+ extended to make a game engine.
71
+
72
+ '
73
+ email: sprkweb@ya.ru
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - LICENSE
79
+ - README.md
80
+ - lib/tardvig.rb
81
+ - lib/tardvig/act.rb
82
+ - lib/tardvig/command.rb
83
+ - lib/tardvig/events.rb
84
+ - lib/tardvig/gameio.rb
85
+ - lib/tardvig/saved_hash.rb
86
+ - lib/tardvig/toolkit.rb
87
+ - lib/tardvig/version.rb
88
+ homepage: https://github.com/sprkweb/tardvig
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 2.0.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Lightweight structure for Ruby-based games
112
+ test_files: []