terminal_game_engine 0.1.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: b7883349a48e97c78a018fd5b5ffc4a90b0e2bb4
4
+ data.tar.gz: bdf023c3abe695541fcefc1eeb2f0bb520fe43fd
5
+ SHA512:
6
+ metadata.gz: d8cfcb5e6e8f809d2a1c855bea5c971dfc84beda8bcf2564bfb6029f833c70f567f1053a50da5f1076e06bd64b929037769351afcb18ff693a23c031585be519
7
+ data.tar.gz: 661d372b1dced48dd0a6338387eebb8f56ed260bbe714a09f55e15b0805743f3d3b355546bee79c0e681a1be0bb19a3357970bed2779eade07ce31bd1f32c079
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg/
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.10.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Odin Dutton
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.markdown ADDED
@@ -0,0 +1,21 @@
1
+ # TerminalGameEngine
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'terminal_game_engine'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install terminal_game_engine
18
+
19
+ ## License
20
+
21
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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
@@ -0,0 +1,4 @@
1
+ Dir[File.dirname(__FILE__) + "/**/*.rb"].each { |f| require f }
2
+
3
+ module TerminalGameEngine
4
+ end
@@ -0,0 +1,22 @@
1
+ module TerminalGameEngine
2
+ class Engine
3
+ attr_accessor :tick
4
+
5
+ def self.tick(&block)
6
+ self.new(&block).tap(&:call)
7
+ end
8
+
9
+ def initialize(&block)
10
+ @tick = 0
11
+ @block = block
12
+ end
13
+
14
+ def call
15
+ loop do
16
+ @block.call @tick
17
+ @tick += 1
18
+ sleep 0.1
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,84 @@
1
+ module TerminalGameEngine
2
+ class Frame
3
+ attr_reader :rows
4
+
5
+ def initialize(width, height)
6
+ @rows = height.times.map { ' ' * width }
7
+ end
8
+
9
+ def width
10
+ @rows.first.size
11
+ end
12
+
13
+ def height
14
+ @rows.size
15
+ end
16
+
17
+ def draw(x, y, sprite)
18
+ lines = sprite.split("\n")
19
+
20
+ lines.each_with_index do |line, i|
21
+ if line.size > 0 && y+lines.size <= self.height && y+i >= 0
22
+ # crop when drawing off left
23
+ if x < 0
24
+ line = line[x.abs..-1]
25
+ x = 0
26
+ end
27
+
28
+ # crop when drawing off right
29
+ if x+line.size-1 >= self.width
30
+ line = line[0..(self.width-1)-(x+line.size)]
31
+ end
32
+
33
+ @rows[y+i][x..x+line.size-1] = line
34
+ end
35
+ end
36
+ end
37
+
38
+ def draw_center(y, sprite)
39
+ sprite_width = sprite.split("\n").first.size
40
+ x = self.width / 2 - sprite_width / 2
41
+ draw x, y, sprite
42
+ end
43
+
44
+ def render
45
+ @rows.each_with_index do |row, i|
46
+ Frame.move_cursor 0, i
47
+ print row
48
+ end
49
+ end
50
+
51
+ def self.move_cursor(x, y)
52
+ print "\033[#{y+1};#{x+1}H"
53
+ end
54
+
55
+ def self.clear_screen
56
+ print "\033[2J"
57
+ end
58
+
59
+ def self.disable_cursor
60
+ print "\x1B[?25l"
61
+ end
62
+
63
+ def self.enable_cursor
64
+ print "\x1B[?25h"
65
+ end
66
+
67
+ def self.setup
68
+ clear_screen
69
+ disable_cursor
70
+
71
+ $stdin.raw!
72
+ at_exit do
73
+ puts "\r"
74
+ enable_cursor
75
+ $stdin.cooked!
76
+ system 'stty sane'
77
+ end
78
+
79
+ trap 'WINCH' do
80
+ clear_screen
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,13 @@
1
+ module TerminalGameEngine
2
+ class Input
3
+ def self.call(&block)
4
+ begin
5
+ loop do
6
+ key = $stdin.read_nonblock(1).ord
7
+ block.call key
8
+ end
9
+ rescue Errno::EAGAIN
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,52 @@
1
+ require 'logger'
2
+ require 'socket'
3
+ require 'yaml'
4
+
5
+ module TerminalGameEngine
6
+ class Network
7
+ PORT = 47357
8
+
9
+ attr_reader :logger
10
+
11
+ def initialize(logger: Logger.new('/dev/null'))
12
+ @logger = logger
13
+ end
14
+
15
+ def open_socket
16
+ @socket = UDPSocket.new
17
+ @socket.bind '0.0.0.0', PORT
18
+ @socket.setsockopt Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true
19
+ @socket.setsockopt Socket::SOL_SOCKET, Socket::SO_BROADCAST, true
20
+ rescue Errno::EADDRINUSE
21
+ $stderr.puts "Game is already running."
22
+ exit 1
23
+ end
24
+
25
+ def receive_updates(&block)
26
+ loop do
27
+ begin
28
+ data, addr = @socket.recvfrom_nonblock 8192
29
+ data = YAML.load(data)
30
+ block.call(data)
31
+ rescue Psych::SyntaxError => error
32
+ logger.error error
33
+ end
34
+ end
35
+ rescue Errno::EAGAIN
36
+ end
37
+
38
+ def send_update(data)
39
+ data = data.merge(hostname: hostname)
40
+
41
+ begin
42
+ @socket.send data.to_yaml, 0, '255.255.255.255', PORT
43
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::EMSGSIZE, Errno::ENETDOWN => error
44
+ logger.error error
45
+ end
46
+ end
47
+
48
+ def hostname
49
+ @hostname ||= `hostname -s`.strip
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ module TerminalGameEngine
2
+ class Sound
3
+ def self.play(sound)
4
+ case RUBY_PLATFORM
5
+ when /darwin/
6
+ system "afplay assets/sounds/#{sound} &"
7
+ when /linux/
8
+ system "command -v mplayer >/dev/null 2>&1 && mplayer -msglevel all=-1 -nolirc assets/sounds/#{sound} &"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module TerminalGameEngine
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,11 @@
1
+ module TerminalGameEngine
2
+ class Window
3
+ def self.rows
4
+ $stdin.winsize[0]
5
+ end
6
+
7
+ def self.columns
8
+ $stdin.winsize[1]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'terminal_game_engine/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'terminal_game_engine'
8
+ spec.version = TerminalGameEngine::VERSION
9
+ spec.authors = ['Odin Dutton']
10
+ spec.email = ['odindutton@gmail.com']
11
+
12
+ spec.summary = 'Terminal Game Engine'
13
+ spec.homepage = 'https://github.com/twe4ked/terminal_game_engine'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.10'
20
+ spec.add_development_dependency 'rake', '~> 10.0'
21
+ spec.add_development_dependency 'rspec'
22
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terminal_game_engine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Odin Dutton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-11 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - odindutton@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.markdown
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.markdown
69
+ - Rakefile
70
+ - lib/terminal_game_engine.rb
71
+ - lib/terminal_game_engine/engine.rb
72
+ - lib/terminal_game_engine/frame.rb
73
+ - lib/terminal_game_engine/input.rb
74
+ - lib/terminal_game_engine/network.rb
75
+ - lib/terminal_game_engine/sound.rb
76
+ - lib/terminal_game_engine/version.rb
77
+ - lib/terminal_game_engine/window.rb
78
+ - terminal_game_engine.gemspec
79
+ homepage: https://github.com/twe4ked/terminal_game_engine
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Terminal Game Engine
103
+ test_files: []