diff options
| author | realtradam <[email protected]> | 2021-08-07 14:06:57 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-08-07 14:06:57 -0400 |
| commit | 548bd9fc01cbad4ae4b65c1e9f2a542c95a3822c (patch) | |
| tree | b035792863ac46e768760e00fce10424c68b48dc | |
| parent | 691b1e5752141294dea7f30d4f468733dac153e3 (diff) | |
| download | ruby2d-camera-548bd9fc01cbad4ae4b65c1e9f2a542c95a3822c.tar.gz ruby2d-camera-548bd9fc01cbad4ae4b65c1e9f2a542c95a3822c.zip | |
initial test
| -rw-r--r-- | .gitignore | 8 | ||||
| -rw-r--r-- | .rubocop.yml | 13 | ||||
| -rw-r--r-- | CHANGELOG.md | 5 | ||||
| -rw-r--r-- | Gemfile | 11 | ||||
| -rw-r--r-- | LICENSE.txt | 21 | ||||
| -rw-r--r-- | Rakefile | 8 | ||||
| -rwxr-xr-x | bin/console | 15 | ||||
| -rwxr-xr-x | bin/setup | 8 | ||||
| -rw-r--r-- | lib/ruby2d/camera.rb | 132 | ||||
| -rw-r--r-- | lib/ruby2d/camera/triangle.rb | 102 | ||||
| -rw-r--r-- | lib/ruby2d/camera/version.rb | 7 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/circle_wrapper.rb | 47 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/image_wrapper.rb | 73 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/line_wrapper.rb | 91 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/quad_wrapper.rb | 108 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/rectangle_wrapper.rb | 138 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/sprite_wrapper.rb | 86 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/square_wrapper.rb | 146 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/text_wrapper.rb | 84 | ||||
| -rw-r--r-- | lib/ruby2d/camera/wrappers/triangle_wrapper.rb | 90 | ||||
| -rw-r--r-- | ruby2d-camera-0.1.0.gem | bin | 0 -> 1452032 bytes | |||
| -rw-r--r-- | ruby2d-camera.gemspec | 38 | ||||
| -rw-r--r-- | test.rb | 10 |
23 files changed, 1239 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9106b2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..bfef2d0 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,13 @@ +AllCops: + TargetRubyVersion: 2.4 + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes + +Style/StringLiteralsInInterpolation: + Enabled: true + EnforcedStyle: double_quotes + +Layout/LineLength: + Max: 120 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..29886ab --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## [Unreleased] + +## [0.1.0] - 2021-08-07 + +- Initial release @@ -1,3 +1,10 @@ -ruby '2.7.2' +# frozen_string_literal: true -gem 'ruby2d', '3.1.4' +source "https://rubygems.org" + +# Specify your gem's dependencies in ruby2d-camera.gemspec +gemspec + +gem "rake", "~> 13.0" + +gem "rubocop", "~> 1.7" diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..82abdd9 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 realtradam + +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: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +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. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..1924143 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "rubocop/rake_task" + +RuboCop::RakeTask.new + +task default: :rubocop diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..f814e79 --- /dev/null +++ b/bin/console @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "ruby2d/camera" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/lib/ruby2d/camera.rb b/lib/ruby2d/camera.rb new file mode 100644 index 0000000..c8f1667 --- /dev/null +++ b/lib/ruby2d/camera.rb @@ -0,0 +1,132 @@ +# frozen_string_literal: true + +require_relative "camera/version" +require_relative "camera/triangle" + +#Dir[File.join(__dir__, 'camera', '*.rb')].sort.each { |file| require file } + +# Handles rendering objects relative +# to a camera location +module Ruby2D + module Camera + class <<self + + private + # Contains all objects that are tracked + def objects + @objects ||= [] + end + end + + def self.debug_x + @debug_x ||= 0 + end + + def self.debug_x=(debug_x) + @debug_x = debug_x + end + + def self.debug_y + @debug_y ||= 0 + end + + def self.debug_y=(debug_y) + @debug_y = debug_y + end + + # Adding objects so they are + # tracked by the Camera + def self.<<(item) +=begin + case item + when Triangle + item.extend Camera::TriangleWrapped + when Square + item.extend Camera::SquareWrapped + when Rectangle + item.extend Camera::RectangleWrapped + when Quad + item.extend Camera::QuadWrapped + when Line + item.extend Camera::LineWrapped + when Circle + item.extend Camera::CircleWrapped + when Image + item.extend Camera::ImageWrapped + when Sprite + item.extend Camera::SpriteWrapped + when Text + item.extend Camera::TextWrapped + else + puts 'Warning: Non-standard Object added to Camera' + puts ' Object may not behave as expected' + end +=end + objects.push(item) unless objects.include?(item) + end + + def self.remove(item) + objects.delete(item) if objects.include?(item) + end + + # Redraw all objects that + # are tracked by the Camera + def self.redraw(auto_purge: false) + #if auto_purge + # objects.each do |item| + # if item.nil? + # puts "Warning: Nil Object detected in Camera" + # puts " Nil Object removed" + # objects.delete(obj) + # else + # item.redraw + # end + # end + #else + objects.each(&:_draw) + #end + end + + # Variables changing Camera properties + def self._x(x) + @x += x + end + + def self._y(y) + @y += y + end + + def self.x + @x ||= 0 + end + + def self.x=(x) + @x = x + end + + def self.y + @y ||= 0 + end + + def self.y=(y) + @y = y + end + + def self.zoom + @zoom ||= 1.0 + end + + def self.zoom=(zoom) + @zoom = zoom + end + + def self.angle + @angle ||= 0 + end + + def self.angle=(angle) + angle %= 360 + @angle = angle + end + end +end diff --git a/lib/ruby2d/camera/triangle.rb b/lib/ruby2d/camera/triangle.rb new file mode 100644 index 0000000..cac74b8 --- /dev/null +++ b/lib/ruby2d/camera/triangle.rb @@ -0,0 +1,102 @@ +# frozen_string_literal: true + +module Ruby2D + module Camera + # Wraps existing variables as well as adding new methods + # so that it can be handled by the Ruby2D::Camera Module + class Triangle << Ruby2D::Triangle + # Recalculates real coordiantes + # Use after changing variables + def _draw + angle = Ruby2D::Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + temp_x1 = (((x + @x1 - Ruby2D::Camera.x) * Math.cos(angle)) - ((y + @y1 - Ruby2D::Camera.y) * Math.sin(angle))) * Ruby2D::Camera.zoom + half_width + temp_y1 = (((x + @x1 - Ruby2D::Camera.x) * Math.sin(angle)) + ((y + @y1 - Ruby2D::Camera.y) * Math.cos(angle))) * Ruby2D::Camera.zoom + half_height + temp_x2 = (((x + @x2 - Ruby2D::Camera.x) * Math.cos(angle)) - ((y + @y2 - Ruby2D::Camera.y) * Math.sin(angle))) * Ruby2D::Camera.zoom + half_width + temp_y2 = (((x + @x2 - Ruby2D::Camera.x) * Math.sin(angle)) + ((y + @y2 - Ruby2D::Camera.y) * Math.cos(angle))) * Ruby2D::Camera.zoom + half_height + temp_x3 = (((x + @x3 - Ruby2D::Camera.x) * Math.cos(angle)) - ((y + @y3 - Ruby2D::Camera.y) * Math.sin(angle))) * Ruby2D::Camera.zoom + half_width + temp_y3 = (((x + @x3 - Ruby2D::Camera.x) * Math.sin(angle)) + ((y + @y3 - Ruby2D::Camera.y) * Math.cos(angle))) * Ruby2D::Camera.zoom + half_height + self.draw(x1: temp_x1, y1: temp_y1, + x2: temp_x2, y2: temp_y2, + x3: temp_x3, y3: temp_y3) + end + + def initialize(opts= {}) + #add to camera? + super(opts) + Ruby2D::Camera << self + end + + #Methods for moving the shape + def x + @x ||= 0 + end + + def x=(x) + @x = x + end + + def y + @y ||= 0 + end + + def y=(y) + @y = y + end +=begin + # Wrappers for the coordinates so that + # they are handled by the Ruby2D::Camera instead + # and for ease of programming + def x1 + @virtual_x1 ||= @x1 + end + + def x1=(x1) + @virtual_x1 = x1 + end + + def y1 + @virtual_y1 ||= @y1 + end + + def y1=(y1) + @virtual_y1 = y1 + end + + def x2 + @virtual_x2 ||= @x2 + end + + def x2=(x2) + @virtual_x2 = x2 + end + + def y2 + @virtual_y2 ||= @y2 + end + + def y2=(y2) + @virtual_y2 = y2 + end + + def x3 + @virtual_x3 ||= @x3 + end + + def x3=(x3) + @virtual_x3 = x3 + end + + def y3 + @virtual_y3 ||= @y3 + end + + def y3=(y3) + @virtual_y3 = y3 + end +=end + end + end +end + diff --git a/lib/ruby2d/camera/version.rb b/lib/ruby2d/camera/version.rb new file mode 100644 index 0000000..a78e2f2 --- /dev/null +++ b/lib/ruby2d/camera/version.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Ruby2d + module Camera + VERSION = "0.1.0" + end +end diff --git a/lib/ruby2d/camera/wrappers/circle_wrapper.rb b/lib/ruby2d/camera/wrappers/circle_wrapper.rb new file mode 100644 index 0000000..a7db05a --- /dev/null +++ b/lib/ruby2d/camera/wrappers/circle_wrapper.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Camera +# Wraps existing variables as well as adding new methods +# so that it can be handled by the Camera Module + module CircleWrapped + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + @radius = radius * Camera.zoom + @x = (((x - Camera.x + radius) * Math.cos(angle)) - ((y - Camera.y + radius) * Math.sin(angle))) * Camera.zoom + half_width + @y = (((x - Camera.x + radius) * Math.sin(angle)) + ((y - Camera.y + radius) * Math.cos(angle))) * Camera.zoom + half_height + end + + # Methods for moving the shape as well as + # wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x + @virtual_x ||= @x + end + + def x=(x) + @virtual_x = x + end + + def y + @virtual_y ||= @y + end + + def y=(y) + @virtual_y = y + end + + def radius + @virtual_radius ||= @radius + end + + def radius=(radius) + @virtual_radius = radius + end + end +end + diff --git a/lib/ruby2d/camera/wrappers/image_wrapper.rb b/lib/ruby2d/camera/wrappers/image_wrapper.rb new file mode 100644 index 0000000..9e4b163 --- /dev/null +++ b/lib/ruby2d/camera/wrappers/image_wrapper.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +module Camera + # Wraps existing variables as well as adding new methods + # so that it can be handled by the Camera Module + module ImageWrapped + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + offset_x = x + (width / 2) + offset_y = y + (height / 2) + @x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \ + * Camera.zoom + half_width - (width * Camera.zoom / 2) + @y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \ + * Camera.zoom + half_height - (height * Camera.zoom / 2) + @rotate = rotate + Camera.angle + @width = width * Camera.zoom + @height = height * Camera.zoom + end + + #Methods for moving the shape + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x + @virtual_x ||= @x + end + + def x=(x) + @virtual_x = x + end + + def y + @virtual_y ||= @y + end + + def y=(y) + @virtual_y = y + end + + def x + @virtual_x ||= @x + end + + def rotate + @virtual_rotate ||= @rotate + end + + def rotate=(rotate) + @virtual_rotate = rotate + end + + def width + @virtual_width ||= @width + end + + def width=(width) + @virtual_width = width + end + + def height + @virtual_height ||= @height + end + + def height=(height) + @virtual_height = height + end + end +end diff --git a/lib/ruby2d/camera/wrappers/line_wrapper.rb b/lib/ruby2d/camera/wrappers/line_wrapper.rb new file mode 100644 index 0000000..7d4d944 --- /dev/null +++ b/lib/ruby2d/camera/wrappers/line_wrapper.rb @@ -0,0 +1,91 @@ +# frozen_string_literal: true + +module Camera +# Wraps existing variables as well as adding new methods +# so that it can be handled by the Camera Module + module LineWrapped + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + @x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @width = width * Camera.zoom + end + + #Methods for moving the shape + def x + @x ||= 0 + end + + def x=(x) + @x = x + end + + def y + @y ||= 0 + end + + def y=(y) + @y = y + end + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x1 + @virtual_x1 ||= @x1 + end + + def x1=(x1) + @virtual_x1 = x1 + end + + def y1 + @virtual_y1 ||= @y1 + end + + def y1=(y1) + @virtual_y1 = y1 + end + + def x2 + @virtual_x2 ||= @x2 + end + + def x2=(x2) + @virtual_x2 = x2 + end + + def y2 + @virtual_y2 ||= @y2 + end + + def y2=(y2) + @virtual_y2 = y2 + end + + def width + @virtual_width ||= @width + end + + def width=(width) + @virtual_width = width + end + + def length + points_distance(x1, y1, x2, y2) + end + + def contains?(x, y) + points_distance(x1, y1, x, y) <= length && + points_distance(x2, y2, x, y) <= length && + (((y2 - y1) * x - (x2 - x1) * y + x2 * y1 - y2 * x1).abs / length) <= 0.5 * width + end + end +end + diff --git a/lib/ruby2d/camera/wrappers/quad_wrapper.rb b/lib/ruby2d/camera/wrappers/quad_wrapper.rb new file mode 100644 index 0000000..477dbfd --- /dev/null +++ b/lib/ruby2d/camera/wrappers/quad_wrapper.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +module Camera +# Wraps existing variables as well as adding new methods +# so that it can be handled by the Camera Module + module QuadWrapped + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + @x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x3 = (((x + x3 - Camera.x) * Math.cos(angle)) - ((y + y3 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x4 = (((x + x4 - Camera.x) * Math.cos(angle)) - ((y + y4 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y4 = (((x + x4 - Camera.x) * Math.sin(angle)) + ((y + y4 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + end + + #Methods for moving the shape + def x + @x ||= 0 + end + + def x=(x) + @x = x + end + + def y + @y ||= 0 + end + + def y=(y) + @y = y + end + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x1 + @virtual_x1 ||= @x1 + end + + def x1=(x1) + @virtual_x1 = x1 + end + + def y1 + @virtual_y1 ||= @y1 + end + + def y1=(y1) + @virtual_y1 = y1 + end + + def x2 + @virtual_x2 ||= @x2 + end + + def x2=(x2) + @virtual_x2 = x2 + end + + def y2 + @virtual_y2 ||= @y2 + end + + def y2=(y2) + @virtual_y2 = y2 + end + + def x3 + @virtual_x3 ||= @x3 + end + + def x3=(x3) + @virtual_x3 = x3 + end + + def y3 + @virtual_y3 ||= @y3 + end + + def y3=(y3) + @virtual_y3 = y3 + end + + def x4 + @virtual_x4 ||= @x4 + end + + def x4=(x4) + @virtual_x4 = x4 + end + + def y4 + @virtual_y4 ||= @y4 + end + + def y4=(y4) + @virtual_y4 = y4 + end + end +end + diff --git a/lib/ruby2d/camera/wrappers/rectangle_wrapper.rb b/lib/ruby2d/camera/wrappers/rectangle_wrapper.rb new file mode 100644 index 0000000..93e888a --- /dev/null +++ b/lib/ruby2d/camera/wrappers/rectangle_wrapper.rb @@ -0,0 +1,138 @@ +# frozen_string_literal: true + +module Camera +# Wraps existing variables as well as adding new methods +# so that it can be handled by the Camera Module + module RectangleWrapped + # Rectangles are part of the exception where + # their x and y variables need to be reset + # when wrapped + def self.extended(obj) + obj.instance_exec do + @x = 0 + @y = 0 + end + end + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + @x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x3 = (((x + x3 - Camera.x) * Math.cos(angle)) - ((y + y3 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x4 = (((x + x4 - Camera.x) * Math.cos(angle)) - ((y + y4 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y4 = (((x + x4 - Camera.x) * Math.sin(angle)) + ((y + y4 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + end + + #Methods for moving the shape + def x + @x ||= 0 + end + + def x=(x) + @x = x + end + + def y + @y ||= 0 + end + + def y=(y) + @y = y + end + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x1 + @virtual_x1 ||= @x1 + end + + def x1=(x1) + @virtual_x1 = x1 + end + + def y1 + @virtual_y1 ||= @y1 + end + + def y1=(y1) + @virtual_y1 = y1 + end + + def x2 + @virtual_x2 ||= @x2 + end + + def x2=(x2) + @virtual_x2 = x2 + end + + def y2 + @virtual_y2 ||= @y2 + end + + def y2=(y2) + @virtual_y2 = y2 + end + + def x3 + @virtual_x3 ||= @x3 + end + + def x3=(x3) + @virtual_x3 = x3 + end + + def y3 + @virtual_y3 ||= @y3 + end + + def y3=(y3) + @virtual_y3 = y3 + end + + def x4 + @virtual_x4 ||= @x4 + end + + def x4=(x4) + @virtual_x4 = x4 + end + + def y4 + @virtual_y4 ||= @y4 + end + + def y4=(y4) + @virtual_y4 = y4 + end + + + def width + @width ||= 200 + end + + def width=(width) + self.x2 = width + self.x3 = width + @width = width + end + + def height + @height ||= 100 + end + + def height=(height) + self.y3 = height + self.y4 = height + @height = height + end + end +end + diff --git a/lib/ruby2d/camera/wrappers/sprite_wrapper.rb b/lib/ruby2d/camera/wrappers/sprite_wrapper.rb new file mode 100644 index 0000000..d38c0bc --- /dev/null +++ b/lib/ruby2d/camera/wrappers/sprite_wrapper.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +module Camera + # Wraps existing variables as well as adding new methods + # so that it can be handled by the Camera Module + module SpriteWrapped + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + offset_x = x + (width / 2) + offset_y = y + (height / 2) + @x = @flip_x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \ + * Camera.zoom + half_width - (width * Camera.zoom / 2) + @y = @flip_y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \ + * Camera.zoom + half_height - (height * Camera.zoom / 2) + @rotate = rotate + Camera.angle + @width = @flip_width = width * Camera.zoom + @height = @flip_height = height * Camera.zoom + case @flip + when :both + @flip_x = @x + @height + @flip_width = -@width + @flip_y = @y + @width + @flip_height = -@height + when :horizontal + @flip_y = @y + @width + @flip_height = -@height + when :vertical + @flip_x = @x + @height + @flip_width = -@width + end + end + + #Methods for moving the shape + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x + @virtual_x ||= @x + end + + def x=(x) + @virtual_x = x + end + + def y + @virtual_y ||= @y + end + + def y=(y) + @virtual_y = y + end + + def x + @virtual_x ||= @x + end + + def rotate + @virtual_rotate ||= @rotate + end + + def rotate=(rotate) + @virtual_rotate = rotate + end + + def width + @virtual_width ||= @width + end + + def width=(width) + @virtual_width = width + end + + def height + @virtual_height ||= @height + end + + def height=(height) + @virtual_height = height + end + end +end diff --git a/lib/ruby2d/camera/wrappers/square_wrapper.rb b/lib/ruby2d/camera/wrappers/square_wrapper.rb new file mode 100644 index 0000000..2215a30 --- /dev/null +++ b/lib/ruby2d/camera/wrappers/square_wrapper.rb @@ -0,0 +1,146 @@ +# frozen_string_literal: true + +module Camera + # Wraps existing variables as well as adding new methods + # so that it can be handled by the Camera Module + module SquareWrapped + # Squares are part of the exception where + # their x and y variables need to be reset + # when wrapped + def self.extended(obj) + obj.instance_exec do + @x = 0 + @y = 0 + end + end + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + @x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x3 = (((x + x3 - Camera.x) * Math.cos(angle)) - ((y + y3 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x4 = (((x + x4 - Camera.x) * Math.cos(angle)) - ((y + y4 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y4 = (((x + x4 - Camera.x) * Math.sin(angle)) + ((y + y4 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + end + + #Methods for moving the shape + def x + @x ||= 0 + end + + def x=(x) + @x = x + end + + def y + @y ||= 0 + end + + def y=(y) + @y = y + end + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x1 + @virtual_x1 ||= @x1 + end + + def x1=(x1) + @virtual_x1 = x1 + end + + def y1 + @virtual_y1 ||= @y1 + end + + def y1=(y1) + @virtual_y1 = y1 + end + + def x2 + @virtual_x2 ||= @x2 + end + + def x2=(x2) + @virtual_x2 = x2 + end + + def y2 + @virtual_y2 ||= @y2 + end + + def y2=(y2) + @virtual_y2 = y2 + end + + def x3 + @virtual_x3 ||= @x3 + end + + def x3=(x3) + @virtual_x3 = x3 + end + + def y3 + @virtual_y3 ||= @y3 + end + + def y3=(y3) + @virtual_y3 = y3 + end + + def x4 + @virtual_x4 ||= @x4 + end + + def x4=(x4) + @virtual_x4 = x4 + end + + def y4 + @virtual_y4 ||= @y4 + end + + def y4=(y4) + @virtual_y4 = y4 + end + + + def width + @width ||= 200 + end + + def width=(width) + self.x2 = width + self.x3 = width + @width = width + end + + def height + @height ||= 100 + end + + def height=(height) + self.y3 = height + self.y4 = height + @height = height + end + + def size + @size ||= 100 + end + + def size=(size) + @size = self.width = self.height = size + end + end +end + diff --git a/lib/ruby2d/camera/wrappers/text_wrapper.rb b/lib/ruby2d/camera/wrappers/text_wrapper.rb new file mode 100644 index 0000000..ad96881 --- /dev/null +++ b/lib/ruby2d/camera/wrappers/text_wrapper.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +module Camera + # Wraps existing variables as well as adding new methods + # so that it can be handled by the Camera Module + # TODO: note that text could not be resized at the current iteration + # of Ruby2D so the math needs to be changed compensate for this. + # When Ruby2D gets updated to allow text resizing the math will need + # to be corrected again(see image_wrapper.rb for reference, that has + # math that allows for resizing) + module TextWrapped + @center = false + + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + if center + offset_y = y + (Camera.zoom / 2) + offset_x = x + (Camera.zoom / 2) + else + offset_x = x + (width / Camera.zoom / 2) + offset_y = y + (height / Camera.zoom / 2) + end + @x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \ + * Camera.zoom + half_width - (width / 2) + @y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \ + * Camera.zoom + half_height - (height / 2) + @rotate = rotate + Camera.angle + @size = size * Camera.zoom + end + + #Methods for moving the shape + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x + @virtual_x ||= @x + end + + def x=(x) + @virtual_x = x + end + + def y + @virtual_y ||= @y + end + + def y=(y) + @virtual_y = y + end + + def x + @virtual_x ||= @x + end + + def rotate + @virtual_rotate ||= @rotate + end + + def rotate=(rotate) + @virtual_rotate = rotate + end + + def size + @virtual_size ||= @size + end + + def size=(size) + @virtual_size = size + end + + def center + @center + end + + def center=(center) + @center = center + end + end +end diff --git a/lib/ruby2d/camera/wrappers/triangle_wrapper.rb b/lib/ruby2d/camera/wrappers/triangle_wrapper.rb new file mode 100644 index 0000000..0aba2c0 --- /dev/null +++ b/lib/ruby2d/camera/wrappers/triangle_wrapper.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +module Camera +# Wraps existing variables as well as adding new methods +# so that it can be handled by the Camera Module + module TriangleWrapped + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + @x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x3 = (((x + x3 - Camera.x) * Math.cos(angle)) - ((y + y3 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + end + + #Methods for moving the shape + def x + @x ||= 0 + end + + def x=(x) + @x = x + end + + def y + @y ||= 0 + end + + def y=(y) + @y = y + end + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x1 + @virtual_x1 ||= @x1 + end + + def x1=(x1) + @virtual_x1 = x1 + end + + def y1 + @virtual_y1 ||= @y1 + end + + def y1=(y1) + @virtual_y1 = y1 + end + + def x2 + @virtual_x2 ||= @x2 + end + + def x2=(x2) + @virtual_x2 = x2 + end + + def y2 + @virtual_y2 ||= @y2 + end + + def y2=(y2) + @virtual_y2 = y2 + end + + def x3 + @virtual_x3 ||= @x3 + end + + def x3=(x3) + @virtual_x3 = x3 + end + + def y3 + @virtual_y3 ||= @y3 + end + + def y3=(y3) + @virtual_y3 = y3 + end + end +end + diff --git a/ruby2d-camera-0.1.0.gem b/ruby2d-camera-0.1.0.gem Binary files differnew file mode 100644 index 0000000..3517cdf --- /dev/null +++ b/ruby2d-camera-0.1.0.gem diff --git a/ruby2d-camera.gemspec b/ruby2d-camera.gemspec new file mode 100644 index 0000000..507e8c0 --- /dev/null +++ b/ruby2d-camera.gemspec @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require_relative "lib/ruby2d/camera/version" + +Gem::Specification.new do |spec| + spec.name = "ruby2d-camera" + spec.version = Ruby2d::Camera::VERSION + spec.authors = ["Tradam"] + spec.email = ["[email protected]"] + + spec.summary = "A library for camera movement in the Ruby2D gem" + #spec.description = "TODO: Write a longer description or delete this line." + spec.homepage = "https://github.com/realtradam/ruby2d-camera" + spec.license = "MIT" + spec.required_ruby_version = ">= 2.4.0" + + spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'" + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = "https://github.com/realtradam/ruby2d-camera" + #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." + + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + spec.files = Dir.chdir(File.expand_path(__dir__)) do + `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) } + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + # Uncomment to register a new dependency of your gem + # spec.add_dependency "example-gem", "~> 1.0" + spec.add_dependency "ruby2d", "~> 0.10" + + # For more information and examples about making a new gem, checkout our + # guide at: https://bundler.io/guides/creating_gem.html +end @@ -0,0 +1,10 @@ +require 'ruby2d' +require 'ruby2d/camera' + +@tri = Ruby2D::Camera::Triangle.new + +update do + Ruby2D::Camera.redraw +end + +show |
