From 5f46f1f975bd621fbca9c7e173de78e83ad70e3a Mon Sep 17 00:00:00 2001 From: tradam Date: Mon, 27 Sep 2021 08:39:23 -0400 Subject: partially complete fixing code, found a strange bug with ruby2d --- example/README.mdown | 3 - example/assets/background.png | Bin 60142 -> 0 bytes example/assets/blobcoolthink.png | Bin 13066 -> 0 bytes example/assets/blobshadow.png | Bin 5679 -> 0 bytes example/assets/bricktexture.png | Bin 741798 -> 0 bytes example/assets/legacy_player.png | Bin 1044 -> 0 bytes example/assets/sprites/alienpls-56.png | Bin 255710 -> 0 bytes example/assets/sprites/blobdance-128.png | Bin 35825 -> 0 bytes example/assets/sprites/dance2-112.png | Bin 214857 -> 0 bytes example/assets/sprites/dancer-128.png | Bin 39270 -> 0 bytes example/assets/sprites/mainblob-128.png | Bin 94731 -> 0 bytes example/house.rb | 100 ------ example/old_example/README.mdown | 3 + example/old_example/assets/background.png | Bin 0 -> 60142 bytes example/old_example/assets/blobcoolthink.png | Bin 0 -> 13066 bytes example/old_example/assets/blobshadow.png | Bin 0 -> 5679 bytes example/old_example/assets/bricktexture.png | Bin 0 -> 741798 bytes example/old_example/assets/legacy_player.png | Bin 0 -> 1044 bytes example/old_example/assets/sprites/alienpls-56.png | Bin 0 -> 255710 bytes .../old_example/assets/sprites/blobdance-128.png | Bin 0 -> 35825 bytes example/old_example/assets/sprites/dance2-112.png | Bin 0 -> 214857 bytes example/old_example/assets/sprites/dancer-128.png | Bin 0 -> 39270 bytes .../old_example/assets/sprites/mainblob-128.png | Bin 0 -> 94731 bytes example/old_example/house.rb | 100 ++++++ example/old_example/room.rb | 129 +++++++ example/old_example/run.rb | 223 ++++++++++++ example/room.rb | 129 ------- example/run.rb | 223 ------------ example/test/assets/alienpls-56.png | Bin 0 -> 255710 bytes example/test/assets/bricktexture-600.png | Bin 0 -> 741798 bytes example/test/run.rb | 378 +++++++++++++++++++++ 31 files changed, 833 insertions(+), 455 deletions(-) delete mode 100644 example/README.mdown delete mode 100644 example/assets/background.png delete mode 100644 example/assets/blobcoolthink.png delete mode 100644 example/assets/blobshadow.png delete mode 100644 example/assets/bricktexture.png delete mode 100644 example/assets/legacy_player.png delete mode 100644 example/assets/sprites/alienpls-56.png delete mode 100644 example/assets/sprites/blobdance-128.png delete mode 100644 example/assets/sprites/dance2-112.png delete mode 100644 example/assets/sprites/dancer-128.png delete mode 100644 example/assets/sprites/mainblob-128.png delete mode 100644 example/house.rb create mode 100644 example/old_example/README.mdown create mode 100644 example/old_example/assets/background.png create mode 100644 example/old_example/assets/blobcoolthink.png create mode 100644 example/old_example/assets/blobshadow.png create mode 100644 example/old_example/assets/bricktexture.png create mode 100644 example/old_example/assets/legacy_player.png create mode 100644 example/old_example/assets/sprites/alienpls-56.png create mode 100644 example/old_example/assets/sprites/blobdance-128.png create mode 100644 example/old_example/assets/sprites/dance2-112.png create mode 100644 example/old_example/assets/sprites/dancer-128.png create mode 100644 example/old_example/assets/sprites/mainblob-128.png create mode 100644 example/old_example/house.rb create mode 100644 example/old_example/room.rb create mode 100644 example/old_example/run.rb delete mode 100644 example/room.rb delete mode 100644 example/run.rb create mode 100644 example/test/assets/alienpls-56.png create mode 100644 example/test/assets/bricktexture-600.png create mode 100644 example/test/run.rb (limited to 'example') diff --git a/example/README.mdown b/example/README.mdown deleted file mode 100644 index 186f6e3..0000000 --- a/example/README.mdown +++ /dev/null @@ -1,3 +0,0 @@ -### Example Is Currently Broken - -Most likely an issue with the example code, but is too spaghetti for me to want to revisit it diff --git a/example/assets/background.png b/example/assets/background.png deleted file mode 100644 index f411e96..0000000 Binary files a/example/assets/background.png and /dev/null differ diff --git a/example/assets/blobcoolthink.png b/example/assets/blobcoolthink.png deleted file mode 100644 index 7cdfcc8..0000000 Binary files a/example/assets/blobcoolthink.png and /dev/null differ diff --git a/example/assets/blobshadow.png b/example/assets/blobshadow.png deleted file mode 100644 index ff981cb..0000000 Binary files a/example/assets/blobshadow.png and /dev/null differ diff --git a/example/assets/bricktexture.png b/example/assets/bricktexture.png deleted file mode 100644 index 4ef86e0..0000000 Binary files a/example/assets/bricktexture.png and /dev/null differ diff --git a/example/assets/legacy_player.png b/example/assets/legacy_player.png deleted file mode 100644 index 485e972..0000000 Binary files a/example/assets/legacy_player.png and /dev/null differ diff --git a/example/assets/sprites/alienpls-56.png b/example/assets/sprites/alienpls-56.png deleted file mode 100644 index af9452a..0000000 Binary files a/example/assets/sprites/alienpls-56.png and /dev/null differ diff --git a/example/assets/sprites/blobdance-128.png b/example/assets/sprites/blobdance-128.png deleted file mode 100644 index 6326ec0..0000000 Binary files a/example/assets/sprites/blobdance-128.png and /dev/null differ diff --git a/example/assets/sprites/dance2-112.png b/example/assets/sprites/dance2-112.png deleted file mode 100644 index 2d69026..0000000 Binary files a/example/assets/sprites/dance2-112.png and /dev/null differ diff --git a/example/assets/sprites/dancer-128.png b/example/assets/sprites/dancer-128.png deleted file mode 100644 index c3fdbc0..0000000 Binary files a/example/assets/sprites/dancer-128.png and /dev/null differ diff --git a/example/assets/sprites/mainblob-128.png b/example/assets/sprites/mainblob-128.png deleted file mode 100644 index ca78578..0000000 Binary files a/example/assets/sprites/mainblob-128.png and /dev/null differ diff --git a/example/house.rb b/example/house.rb deleted file mode 100644 index c6257ee..0000000 --- a/example/house.rb +++ /dev/null @@ -1,100 +0,0 @@ -# frozen_string_literal: true - -# Is a house -class House - def initialize(x, y) - @objects = [] - @x = x - @y = y - @objects.push Camera::Image.new('assets/blobshadow.png', - width: 320, - height: 250, - x: x - 10, - y: y + 130, - z: 0) - @objects.push Camera::Image.new('assets/bricktexture.png', - x: x, - y: y, - width: 300, - height: 300) - @objects.push Camera::Square.new(x: 125 + x, - y: 230 + y, - size: 50, - color: 'black') - @objects.push Camera::Circle.new(x: 125 + x, - y: 205 + y, - radius: 25, - color: 'black') - @objects.push Camera::Triangle.new(x1: -5 + x, - y1: 16 + y, - x2: 310 + x, - y2: 14 + y, - x3: 150 + x, - y3: -75 + y, - color: 'red') - @objects.push Camera::Square.new(x: 160 + x, - y: 20 + y, - size: 100, - color: 'brown', - z: 1) - @objects.push Camera::Square.new(x: 170 + x, - y: 25 + y, - size: 80, - opacity: 0.5, - color: 'blue', - z: 2) - @objects.push Camera::Rectangle.new(x: 160 + x, - y: 105 + y, - width: 100, - height: 20, - color: 'brown', - z: 3) - @objects.push Camera::Line.new(x1: 210 + x, - y1: 105 + y, - x2: 210 + x, - y2: 25 + y, - width: 4, - color: 'brown', - z: 3) - @objects.push Camera::Line.new(x1: 250 + x, - y1: 65 + y, - x2: 170 + x, - y2: 65 + y, - width: 4, - color: 'brown', - z: 3) - @objects.push Camera::Sprite.new('./assets/sprites/alienpls-56.png', - x: 175 + x, - y: 65 + y, - width: 56, - height: 56, - clip_width: 56, - loop: true, - time: 35, - z: 1) - @objects.last.play - # @objects.each do |item| - # Camera << item - # end - end - - def remove - @objects.each do |item| - # Camera.remove item - item.remove - end - end - - def visted_by?(character) - x = @x + 80 - y = @y + 160 - if character.x >= x && character.x <= (x + (character.width * 2)) && character.y > y && character.y <= (y + (character.height * 2)) - Camera::Text.new('Press Space To Enter House', - x: x + 70, - y: y + 30, - color: 'white', - z: 98, - size: 25.0) - end - end -end diff --git a/example/old_example/README.mdown b/example/old_example/README.mdown new file mode 100644 index 0000000..186f6e3 --- /dev/null +++ b/example/old_example/README.mdown @@ -0,0 +1,3 @@ +### Example Is Currently Broken + +Most likely an issue with the example code, but is too spaghetti for me to want to revisit it diff --git a/example/old_example/assets/background.png b/example/old_example/assets/background.png new file mode 100644 index 0000000..f411e96 Binary files /dev/null and b/example/old_example/assets/background.png differ diff --git a/example/old_example/assets/blobcoolthink.png b/example/old_example/assets/blobcoolthink.png new file mode 100644 index 0000000..7cdfcc8 Binary files /dev/null and b/example/old_example/assets/blobcoolthink.png differ diff --git a/example/old_example/assets/blobshadow.png b/example/old_example/assets/blobshadow.png new file mode 100644 index 0000000..ff981cb Binary files /dev/null and b/example/old_example/assets/blobshadow.png differ diff --git a/example/old_example/assets/bricktexture.png b/example/old_example/assets/bricktexture.png new file mode 100644 index 0000000..4ef86e0 Binary files /dev/null and b/example/old_example/assets/bricktexture.png differ diff --git a/example/old_example/assets/legacy_player.png b/example/old_example/assets/legacy_player.png new file mode 100644 index 0000000..485e972 Binary files /dev/null and b/example/old_example/assets/legacy_player.png differ diff --git a/example/old_example/assets/sprites/alienpls-56.png b/example/old_example/assets/sprites/alienpls-56.png new file mode 100644 index 0000000..af9452a Binary files /dev/null and b/example/old_example/assets/sprites/alienpls-56.png differ diff --git a/example/old_example/assets/sprites/blobdance-128.png b/example/old_example/assets/sprites/blobdance-128.png new file mode 100644 index 0000000..6326ec0 Binary files /dev/null and b/example/old_example/assets/sprites/blobdance-128.png differ diff --git a/example/old_example/assets/sprites/dance2-112.png b/example/old_example/assets/sprites/dance2-112.png new file mode 100644 index 0000000..2d69026 Binary files /dev/null and b/example/old_example/assets/sprites/dance2-112.png differ diff --git a/example/old_example/assets/sprites/dancer-128.png b/example/old_example/assets/sprites/dancer-128.png new file mode 100644 index 0000000..c3fdbc0 Binary files /dev/null and b/example/old_example/assets/sprites/dancer-128.png differ diff --git a/example/old_example/assets/sprites/mainblob-128.png b/example/old_example/assets/sprites/mainblob-128.png new file mode 100644 index 0000000..ca78578 Binary files /dev/null and b/example/old_example/assets/sprites/mainblob-128.png differ diff --git a/example/old_example/house.rb b/example/old_example/house.rb new file mode 100644 index 0000000..c6257ee --- /dev/null +++ b/example/old_example/house.rb @@ -0,0 +1,100 @@ +# frozen_string_literal: true + +# Is a house +class House + def initialize(x, y) + @objects = [] + @x = x + @y = y + @objects.push Camera::Image.new('assets/blobshadow.png', + width: 320, + height: 250, + x: x - 10, + y: y + 130, + z: 0) + @objects.push Camera::Image.new('assets/bricktexture.png', + x: x, + y: y, + width: 300, + height: 300) + @objects.push Camera::Square.new(x: 125 + x, + y: 230 + y, + size: 50, + color: 'black') + @objects.push Camera::Circle.new(x: 125 + x, + y: 205 + y, + radius: 25, + color: 'black') + @objects.push Camera::Triangle.new(x1: -5 + x, + y1: 16 + y, + x2: 310 + x, + y2: 14 + y, + x3: 150 + x, + y3: -75 + y, + color: 'red') + @objects.push Camera::Square.new(x: 160 + x, + y: 20 + y, + size: 100, + color: 'brown', + z: 1) + @objects.push Camera::Square.new(x: 170 + x, + y: 25 + y, + size: 80, + opacity: 0.5, + color: 'blue', + z: 2) + @objects.push Camera::Rectangle.new(x: 160 + x, + y: 105 + y, + width: 100, + height: 20, + color: 'brown', + z: 3) + @objects.push Camera::Line.new(x1: 210 + x, + y1: 105 + y, + x2: 210 + x, + y2: 25 + y, + width: 4, + color: 'brown', + z: 3) + @objects.push Camera::Line.new(x1: 250 + x, + y1: 65 + y, + x2: 170 + x, + y2: 65 + y, + width: 4, + color: 'brown', + z: 3) + @objects.push Camera::Sprite.new('./assets/sprites/alienpls-56.png', + x: 175 + x, + y: 65 + y, + width: 56, + height: 56, + clip_width: 56, + loop: true, + time: 35, + z: 1) + @objects.last.play + # @objects.each do |item| + # Camera << item + # end + end + + def remove + @objects.each do |item| + # Camera.remove item + item.remove + end + end + + def visted_by?(character) + x = @x + 80 + y = @y + 160 + if character.x >= x && character.x <= (x + (character.width * 2)) && character.y > y && character.y <= (y + (character.height * 2)) + Camera::Text.new('Press Space To Enter House', + x: x + 70, + y: y + 30, + color: 'white', + z: 98, + size: 25.0) + end + end +end diff --git a/example/old_example/room.rb b/example/old_example/room.rb new file mode 100644 index 0000000..27c002d --- /dev/null +++ b/example/old_example/room.rb @@ -0,0 +1,129 @@ +# frozen_string_literal: true + +# Is a house +class Room + def initialize(x, y) + @objects = [] + @x = x + @y = y + roomy = 155 + @objects.push Camera::Square.new(x: 125 + x, + y: 230 + y, + size: 50, + color: 'blue', + z: 3) + @objects.push Camera::Circle.new(x: 125 + x, + y: 205 + y, + radius: 25, + sectors: 64, + color: 'blue', + z: 3) + @objects.push Camera::Quad.new(x1: 50 + x, + y1: 130 + y + roomy, + x2: 125 + x, + y2: 180 + y + roomy, + x3: 325 + x, + y3: 180 + y + roomy, + x4: 250 + x, + y4: 130 + y + roomy, + color: 'aqua', + z: 1) + @objects.push Camera::Quad.new(x1: 50 + x, + y1: 45 + y + roomy, + x2: 50 + x, + y2: 125 + y + roomy, + x3: 250 + x, + y3: 125 + y + roomy, + x4: 250 + x, + y4: 45 + y + roomy, + color: 'orange', + z: 1) + @objects.push Camera::Quad.new(x1: 255 + x, + y1: 45 + y + roomy, + x2: 255 + x, + y2: 125 + y + roomy, + x3: 330 + x, + y3: 175 + y + roomy, + x4: 330 + x, + y4: 95 + y + roomy, + color: 'olive', + z: 1) + @objects.push Camera::Sprite.new('./assets/sprites/blobdance-128.png', + x: 250 + x, + y: 135 + y + roomy, + width: 40, + height: 40, + clip_width: 128, + loop: true, + time: 24, + z: 5) + @objects.last.play + @objects.push Camera::Image.new( + 'assets/blobshadow.png', + x: 250 + x - 2 - 5, + y: 135 + y + roomy + 42 - 15, + width: 52, + height: 10, + z: 4 + ) + @objects.push Camera::Sprite.new('./assets/sprites/dance2-112.png', + x: 95 + x, + y: 115 + y + roomy, + width: 40, + height: 40, + clip_width: 112, + loop: true, + time: 13, + z: 5) + @objects.last.play + @objects.push Camera::Image.new( + 'assets/blobshadow.png', + x: 95 + x - 2 - 5, + y: 115 + y + roomy + 42 - 10, + width: 52, + height: 10, + z: 4 + ) + @objects.push Camera::Sprite.new('./assets/sprites/dancer-128.png', + x: 175 + x, + y: 120 + y + roomy, + width: 45, + height: 45, + clip_width: 128, + loop: true, + time: 60, + z: 5) + @objects.last.play + @objects.push Camera::Image.new( + 'assets/blobshadow.png', + x: 175 + x - 2 - 5, + y: 120 + y + roomy + 42 - 5, + width: 52, + height: 10, + z: 4 + ) + # @objects.each do |item| + # Camera << item + # end + end + + def remove + @objects.each do |item| + # Camera.remove item + item.remove + end + end + + def visted_by?(character) + x = @x + 80 + y = @y + 160 + if character.x >= x && character.x <= (x + (character.width * 2)) && character.y > y && character.y <= (y + (character.height * 2)) + Camera::Text.new('Press Space To Exit House', + x: x + 70, + y: y + 30, + color: 'white', + z: 98, + size: 25.0) + end + end +end diff --git a/example/old_example/run.rb b/example/old_example/run.rb new file mode 100644 index 0000000..2790f40 --- /dev/null +++ b/example/old_example/run.rb @@ -0,0 +1,223 @@ +# frozen_string_literal: true + +require 'ruby2d' +require 'ruby2d/camera' +require_relative 'house' +require_relative 'room' + +Window.set(icon: './assets/blobcoolthink.png', + width: 1280, + height: 720, + background: 'blue') + +@player = Camera::Sprite.new('./assets/sprites/mainblob-128.png', + x: 1920 / 1.1, + y: 1080 / 1.1, + width: 50, + height: 50, + clip_width: 128, + loop: true, + time: 1, + z: 99, + animations: { + walk: 0...60, + stand: 60...61 + }) +@shadow = Camera::Image.new( + 'assets/blobshadow.png', + width: 52, + height: 10, + z: 4 +) +@player.play animation: :walk, loop: true + +# UI +Rectangle.new( + width: 350, + height: 135, + color: 'navy', + z: 100 +) +@ui_pos_cam = Text.new( + 'pos: 0,0', + x: 10, + y: 10, + color: 'teal', + z: 101 +) +@ui_pos_ply = Text.new( + 'pos: 0,0', + x: 10, + y: 40, + color: 'teal', + z: 101 +) +@ui_zoom = Text.new( + 'zoom: 0', + x: 10, + y: 70, + color: 'lime', + z: 101 +) +@ui_rotation = Text.new( + 'rotation: 0', + x: 10, + y: 100, + color: 'lime', + z: 101 +) +Rectangle.new( + x: (Window.width - 120), + width: 120, + height: 45, + color: 'navy', + z: 100 +) +@ui_fps = Text.new( + 'fps: 60.00', + x: (Window.width - 110), + y: 10, + color: 'teal', + z: 101 +) + +# How fast the player can move +@speed = 5 + +# Initializing +@player_movement_x = 0 +@player_movement_y = 0 +@pressed_space = false +@scene_transition_into = false +@scene_transition_out = false +@indoors = false +@house = nil +@room = nil + +on :key do |event| + @player_movement_y -= @speed if event.key == 'w' && !(@scene_transition_into || @scene_transition_out) + @player_movement_y += @speed if event.key == 's' && !(@scene_transition_into || @scene_transition_out) + @player_movement_x += @speed if event.key == 'd' && !(@scene_transition_into || @scene_transition_out) + @player_movement_x -= @speed if event.key == 'a' && !(@scene_transition_into || @scene_transition_out) + @pressed_space = true if event.key == 'space' && !(@scene_transition_into || @scene_transition_out) + Camera.angle += 1 if event.key == 'q' && !(@scene_transition_into || @scene_transition_out) + Camera.angle -= 1 if event.key == 'e' && !(@scene_transition_into || @scene_transition_out) + if event.key == 'r' && !(@scene_transition_into || @scene_transition_out || Camera.angle.zero?) + if Camera.angle <= 180 + Camera.angle -= if Camera.angle > 10 + 10 + else + 1 + end + elsif Camera.angle += if Camera.angle < 350 + 10 + else + 1 + end + end + end +end + +update do + if (@player.x > 2371 && @player_movement_x.positive?) || (@player.x.negative? && @player_movement_x.negative?) + @player_movement_x = 0 + end + if (@player.y > 1608 && @player_movement_y.positive?) || (@player.y.negative? && @player_movement_y.negative?) + @player_movement_y = 0 + end + if !@player_movement_y.zero? == !@player_movement_x.zero? + @player_movement_x /= 1.4141 + @player_movement_y /= 1.4141 + end + @player.x += @player_movement_x + @player.y += @player_movement_y + if @player_movement_x.negative? + @player.play animation: :walk, loop: true + elsif @player_movement_x.positive? || !@player_movement_y.zero? + @player.play animation: :walk, loop: true, flip: :vertical + else + @player.play animation: :stand + end + + if !@scene_transition_into && !@scene_transition_out + Camera.zoom += ((-[ + Math.sqrt(((@player.x + (@player.width / 2) - Camera.x)**2) + ((@player.y + (@player.width / 2) - Camera.y)**2)), 350 + ].min * 0.004) + 2 - Camera.zoom) * 0.25 + Camera.x += (@player.x + (@player.width / 2) - Camera.x) * 0.025 + Camera.y += (@player.y + (@player.height / 2) - Camera.y) * 0.025 + elsif @scene_transition_into + if Camera.zoom < 250 + Camera.zoom *= 1.05 + Camera.angle += 5 + else + @scene_transition_into = false + @scene_transition_out = true + if @room.nil? + @house.remove + @house = nil + @indoors = true + else + @room.remove + @room = nil + @indoors = false + end + @background.remove + @background = nil + end + Camera.x += (@player.x + (@player.width / 2) - Camera.x) * 0.25 + Camera.y += (@player.y + (@player.height / 2) - Camera.y) * 0.25 + elsif @scene_transition_out + if !((Camera.zoom <= (@zoom_transition * 1.001)) && (Camera.angle == @angle_transition)) + Camera.zoom /= 1.05 + Camera.angle -= 5 + else + @scene_transition_out = false + end + end + + if @house.nil? && !@indoors + @house = House.new(750, 300) + @background = Camera::Image.new( + 'assets/background.png', + x: 100, y: 100, + z: -1 + ) + elsif @room.nil? && @indoors + @room = Room.new(750, 300) + @background = Camera::Rectangle.new( + color: 'black', + x: 0, + y: 0, + width: 1920, + height: 1080, + z: -1 + ) + end + @shadow.x = @player.x - 2 + @shadow.y = @player.y + 42 + + @house_text&.remove + @house_text = if @indoors + @room.visted_by?(@player) + else + @house.visted_by?(@player) + end + unless @house_text.nil? + @house_text.center = true + end + if !@house_text.nil? && @pressed_space && !@scene_transition_into && !@scene_transition_out + @scene_transition_into = true + @angle_transition = Camera.angle + @zoom_transition = Camera.zoom + end + @ui_pos_cam.text = "Camera Position: #{Camera.x.round(1)}, #{Camera.y.round(1)}" + @ui_pos_ply.text = "Player Position: #{@player.x.round(1)}, #{@player.y.round(1)}" + @ui_zoom.text = "Zoom: #{Camera.zoom.round(3)}" + @ui_fps.text = "FPS: #{Window.fps.round(2)}" + @ui_rotation.text = "Angle: #{Camera.angle}" + @player_movement_x = 0 + @player_movement_y = 0 + @pressed_space = false + +end +show diff --git a/example/room.rb b/example/room.rb deleted file mode 100644 index 27c002d..0000000 --- a/example/room.rb +++ /dev/null @@ -1,129 +0,0 @@ -# frozen_string_literal: true - -# Is a house -class Room - def initialize(x, y) - @objects = [] - @x = x - @y = y - roomy = 155 - @objects.push Camera::Square.new(x: 125 + x, - y: 230 + y, - size: 50, - color: 'blue', - z: 3) - @objects.push Camera::Circle.new(x: 125 + x, - y: 205 + y, - radius: 25, - sectors: 64, - color: 'blue', - z: 3) - @objects.push Camera::Quad.new(x1: 50 + x, - y1: 130 + y + roomy, - x2: 125 + x, - y2: 180 + y + roomy, - x3: 325 + x, - y3: 180 + y + roomy, - x4: 250 + x, - y4: 130 + y + roomy, - color: 'aqua', - z: 1) - @objects.push Camera::Quad.new(x1: 50 + x, - y1: 45 + y + roomy, - x2: 50 + x, - y2: 125 + y + roomy, - x3: 250 + x, - y3: 125 + y + roomy, - x4: 250 + x, - y4: 45 + y + roomy, - color: 'orange', - z: 1) - @objects.push Camera::Quad.new(x1: 255 + x, - y1: 45 + y + roomy, - x2: 255 + x, - y2: 125 + y + roomy, - x3: 330 + x, - y3: 175 + y + roomy, - x4: 330 + x, - y4: 95 + y + roomy, - color: 'olive', - z: 1) - @objects.push Camera::Sprite.new('./assets/sprites/blobdance-128.png', - x: 250 + x, - y: 135 + y + roomy, - width: 40, - height: 40, - clip_width: 128, - loop: true, - time: 24, - z: 5) - @objects.last.play - @objects.push Camera::Image.new( - 'assets/blobshadow.png', - x: 250 + x - 2 - 5, - y: 135 + y + roomy + 42 - 15, - width: 52, - height: 10, - z: 4 - ) - @objects.push Camera::Sprite.new('./assets/sprites/dance2-112.png', - x: 95 + x, - y: 115 + y + roomy, - width: 40, - height: 40, - clip_width: 112, - loop: true, - time: 13, - z: 5) - @objects.last.play - @objects.push Camera::Image.new( - 'assets/blobshadow.png', - x: 95 + x - 2 - 5, - y: 115 + y + roomy + 42 - 10, - width: 52, - height: 10, - z: 4 - ) - @objects.push Camera::Sprite.new('./assets/sprites/dancer-128.png', - x: 175 + x, - y: 120 + y + roomy, - width: 45, - height: 45, - clip_width: 128, - loop: true, - time: 60, - z: 5) - @objects.last.play - @objects.push Camera::Image.new( - 'assets/blobshadow.png', - x: 175 + x - 2 - 5, - y: 120 + y + roomy + 42 - 5, - width: 52, - height: 10, - z: 4 - ) - # @objects.each do |item| - # Camera << item - # end - end - - def remove - @objects.each do |item| - # Camera.remove item - item.remove - end - end - - def visted_by?(character) - x = @x + 80 - y = @y + 160 - if character.x >= x && character.x <= (x + (character.width * 2)) && character.y > y && character.y <= (y + (character.height * 2)) - Camera::Text.new('Press Space To Exit House', - x: x + 70, - y: y + 30, - color: 'white', - z: 98, - size: 25.0) - end - end -end diff --git a/example/run.rb b/example/run.rb deleted file mode 100644 index 96ab9ab..0000000 --- a/example/run.rb +++ /dev/null @@ -1,223 +0,0 @@ -# frozen_string_literal: true - -require 'ruby2d' -require 'ruby2d/camera' -require_relative 'house' -require_relative 'room' - -Window.set(icon: './assets/blobcoolthink.png', - width: 1280, - height: 720, - background: 'blue') - -@player = Camera::Sprite.new('./assets/sprites/mainblob-128.png', - x: 1920 / 1.1, - y: 1080 / 1.1, - width: 50, - height: 50, - clip_width: 128, - loop: true, - time: 1, - z: 99, - animations: { - walk: 0...60, - stand: 60...61 - }) -@shadow = Camera::Image.new( - 'assets/blobshadow.png', - width: 52, - height: 10, - z: 4 -) -@player.play animation: :walk, loop: true - -# UI -Rectangle.new( - width: 350, - height: 135, - color: 'navy', - z: 100 -) -@ui_pos_cam = Text.new( - 'pos: 0,0', - x: 10, - y: 10, - color: 'teal', - z: 101 -) -@ui_pos_ply = Text.new( - 'pos: 0,0', - x: 10, - y: 40, - color: 'teal', - z: 101 -) -@ui_zoom = Text.new( - 'zoom: 0', - x: 10, - y: 70, - color: 'lime', - z: 101 -) -@ui_rotation = Text.new( - 'rotation: 0', - x: 10, - y: 100, - color: 'lime', - z: 101 -) -Rectangle.new( - x: (Window.width - 120), - width: 120, - height: 45, - color: 'navy', - z: 100 -) -@ui_fps = Text.new( - 'fps: 60.00', - x: (Window.width - 110), - y: 10, - color: 'teal', - z: 101 -) - -# How fast the player can move -@speed = 5 - -# Initializing -@player_movement_x = 0 -@player_movement_y = 0 -@pressed_space = false -@scene_transition_into = false -@scene_transition_out = false -@indoors = false -@house = nil -@room = nil - -on :key do |event| - @player_movement_y -= @speed if event.key == 'w' && !(@scene_transition_into || @scene_transition_out) - @player_movement_y += @speed if event.key == 's' && !(@scene_transition_into || @scene_transition_out) - @player_movement_x += @speed if event.key == 'd' && !(@scene_transition_into || @scene_transition_out) - @player_movement_x -= @speed if event.key == 'a' && !(@scene_transition_into || @scene_transition_out) - @pressed_space = true if event.key == 'space' && !(@scene_transition_into || @scene_transition_out) - Camera.angle += 1 if event.key == 'q' && !(@scene_transition_into || @scene_transition_out) - Camera.angle -= 1 if event.key == 'e' && !(@scene_transition_into || @scene_transition_out) - if event.key == 'r' && !(@scene_transition_into || @scene_transition_out || Camera.angle.zero?) - if Camera.angle <= 180 - Camera.angle -= if Camera.angle > 10 - 10 - else - 1 - end - elsif Camera.angle += if Camera.angle < 350 - 10 - else - 1 - end - end - end -end - -update do - if (@player.x > 2371 && @player_movement_x.positive?) || (@player.x.negative? && @player_movement_x.negative?) - @player_movement_x = 0 - end - if (@player.y > 1608 && @player_movement_y.positive?) || (@player.y.negative? && @player_movement_y.negative?) - @player_movement_y = 0 - end - if !@player_movement_y.zero? == !@player_movement_x.zero? - @player_movement_x /= 1.4141 - @player_movement_y /= 1.4141 - end - @player.x += @player_movement_x - @player.y += @player_movement_y - if @player_movement_x.negative? - @player.play animation: :walk, loop: true - elsif @player_movement_x.positive? || !@player_movement_y.zero? - @player.play animation: :walk, loop: true, flip: :vertical - else - @player.play animation: :stand - end - - if !@scene_transition_into && !@scene_transition_out - Camera.zoom += ((-[ - Math.sqrt(((@player.x + (@player.width / 2) - Camera.x)**2) + ((@player.y + (@player.width / 2) - Camera.y)**2)), 350 - ].min * 0.004) + 2 - Camera.zoom) * 0.25 - Camera.x += (@player.x + (@player.width / 2) - Camera.x) * 0.025 - Camera.y += (@player.y + (@player.height / 2) - Camera.y) * 0.025 - elsif @scene_transition_into - if Camera.zoom < 250 - Camera.zoom *= 1.05 - Camera.angle += 5 - else - @scene_transition_into = false - @scene_transition_out = true - if @room.nil? - @house.remove - @house = nil - @indoors = true - else - @room.remove - @room = nil - @indoors = false - end - @background.remove - @background = nil - end - Camera.x += (@player.x + (@player.width / 2) - Camera.x) * 0.25 - Camera.y += (@player.y + (@player.height / 2) - Camera.y) * 0.25 - elsif @scene_transition_out - if !((Camera.zoom <= (@zoom_transition * 1.001)) && (Camera.angle == @angle_transition)) - Camera.zoom /= 1.05 - Camera.angle -= 5 - else - @scene_transition_out = false - end - end - - if @house.nil? && !@indoors - @house = House.new(750, 300) - @background = Camera::Image.new( - 'assets/background.png', - x: 100, y: 100, - z: -1 - ) - elsif @room.nil? && @indoors - @room = Room.new(750, 300) - @background = Camera::Rectangle.new( - color: 'black', - x: 0, - y: 0, - width: 1920, - height: 1080, - z: -1 - ) - end - @shadow.x = @player.x - 2 - @shadow.y = @player.y + 42 - - @house_text&.remove - @house_text = if @indoors - @room.visted_by?(@player) - else - @house.visted_by?(@player) - end - unless @house_text.nil? - @house_text.center = true - end - if !@house_text.nil? && @pressed_space && !@scene_transition_into && !@scene_transition_out - @scene_transition_into = true - @angle_transition = Camera.angle - @zoom_transition = Camera.zoom - end - @ui_pos_cam.text = "Camera Position: #{Camera.x.round(1)}, #{Camera.y.round(1)}" - @ui_pos_ply.text = "Player Position: #{@player.x.round(1)}, #{@player.y.round(1)}" - @ui_zoom.text = "Zoom: #{Camera.zoom.round(3)}" - @ui_fps.text = "FPS: #{Window.fps.round(2)}" - @ui_rotation.text = "Angle: #{Camera.angle}" - @player_movement_x = 0 - @player_movement_y = 0 - @pressed_space = false - -end -show diff --git a/example/test/assets/alienpls-56.png b/example/test/assets/alienpls-56.png new file mode 100644 index 0000000..af9452a Binary files /dev/null and b/example/test/assets/alienpls-56.png differ diff --git a/example/test/assets/bricktexture-600.png b/example/test/assets/bricktexture-600.png new file mode 100644 index 0000000..4ef86e0 Binary files /dev/null and b/example/test/assets/bricktexture-600.png differ diff --git a/example/test/run.rb b/example/test/run.rb new file mode 100644 index 0000000..14e9d5c --- /dev/null +++ b/example/test/run.rb @@ -0,0 +1,378 @@ +require 'ruby2d' +require 'ruby2d/camera' + +Window.set(width: 640, + height: 480) + +# ----- UI ----- +#Rectangle.new( +# width: 350, +# height: 135, +# color: 'navy', +# z: 100 +#) +@ui_pos_cam = Text.new( + 'pos: 0,0', + x: 10, + y: 10, + color: 'teal', + z: 101 +) +#@ui_pos_ply = Text.new( +# 'pos: 0,0', +# x: 10, +# y: 40, +# color: 'teal', +# z: 101 +#) +@ui_zoom = Text.new( + 'zoom: 0', + x: 10, + y: 35, + color: 'lime', + z: 101 +) +@ui_rotation = Text.new( + 'rotation: 0', + x: 10, + y: 60, + color: 'lime', + z: 101 +) +#Rectangle.new( +# x: (Window.width - 120), +# width: 80, +# height: 45, +# color: 'navy', +# opacity: 0.3, +# z: 100 +#) +@ui_fps = Text.new( + 'fps: 60.00', + x: (Window.width - 110), + y: 10, + color: 'teal', + z: 101 +) +@dancing_alien1 = Camera::Sprite.new('./assets/alienpls-56.png', + x: 520 + -28, + y: -28, + width: 56, + height: 56, + clip_width: 56, + loop: true, + time: 35, + z: 1) +@dancing_alien1.play +@dancing_alien2 = Camera::Sprite.new('./assets/alienpls-56.png', + x: -28, + y: 370 + -28, + width: 56, + height: 56, + clip_width: 56, + loop: true, + time: 35, + z: 1) +@dancing_alien2.play +@dancing_alien3 = Camera::Sprite.new('./assets/alienpls-56.png', + x: -520 + -28, + y: -28, + width: 56, + height: 56, + clip_width: 56, + loop: true, + time: 35, + z: 1) +@dancing_alien3.play +@dancing_alien4 = Camera::Sprite.new('./assets/alienpls-56.png', + x: -28, + y: -370 + -28, + width: 56, + height: 56, + clip_width: 56, + loop: true, + time: 35, + z: 1) +@dancing_alien4.play + +@caution_topleft = [] +@caution_topright = [] +@caution_botleft = [] +@caution_botright = [] + +@caution_topleft.push Camera::Triangle.new(color: 'yellow', + x1: -280, y1: -200, + x2: -260, y2: -220, + x3: -300, y3: -220 + ) +@caution_topleft.push Camera::Triangle.new(color: 'gray', + x1: -280, y1: -200, + x2: -300, y2: -180, + x3: -300, y3: -220 + ) +@caution_topleft.push Camera::Quad.new(color: 'yellow', + x1: -280, y1: -200, + x2: -300, y2: -180, + x3: -300, y3: -160, + x4: -280, y4: -180 + ) +@caution_topleft.push Camera::Quad.new(color: 'gray', + x1: -280, y1: -160, + x2: -300, y2: -140, + x3: -300, y3: -160, + x4: -280, y4: -180 + ) +@caution_topleft.push Camera::Quad.new(color: 'gray', + x1: -280, y1: -200, + x2: -260, y2: -220, + x3: -240, y3: -220, + x4: -260, y4: -200 + ) +@caution_topleft.push Camera::Quad.new(color: 'yellow', + x1: -240, y1: -200, + x2: -220, y2: -220, + x3: -240, y3: -220, + x4: -260, y4: -200 + ) + +@caution_topright.push Camera::Triangle.new(color: 'yellow', + x1: 280, y1: -200, + x2: 260, y2: -220, + x3: 300, y3: -220 + ) +@caution_topright.push Camera::Triangle.new(color: 'gray', + x1: 280, y1: -200, + x2: 300, y2: -180, + x3: 300, y3: -220 + ) +@caution_topright.push Camera::Quad.new(color: 'yellow', + x1: 280, y1: -200, + x2: 300, y2: -180, + x3: 300, y3: -160, + x4: 280, y4: -180 + ) +@caution_topright.push Camera::Quad.new(color: 'gray', + x1: 280, y1: -160, + x2: 300, y2: -140, + x3: 300, y3: -160, + x4: 280, y4: -180 + ) +@caution_topright.push Camera::Quad.new(color: 'gray', + x1: 280, y1: -200, + x2: 260, y2: -220, + x3: 240, y3: -220, + x4: 260, y4: -200 + ) +@caution_topright.push Camera::Quad.new(color: 'yellow', + x1: 240, y1: -200, + x2: 220, y2: -220, + x3: 240, y3: -220, + x4: 260, y4: -200 + ) + +@caution_botleft.push Camera::Triangle.new(color: 'yellow', + x1: -280, y1: 200, + x2: -260, y2: 220, + x3: -300, y3: 220 + ) +@caution_botleft.push Camera::Triangle.new(color: 'gray', + x1: -280, y1: 200, + x2: -300, y2: 180, + x3: -300, y3: 220 + ) +@caution_botleft.push Camera::Quad.new(color: 'yellow', + x1: -280, y1: 200, + x2: -300, y2: 180, + x3: -300, y3: 160, + x4: -280, y4: 180 + ) +@caution_botleft.push Camera::Quad.new(color: 'gray', + x1: -280, y1: 160, + x2: -300, y2: 140, + x3: -300, y3: 160, + x4: -280, y4: 180 + ) +@caution_botleft.push Camera::Quad.new(color: 'gray', + x1: -280, y1: 200, + x2: -260, y2: 220, + x3: -240, y3: 220, + x4: -260, y4: 200 + ) +@caution_botleft.push Camera::Quad.new(color: 'yellow', + x1: -240, y1: 200, + x2: -220, y2: 220, + x3: -240, y3: 220, + x4: -260, y4: 200 + ) + +@caution_botright.push Camera::Triangle.new(color: 'yellow', + x1: 280, y1: 200, + x2: 260, y2: 220, + x3: 300, y3: 220 + ) +@caution_botright.push Camera::Triangle.new(color: 'gray', + x1: 280, y1: 200, + x2: 300, y2: 180, + x3: 300, y3: 220 + ) +@caution_botright.push Camera::Quad.new(color: 'yellow', + x1: 280, y1: 200, + x2: 300, y2: 180, + x3: 300, y3: 160, + x4: 280, y4: 180 + ) +@caution_botright.push Camera::Quad.new(color: 'gray', + x1: 280, y1: 160, + x2: 300, y2: 140, + x3: 300, y3: 160, + x4: 280, y4: 180 + ) +@caution_botright.push Camera::Quad.new(color: 'gray', + x1: 280, y1: 200, + x2: 260, y2: 220, + x3: 240, y3: 220, + x4: 260, y4: 200 + ) +@caution_botright.push Camera::Quad.new(color: 'yellow', + x1: 240, y1: 200, + x2: 220, y2: 220, + x3: 240, y3: 220, + x4: 260, y4: 200 + ) + +@text = Camera::Text.new("Bigger circles should always be in front of smaller ones", + size: 30, + x: 0, y: 65) +@text.center = true + +@zindex_test = [] + +@zindex_test.push Camera::Circle.new(color: 'random', + x: 50 + -25, y: -25, + radius: 25) +@zindex_test.push Camera::Circle.new(color: 'random', + x: 25 + -25, y: -25, + radius: 25) +@zindex_test.push Camera::Circle.new(color: 'random', + x: 0 + -25, y: -25, + radius: 25) +@zindex_test.push Camera::Circle.new(color: 'random', + x: -25 + -25, y: -25, + radius: 25) +@zindex_test.push Camera::Circle.new(color: 'random', + x: -50 + -25, y: -25, + radius: 25) + +@speed = 5 +@one_degree_in_radians = 0.01745329 + +on :key do |event| + Camera.y -= (Math.cos(Camera.angle * @one_degree_in_radians) * @speed) if event.key == 'w' + Camera.y += (Math.cos(Camera.angle * @one_degree_in_radians) * @speed) if event.key == 's' + Camera.x -= (Math.sin(Camera.angle * @one_degree_in_radians) * @speed) if event.key == 'w' + Camera.x += (Math.sin(Camera.angle * @one_degree_in_radians) * @speed) if event.key == 's' + + Camera.x += (Math.cos(Camera.angle * @one_degree_in_radians) * @speed) if event.key == 'd' + Camera.x -= (Math.cos(Camera.angle * @one_degree_in_radians) * @speed) if event.key == 'a' + Camera.y -= (Math.sin(Camera.angle * @one_degree_in_radians) * @speed) if event.key == 'd' + Camera.y += (Math.sin(Camera.angle * @one_degree_in_radians) * @speed) if event.key == 'a' + + #@pressed_space = true if event.key == 'space' && !(@scene_transition_into || @scene_transition_out) + Camera.angle += 1.5 if event.key == 'q' + Camera.angle -= 1.5 if event.key == 'e' + Camera.zoom *= 1.01 if event.key == 'z' + Camera.zoom *= 0.99 if event.key == 'x' + if event.key == 'r'# && !(@scene_transition_into || @scene_transition_out || Camera.angle.zero?) + if Camera.zoom < 1.0 + Camera.zoom *= 1.03 + if Camera.zoom > 1.0 + Camera.zoom = 1.0 + end + elsif Camera.zoom > 1.0 + Camera.zoom /= 1.03 + if Camera.zoom < 1.0 + Camera.zoom = 1.0 + end + end + if Camera.angle <= 180 + Camera.angle -= if Camera.angle > 10 + 10 + elsif Camera.angle != 0 + 1 + else + 0 + end + elsif Camera.angle += if Camera.angle < 350 + 10 + elsif Camera.angle != 0 + 1 + else + 0 + end + if (Camera.angle > 359) || (Camera.angle < 1) + Camera.angle = 0 + end + end + end +end + +@frame = 0 +update do + @frame += 1 + + @a = (Math.sin(((@frame - 30) % 120) * (Math::PI / 60.0)) * 15 + 25) + @b = (Math.sin(((@frame - 15) % 120) * (Math::PI / 60.0)) * 15 + 25) + @c = (Math.sin((@frame % 120) * (Math::PI / 60.0)) * 15 + 25) + @d = (Math.sin(((@frame + 15) % 120) * (Math::PI / 60.0)) * 15 + 25) + @e = (Math.sin(((@frame + 30) % 120) * (Math::PI / 60.0)) * 15 + 25) + @zindex_test[0].z = @a + @zindex_test[0].radius = @a + @zindex_test[0].x = 50 + -@a + @zindex_test[0].y = -@a + @zindex_test[1].z = @b + @zindex_test[1].radius = @b + @zindex_test[1].x = 25 + -@b + @zindex_test[1].y = -@b + @zindex_test[2].z = @c + @zindex_test[2].radius = @c + @zindex_test[2].x = -@c + @zindex_test[2].y = -@c + @zindex_test[3].z = @d + @zindex_test[3].radius = @d + @zindex_test[3].x = -25 + -@d + @zindex_test[3].y = -@d + @zindex_test[4].z = @e + @zindex_test[4].radius = @e + @zindex_test[4].x = -50 + -@e + @zindex_test[4].y = -@e + + if (@frame % 120) < 30 + @dancing_alien1.add + @dancing_alien2.remove + @dancing_alien3.remove + @dancing_alien4.remove + elsif ((@frame + 30) % 120) < 30 + @dancing_alien1.remove + @dancing_alien2.add + @dancing_alien3.remove + @dancing_alien4.remove + elsif ((@frame + 60) % 120) < 30 + @dancing_alien1.remove + @dancing_alien2.remove + @dancing_alien3.add + @dancing_alien4.remove + elsif ((@frame + 90) % 120) < 30 + @dancing_alien1.remove + @dancing_alien2.remove + @dancing_alien3.remove + @dancing_alien4.add + end + + @ui_pos_cam.text = "Camera Position: #{Camera.x.round(1)}, #{Camera.y.round(1)}" + #@ui_pos_ply.text = "Player Position: #{@player.x.round(1)}, #{@player.y.round(1)}" + @ui_zoom.text = "Zoom: #{Camera.zoom.round(3)}" + @ui_fps.text = "FPS: #{Window.fps.round(2)}" + @ui_rotation.text = "Angle: #{Camera.angle}" +end + +show -- cgit v1.2.3