phosphor 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 21f8fe8fed4153375b120bb9f15fde3ea1501d225274731dc4f73fefedaa922d
4
+ data.tar.gz: b63eb1a1bfa080004ef0cd5c1ffd2feb85f5b04633243c6cd786a135fca355c1
5
+ SHA512:
6
+ metadata.gz: d5d0a29df062d80ec4918b9670bdd1db524d6fb330d98d9cc273173b7e283e4a3ce4c7b82258219383954111bd5e0b75446294cf83eff823f75d7ffcf78d70e9
7
+ data.tar.gz: 595929050f00724b15b7d26abf4fd8ccbe17f75504593a08c42d327cfa3db4662335e4f215e470c7ce743a68ca2b6d7d087cfdf5a00ec58dcd81183476193255
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders 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, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at albertalef@protonmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Albert Alef
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,39 @@
1
+ # Phosphor
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/phosphor`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/phosphor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/phosphor/blob/master/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Phosphor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/phosphor/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/razor'
5
+ require 'debug'
6
+
7
+ class LinePlotter < Razor::App
8
+ def on_start
9
+ Razor::Mouse::Utils.enable_xterm_1003
10
+
11
+ @fps_text = Text.new('FPS: x')
12
+ @mouse_text = Text.new('', 1, 1)
13
+ @lines = []
14
+ @line = Line.new(0, 0, 0, 0)
15
+ @line.hide
16
+ @lines << @line
17
+
18
+ @rendered_frames = 0
19
+
20
+ EM.add_periodic_timer(1) do
21
+ @fps_text.text = "FPS: #{@rendered_frames}"
22
+ @rendered_frames = 0
23
+ end
24
+
25
+ on(:mouse) do |event|
26
+ @mouse_text.text = "X: #{event.x_pos} Y: #{event.y_pos}"
27
+ end
28
+
29
+ on(:mouse_button) do |event|
30
+ if event.button == :left
31
+
32
+ @last_pressed_x = event.x_pos
33
+ @last_pressed_y = event.y_pos
34
+
35
+ if event.pressed?
36
+ @line.x1_pos = event.x_pos
37
+ @line.y1_pos = event.y_pos
38
+ else
39
+ @line = Line.new(0, 0, 0, 0)
40
+ @line.hide
41
+
42
+ @lines << @line
43
+
44
+ @last_pressed_x = nil
45
+ end
46
+ else
47
+ @lines.each { |a| a.stroke_char = event.pressed? ? '@' : '-' }
48
+ end
49
+ end
50
+
51
+ on(:mouse_scroll) do |event|
52
+ @lines.each(&:hide)
53
+ end
54
+
55
+ on(:mouse_move) do |event|
56
+ next unless @last_pressed_x
57
+
58
+ if (event.button == :left) && @line
59
+ @line.unhide
60
+ @line.x2_pos = event.x_pos
61
+ @line.y2_pos = event.y_pos
62
+ end
63
+ end
64
+ end
65
+
66
+ def on_update
67
+ @rendered_frames += 1
68
+ end
69
+ end
70
+
71
+ LinePlotter.new.start
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/razor'
5
+ require 'debug'
6
+
7
+ class MouseTracker < Razor::App
8
+ def on_start
9
+ Razor::Mouse::Utils.enable_xterm_1003
10
+
11
+ Razor::Events::InputEventReactor.start
12
+
13
+ @fps_text = Razor::Objects::Text.new('FPS: x')
14
+ @mouse_text = Razor::Objects::Text.new('', 1, 1)
15
+ @line = Razor::Objects::Line.new(0, 0, 0, 0)
16
+ @line.hide
17
+ @box = Razor::Objects::Box.new(
18
+ 8,
19
+ 8,
20
+ 10,
21
+ 10,
22
+ x_anchor: :middle,
23
+ fill_char: '-'
24
+ )
25
+
26
+ @rendered_frames = 0
27
+
28
+ EM.add_periodic_timer(1) do
29
+ @fps_text.text = "FPS: #{@rendered_frames}"
30
+ @rendered_frames = 0
31
+ end
32
+
33
+ Razor::Events::InputEventReactor.on(:mouse) do |event|
34
+ @mouse_text.text = "X: #{event.x_pos} Y: #{event.y_pos}"
35
+ end
36
+
37
+ @box.on(:mouse_button) do |box, event|
38
+ if event.button == :left
39
+
40
+ @last_pressed_x = event.x_pos
41
+ @last_pressed_y = event.y_pos
42
+
43
+ @box_on_last_pressed_x = @box.x_pos
44
+ @box_on_last_pressed_y = @box.y_pos
45
+
46
+ if event.pressed?
47
+ @box.fill_char = 'X'
48
+
49
+ @line.x1_pos = box.x_pos
50
+ @line.y1_pos = box.y_pos
51
+ else
52
+ @line = Razor::Objects::Line.new(0, 0, 0, 0)
53
+ @line.hide
54
+
55
+ @last_pressed_x = nil
56
+ @box.fill_char = '-'
57
+ end
58
+ end
59
+ end
60
+
61
+ Razor::Events::InputEventReactor.on(:mouse_move) do |event|
62
+ next unless @last_pressed_x
63
+
64
+ if event.button == :left
65
+ @box.fill_char = 'X'
66
+
67
+ @box.x_pos = @box_on_last_pressed_x + event.x_pos - @last_pressed_x
68
+ @box.y_pos = @box_on_last_pressed_y + event.y_pos - @last_pressed_y
69
+
70
+ if @line
71
+ @line.unhide
72
+ @line.x2_pos = @box.x_pos
73
+ @line.y2_pos = @box.y_pos
74
+ end
75
+ else
76
+ @box.fill_char = '-'
77
+ end
78
+ end
79
+ end
80
+
81
+ def on_update
82
+ @rendered_frames += 1
83
+ end
84
+ end
85
+
86
+ MouseTracker.new.start
data/examples/rays.rb ADDED
@@ -0,0 +1,136 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/razor'
5
+ require 'debug'
6
+
7
+ class Rays < Razor::App
8
+ FILLER_CHARS = [
9
+ '@', '#', 'W', '$', 'M', 'G', 'B', '%',
10
+ 'E', 'O', 'C', 'S', 'z', 'o', 'r', 'l',
11
+ 'i', ':', ',', '.'
12
+ ]
13
+
14
+ BRAILE_CHARS = ['⣿', '⣷', '⢾', '⢹', '⢩', '⠇', '⠃', '⠁']
15
+
16
+ def char_color(actual, max)
17
+ percentage = actual / max.to_f
18
+
19
+ FILLER_CHARS[(FILLER_CHARS.count * percentage).ceil] || '⠁'
20
+ end
21
+
22
+ def on_start
23
+ Razor::Mouse::Utils.enable_xterm_1003
24
+
25
+ @fps_text = Text.new('FPS: x')
26
+
27
+ @rendered_frames = 0
28
+
29
+ EM.add_periodic_timer(1) do
30
+ @fps_text.text = "FPS: #{@rendered_frames}"
31
+ @rendered_frames = 0
32
+ end
33
+
34
+ Box.new(
35
+ 140,
36
+ 200,
37
+ 100,
38
+ 35,
39
+ x_anchor: :middle,
40
+ y_anchor: :middle
41
+ )
42
+
43
+ Box.new(
44
+ 30,
45
+ 30,
46
+ 50,
47
+ 35,
48
+ x_anchor: :middle,
49
+ y_anchor: :middle
50
+ )
51
+
52
+ @lines = []
53
+ @walls = {}
54
+
55
+ @main_angle = 0
56
+
57
+ rays = 90
58
+ rays.times do |i|
59
+ final_angle = (i * 10) - 45 - 180 + @main_angle
60
+ line = Ray.new(
61
+ 90,
62
+ 50,
63
+ 90 + (Math.sin(final_angle * Math::PI / 180) * 20).round,
64
+ 80 + (Math.cos(final_angle * Math::PI / 180) * 10).round
65
+ )
66
+
67
+ @lines << line
68
+
69
+ box = Box.new(
70
+ 100,
71
+ Curses.cols / rays,
72
+ Curses.cols / (rays - 1) * (i - (rays - 1)).abs,
73
+ (Curses.lines / 2) + 31,
74
+ fill_char: 'X',
75
+ x_anchor: :middle,
76
+ y_anchor: :middle
77
+ )
78
+
79
+ box.hide
80
+
81
+ @walls[line] = box
82
+
83
+ line.on_collide do |event|
84
+ box.height = (event.des_distance - event.distance).ceil / 2 + 40
85
+
86
+ box.fill_char = char_color(event.distance, event.des_distance * 0.9)
87
+ box.stroke_char = char_color(event.distance, event.des_distance * 0.9)
88
+
89
+ box.unhide
90
+ end
91
+ end
92
+
93
+ on(:mouse_button) do |event|
94
+ if event.pressed?
95
+ @left_pressed = event.button == :left
96
+ @right_pressed = event.button == :right
97
+ else
98
+ @left_pressed = false
99
+ @right_pressed = false
100
+ end
101
+ end
102
+
103
+ @previous_x = 0
104
+ @previous_y = 0
105
+
106
+ on(:mouse_move) do |event|
107
+ @main_angle += event.x_pos - @previous_x if @left_pressed
108
+
109
+ if @right_pressed
110
+ @lines.each do |line|
111
+ line.x1_pos += event.x_pos - @previous_x
112
+ line.y1_pos += event.y_pos - @previous_y
113
+ end
114
+ end
115
+
116
+ @previous_x = event.x_pos
117
+ @previous_y = event.y_pos
118
+ end
119
+ end
120
+
121
+ def on_update
122
+ @rendered_frames += 1
123
+
124
+ @lines.each_with_index do |line, i|
125
+ final_angle = (i * 1) - 45 - 180 + @main_angle
126
+ line.x2_pos = line.x1_pos + (Math.sin(final_angle * Math::PI / 180) * 200).round
127
+ line.y2_pos = line.y1_pos + (Math.cos(final_angle * Math::PI / 180) * 100).round
128
+ end
129
+ end
130
+
131
+ def after_render
132
+ @walls.values.each { |a| a.hide }
133
+ end
134
+ end
135
+
136
+ Rays.new.start
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/razor'
5
+ require 'debug'
6
+
7
+ class TextEvent < Razor::App
8
+ def on_start
9
+ Razor::Mouse::Utils.enable_xterm_1003
10
+
11
+ @fps_text = Text.new('FPS: x')
12
+ @mouse_text_object = Text.new('', 0, 1)
13
+
14
+ @rendered_frames = 0
15
+
16
+ EM.add_periodic_timer(1) do
17
+ @fps_text.text = "FPS: #{@rendered_frames}"
18
+ @rendered_frames = 0
19
+ end
20
+
21
+ @text = Text.new('Teste agoraaaa', 10, 10)
22
+ @box = Box.new(20, 20, 15, 15)
23
+ @box2 = Box.new(20, 30, 30, 40, fill_char: '-')
24
+
25
+ on(:mouse_button) do |event|
26
+ if event.pressed?
27
+ @pressed = canvas.entity_on(event.x_pos, event.y_pos)
28
+ mouse_text = 'YES <<<<<'
29
+ else
30
+ @pressed = nil
31
+ mouse_text = 'no'
32
+ end
33
+
34
+ @mouse_text_object.text = "Mouse pressed?: #{mouse_text}"
35
+ end
36
+
37
+ @previous_x = 0
38
+ @previous_y = 0
39
+
40
+ on(:mouse_move) do |event|
41
+ if @pressed
42
+ @pressed.x_pos += event.x_pos - @previous_x
43
+ @pressed.y_pos += event.y_pos - @previous_y
44
+ end
45
+
46
+ @previous_x = event.x_pos
47
+ @previous_y = event.y_pos
48
+ end
49
+ end
50
+
51
+ def on_update
52
+ @rendered_frames += 1
53
+ end
54
+ end
55
+
56
+ TextEvent.new.start
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phosphor
4
+ VERSION = "0.1.0"
5
+ end
data/lib/phosphor.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "phosphor/version"
4
+
5
+ module Phosphor
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
data/lib/razor/app.rb ADDED
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ class App
5
+ include Razor::Objects
6
+
7
+ attr_reader :canvas
8
+
9
+ def start
10
+ Razor::App.instance = self
11
+
12
+ Curses.init_screen
13
+ Curses.noecho
14
+ Curses.curs_set(0)
15
+ Curses.cbreak
16
+ # Curses.stdscr.keypad(true)
17
+ Curses.stdscr.nodelay = true
18
+
19
+ @canvas = Razor::Canvas.new(Curses.cols, Curses.lines)
20
+
21
+ EM.run do
22
+ Razor::Events::InputEventReactor.start
23
+
24
+ EM.next_tick do
25
+ on_start
26
+ end
27
+
28
+ EM.add_periodic_timer(1.0 / 1000) do
29
+ update
30
+ render
31
+ after_render
32
+ end
33
+ end
34
+ end
35
+
36
+ def update
37
+ on_update
38
+ end
39
+
40
+ def render
41
+ @canvas.clear
42
+
43
+ game_objects.each do |go|
44
+ next unless go.to_render?
45
+
46
+ go.render
47
+ end
48
+
49
+ @canvas.render
50
+
51
+ Curses.refresh
52
+ end
53
+
54
+ def after_render
55
+ end
56
+
57
+ def stop
58
+ Curses.close_screen
59
+
60
+ Razor::Mouse::Utils.disable_xterm_1003
61
+
62
+ Razor::App.instance = nil
63
+ end
64
+
65
+ def game_objects
66
+ @game_objects ||= []
67
+ end
68
+
69
+ def on(event_name, &block)
70
+ Razor::Events::InputEventReactor.on(event_name, &block)
71
+ end
72
+
73
+ class << self
74
+ attr_accessor :instance
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ class Canvas
5
+ attr_reader :width, :height, :objects_at
6
+
7
+ def initialize(width, height)
8
+ @width = width
9
+ @height = height
10
+ @mutex = Mutex.new
11
+ clear
12
+ end
13
+
14
+ def clear
15
+ @mutex.synchronize do
16
+ @pixels = Array.new(@height) { Array.new(@width, ' ') }
17
+ @objects_at = Array.new(@height) { Array.new(@width, nil) }
18
+ end
19
+ end
20
+
21
+ def was_changed?
22
+ @was_changed
23
+ end
24
+
25
+ def print_at(x_pos, y_pos, string, owner = nil)
26
+ @mutex.synchronize do
27
+ return if y_pos.negative? || y_pos >= @height
28
+
29
+ @was_changed = true
30
+
31
+ string.chars.each_with_index do |ch, i|
32
+ px = x_pos + i
33
+ break if px >= @width
34
+ next if px.negative?
35
+
36
+ @pixels[y_pos][px] = ch
37
+ @objects_at[y_pos][px] = owner
38
+ end
39
+ end
40
+ end
41
+
42
+ def entity_on(x_pos, y_pos)
43
+ (objects_at[y_pos] || {})[x_pos]
44
+ end
45
+
46
+ def render
47
+ @mutex.synchronize do
48
+ return unless was_changed?
49
+
50
+ @was_changed = false
51
+ @pixels.each_with_index do |row, y|
52
+ Curses.setpos(y, 0)
53
+ Curses.addstr(row.join)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Events
5
+ class InputEventReactor
6
+ class << self
7
+ def event_listeners
8
+ @event_listeners ||= {}
9
+ end
10
+
11
+ def start
12
+ @mouse_queue = EM::Queue.new
13
+ @keyboard_queue = EM::Queue.new
14
+
15
+ EM.add_periodic_timer(0.01) do
16
+ loop do
17
+ ch = Curses.get_char
18
+ break if ch.nil? # nada pra ler, sai do loop
19
+
20
+ case ch
21
+ when 'q'
22
+ return stop
23
+ when "\e" # ESC
24
+ case Curses.get_char
25
+ when '['
26
+ csi = ''
27
+ loop do
28
+ d = Curses.get_char
29
+ csi += d
30
+ break if d.ord >= 0x40 && d.ord <= 0x7E
31
+ end
32
+ if /<(\d+);(\d+);(\d+)(m|M)/ =~ csi
33
+ button = Regexp.last_match(1).to_i
34
+ x = Regexp.last_match(2).to_i
35
+ y = Regexp.last_match(3).to_i
36
+ state = Regexp.last_match(4)
37
+
38
+ event = Razor::Events::MouseEvent.new(
39
+ raw_button: button,
40
+ raw_state: state,
41
+ x_pos: x,
42
+ y_pos: y
43
+ )
44
+
45
+ @mouse_queue.push(event)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ start_mouse_events_consumer
53
+ end
54
+
55
+ def on(event_name, &block)
56
+ event_listeners[event_name.to_sym] ||= []
57
+ event_listeners[event_name.to_sym] << block
58
+ end
59
+
60
+ def emit(event)
61
+ @mouse_queue.push(event)
62
+ end
63
+
64
+ private
65
+
66
+ def start_mouse_events_consumer
67
+ @mouse_queue.pop do |event|
68
+ event_listeners[event.name.to_sym]&.each { |b| b.call(event) }
69
+ event_listeners[:mouse]&.each { |b| b.call(event) }
70
+ ensure
71
+ start_mouse_events_consumer
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Events
5
+ class MouseEvent
6
+ attr_reader :kind, :button, :direction, :x_pos, :y_pos
7
+
8
+ def initialize(raw_button:, raw_state:, x_pos:, y_pos:)
9
+ @raw_button = raw_button
10
+ @raw_state = raw_state
11
+ @x_pos = x_pos - 1
12
+ @y_pos = y_pos - 1
13
+
14
+ @mods = {
15
+ shift: (raw_button & 4) != 0,
16
+ meta: (raw_button & 8) != 0,
17
+ ctrl: (raw_button & 16) != 0
18
+ }
19
+
20
+ motion = (raw_button & 32) != 0
21
+ wheel = (raw_button & 64) != 0
22
+
23
+ base = raw_button & 3
24
+
25
+ if wheel
26
+ @kind = :scroll
27
+ @button = :wheel
28
+ @direction = case base
29
+ when 0 then :up
30
+ when 1 then :down
31
+ when 2 then :left
32
+ when 3 then :right
33
+ end
34
+ elsif motion
35
+ @kind = :move
36
+ @button =
37
+ case base
38
+ when 0 then :left
39
+ when 1 then :middle
40
+ when 2 then :right
41
+ when 3 then nil
42
+ end
43
+ else
44
+ @kind = :button
45
+ @button =
46
+ case base
47
+ when 0 then :left
48
+ when 1 then :middle
49
+ when 2 then :right
50
+ when 3 then :release
51
+ end
52
+ end
53
+ end
54
+
55
+ def name
56
+ "mouse_#{@kind}"
57
+ end
58
+
59
+ def pressed?
60
+ @raw_state == 'M'
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Events
5
+ class RayCollisionEvent
6
+ attr_reader :x_pos, :y_pos, :target, :distance, :des_distance
7
+
8
+ def initialize(
9
+ x_pos:,
10
+ y_pos:,
11
+ target:,
12
+ distance:,
13
+ des_distance:
14
+ )
15
+ @x_pos = x_pos - 1
16
+ @y_pos = y_pos - 1
17
+
18
+ @target = target
19
+ @distance = distance
20
+ @des_distance = des_distance
21
+ end
22
+
23
+ def name
24
+ 'ray_collide'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Mouse
5
+ class Utils
6
+ def self.enable_xterm_1003
7
+ $stdout.write("\e[?1003h") # any-event tracking
8
+ $stdout.write("\e[?1006h") # formato SGR, opcional
9
+ $stdout.flush
10
+ end
11
+
12
+ def self.disable_xterm_1003
13
+ $stdout.write("\e[?1003l")
14
+ $stdout.write("\e[?1006l")
15
+ $stdout.flush
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Objects
5
+ class Base
6
+ attr_accessor :app_instance
7
+
8
+ def initialize(*_)
9
+ @app_instance = Razor::App.instance
10
+ @visible = true
11
+
12
+ @app_instance.game_objects << self
13
+ end
14
+
15
+ def render; end
16
+
17
+ def to_render?
18
+ visible?
19
+ end
20
+
21
+ def hidden?
22
+ !@visible
23
+ end
24
+
25
+ def visible?
26
+ @visible
27
+ end
28
+
29
+ def hide
30
+ @visible = false
31
+
32
+ self
33
+ end
34
+
35
+ def unhide
36
+ @visible = true
37
+
38
+ self
39
+ end
40
+
41
+ def on(event_name, &block)
42
+ Razor::Events::InputEventReactor.on(event_name) do |event|
43
+ next unless app_instance.canvas.entity_on(event.y_pos, event.x_pos) == self
44
+
45
+ block.call(event, self)
46
+ end
47
+
48
+ self
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Objects
5
+ class Box < Base
6
+ attr_accessor :x_pos, :y_pos,
7
+ :height, :width,
8
+ :x_anchor, :y_anchor,
9
+ :stroke_char, :fill_char
10
+
11
+ def initialize(
12
+ height,
13
+ width,
14
+ x_pos = 0,
15
+ y_pos = 0,
16
+ x_anchor: :start,
17
+ y_anchor: :start,
18
+ stroke_char: '*',
19
+ fill_char: nil
20
+ )
21
+ @height = height
22
+ @width = width
23
+
24
+ @x_pos = x_pos
25
+ @y_pos = y_pos
26
+
27
+ @x_anchor = x_anchor
28
+ @y_anchor = y_anchor
29
+
30
+ @stroke_char = stroke_char
31
+ @fill_char = fill_char
32
+
33
+ super
34
+ end
35
+
36
+ def anchor_from(anchor, dimension)
37
+ case anchor
38
+ when :start
39
+ 0
40
+ when :middle
41
+ dimension / 2
42
+ when :end
43
+ dimension
44
+ end
45
+ end
46
+
47
+ def x_pos_start = @x_pos - anchor_from(@x_anchor, @width)
48
+ def x_pos_end = x_pos_start + @width
49
+
50
+ def y_pos_start = @y_pos - anchor_from(@x_anchor, @height) / 2
51
+ def y_pos_end = y_pos_start + @height / 2
52
+
53
+ def render
54
+ x_start = x_pos_start
55
+ x_end = x_pos_end
56
+
57
+ y_start = y_pos_start
58
+ y_end = y_pos_end
59
+
60
+ canvas = app_instance.canvas
61
+
62
+ x_start.upto(x_end) do |x_pos|
63
+ canvas.print_at(x_pos, y_start, @stroke_char, self)
64
+ canvas.print_at(x_pos, y_end, @stroke_char, self)
65
+ end
66
+
67
+ y_start.upto(y_end) do |y_pos|
68
+ canvas.print_at(x_start, y_pos, @stroke_char, self)
69
+ canvas.print_at(x_end, y_pos, @stroke_char, self)
70
+ end
71
+
72
+ return unless x_end - x_start >= 3 && y_end - y_start >= 3
73
+ return unless @fill_char
74
+
75
+ (x_start + 1).upto(x_end - 1) do |x_pos|
76
+ (y_start + 1).upto(y_end - 1) do |y_pos|
77
+ canvas.print_at(x_pos, y_pos, @fill_char, self)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Objects
5
+ class Line < Base
6
+ attr_accessor :x1_pos, :y1_pos, :x2_pos, :y2_pos, :stroke_char
7
+
8
+ def initialize(
9
+ x1_pos,
10
+ y1_pos,
11
+ x2_pos,
12
+ y2_pos,
13
+ stroke_char: '*'
14
+ )
15
+ @x1_pos = x1_pos
16
+ @y1_pos = y1_pos
17
+
18
+ @x2_pos = x2_pos
19
+ @y2_pos = y2_pos
20
+
21
+ @stroke_char = stroke_char
22
+
23
+ super
24
+ end
25
+
26
+ def render
27
+ app_instance.canvas.print_at(@x1_pos, @y1_pos, @stroke_char, self)
28
+ app_instance.canvas.print_at(@x2_pos, @y2_pos, @stroke_char, self)
29
+
30
+ plot_line(@x1_pos, @y1_pos, @x2_pos, @y2_pos)
31
+ end
32
+
33
+ def plot_line(x0, y0, x1, y1)
34
+ dx = (x1 - x0).abs
35
+ dy = (y1 - y0).abs
36
+
37
+ sx = x0 < x1 ? 1 : -1
38
+ sy = y0 < y1 ? 1 : -1
39
+
40
+ err = dx - dy
41
+
42
+ loop do
43
+ app_instance.canvas.print_at(x0, y0, @stroke_char, self)
44
+
45
+ break if x0 == x1 && y0 == y1
46
+
47
+ e2 = 2 * err
48
+
49
+ if e2 > -dy
50
+ err -= dy
51
+ x0 += sx
52
+ end
53
+
54
+ if e2 < dx
55
+ err += dx
56
+ y0 += sy
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Objects
5
+ class Ray < Base
6
+ attr_accessor :x1_pos, :y1_pos, :x2_pos, :y2_pos, :stroke_char
7
+
8
+ def initialize(
9
+ x1_pos,
10
+ y1_pos,
11
+ x2_pos,
12
+ y2_pos,
13
+ stroke_char: '*'
14
+ )
15
+ @x1_pos = x1_pos
16
+ @y1_pos = y1_pos
17
+
18
+ @x2_pos = x2_pos
19
+ @y2_pos = y2_pos
20
+
21
+ @stroke_char = stroke_char
22
+
23
+ super
24
+ end
25
+
26
+ def render
27
+ plot_line(@x1_pos, @y1_pos, @x2_pos, @y2_pos)
28
+ end
29
+
30
+ def collision_blocks
31
+ @collision_blocks ||= []
32
+ end
33
+
34
+ def on_collide(&block)
35
+ collision_blocks << block
36
+ end
37
+
38
+ def plot_line(x0, y0, x1, y1)
39
+ sx0 = x0
40
+ sy0 = y0
41
+
42
+ dx = (x1 - x0).abs
43
+ dy = (y1 - y0).abs
44
+
45
+ sx = x0 < x1 ? 1 : -1
46
+ sy = y0 < y1 ? 1 : -1
47
+
48
+ err = dx - dy
49
+
50
+ loop do
51
+ ddx = (sx0 - x1).abs
52
+ ddy = (sy0 - y1).abs
53
+
54
+ rdx = (sx0 - x0).abs
55
+ rdy = (sy0 - y0).abs
56
+
57
+ des_distance = Math.sqrt(ddx**2.0 + ddy**2.0)
58
+ distance = Math.sqrt(rdx**2.0 + rdy**2.0)
59
+
60
+ target = app_instance.canvas.entity_on(x0, y0)
61
+
62
+ if target && target.class != self.class
63
+ event = Razor::Events::RayCollisionEvent.new(
64
+ x_pos: x0,
65
+ y_pos: y0,
66
+ target:,
67
+ distance:,
68
+ des_distance:
69
+ )
70
+
71
+ collision_blocks.each { |b| b.call(event, self) }
72
+
73
+ break
74
+ end
75
+
76
+ app_instance.canvas.print_at(x0, y0, @stroke_char, self)
77
+
78
+ break if x0 == x1 && y0 == y1
79
+
80
+ e2 = 2 * err
81
+
82
+ if e2 > -dy
83
+ err -= dy
84
+ x0 += sx
85
+ end
86
+
87
+ if e2 < dx
88
+ err += dx
89
+ y0 += sy
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Razor
4
+ module Objects
5
+ class Text < Base
6
+ attr_accessor :text, :x_pos, :y_pos
7
+
8
+ def initialize(text = '', x_pos = 0, y_pos = 0)
9
+ @text = text
10
+ @x_pos = x_pos
11
+ @y_pos = y_pos
12
+
13
+ super
14
+ end
15
+
16
+ def render
17
+ app_instance.canvas.print_at(@x_pos, @y_pos, @text, self)
18
+ end
19
+ end
20
+ end
21
+ end
data/lib/razor.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'curses'
4
+ require 'eventmachine'
5
+
6
+ require_relative 'razor/events/mouse_event'
7
+ require_relative 'razor/events/input_event_reactor'
8
+ require_relative 'razor/events/ray_collision_event'
9
+
10
+ require_relative 'razor/mouse/utils'
11
+
12
+ require_relative 'razor/objects/base'
13
+ require_relative 'razor/objects/text'
14
+ require_relative 'razor/objects/line'
15
+ require_relative 'razor/objects/box'
16
+ require_relative 'razor/objects/ray'
17
+
18
+ require_relative 'razor/canvas'
19
+
20
+ require_relative 'razor/app'
21
+
22
+ module Razor
23
+ class << self
24
+ def start
25
+ init_screen
26
+ end
27
+
28
+ def hide_cursor
29
+ curs_set(0)
30
+ end
31
+
32
+ def show_cursor
33
+ curs_set(1)
34
+ end
35
+ end
36
+ end
data/phosphor.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/phosphor/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "phosphor"
7
+ spec.version = Phosphor::VERSION
8
+ spec.authors = ["albertalef"]
9
+ spec.email = ["albertalef@protonmail.com"]
10
+
11
+ spec.summary = "Terminal Game Engine"
12
+ spec.description = "The best ruby gem"
13
+ spec.homepage = "https://github.com/albertalef/phosphor"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = spec.homepage
22
+ spec.metadata["rubygems_mfa_required"] = "true"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(__dir__) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (File.expand_path(f) == __FILE__) ||
29
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
30
+ end
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ # Uncomment to register a new dependency of your gem
37
+ # spec.add_dependency "example-gem", "~> 1.0"
38
+
39
+ # For more information and examples about making a new gem, check out our
40
+ # guide at: https://bundler.io/guides/creating_gem.html
41
+ end
data/sig/phosphor.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Phosphor
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phosphor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - albertalef
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-12-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The best ruby gem
14
+ email:
15
+ - albertalef@protonmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - examples/line_plotter.rb
27
+ - examples/mouse_tracker.rb
28
+ - examples/rays.rb
29
+ - examples/text_event.rb
30
+ - lib/phosphor.rb
31
+ - lib/phosphor/version.rb
32
+ - lib/razor.rb
33
+ - lib/razor/app.rb
34
+ - lib/razor/canvas.rb
35
+ - lib/razor/events/input_event_reactor.rb
36
+ - lib/razor/events/mouse_event.rb
37
+ - lib/razor/events/ray_collision_event.rb
38
+ - lib/razor/mouse/utils.rb
39
+ - lib/razor/objects/base.rb
40
+ - lib/razor/objects/box.rb
41
+ - lib/razor/objects/line.rb
42
+ - lib/razor/objects/ray.rb
43
+ - lib/razor/objects/text.rb
44
+ - phosphor.gemspec
45
+ - sig/phosphor.rbs
46
+ homepage: https://github.com/albertalef/phosphor
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ allowed_push_host: https://rubygems.org
51
+ homepage_uri: https://github.com/albertalef/phosphor
52
+ source_code_uri: https://github.com/albertalef/phosphor
53
+ changelog_uri: https://github.com/albertalef/phosphor
54
+ rubygems_mfa_required: 'true'
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.6.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.4.19
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Terminal Game Engine
74
+ test_files: []