summaryrefslogtreecommitdiffhomepage
path: root/example
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-08-08 02:14:53 -0400
committerrealtradam <[email protected]>2021-08-08 02:14:53 -0400
commitcad7a2876013ae52f248d7d3fd35bb6d7a2d36cb (patch)
tree7888eb2df8401feaa1426d9ca411a7e7b76cb792 /example
parente9a05cecd41b244977c794931b4706015097774f (diff)
downloadruby2d-camera-cad7a2876013ae52f248d7d3fd35bb6d7a2d36cb.tar.gz
ruby2d-camera-cad7a2876013ae52f248d7d3fd35bb6d7a2d36cb.zip
fixed formatting and file structure
Diffstat (limited to 'example')
-rw-r--r--example/assets/background.pngbin0 -> 60142 bytes
-rw-r--r--example/assets/blobcoolthink.pngbin0 -> 13066 bytes
-rw-r--r--example/assets/blobshadow.pngbin0 -> 5679 bytes
-rw-r--r--example/assets/bricktexture.pngbin0 -> 741798 bytes
-rw-r--r--example/assets/legacy_player.pngbin0 -> 1044 bytes
-rw-r--r--example/assets/sprites/alienpls-56.pngbin0 -> 255710 bytes
-rw-r--r--example/assets/sprites/blobdance-128.pngbin0 -> 35825 bytes
-rw-r--r--example/assets/sprites/dance2-112.pngbin0 -> 214857 bytes
-rw-r--r--example/assets/sprites/dancer-128.pngbin0 -> 39270 bytes
-rw-r--r--example/assets/sprites/mainblob-128.pngbin0 -> 94731 bytes
-rw-r--r--example/house.rb100
-rw-r--r--example/room.rb129
-rw-r--r--example/run.rb223
13 files changed, 452 insertions, 0 deletions
diff --git a/example/assets/background.png b/example/assets/background.png
new file mode 100644
index 0000000..f411e96
--- /dev/null
+++ b/example/assets/background.png
Binary files differ
diff --git a/example/assets/blobcoolthink.png b/example/assets/blobcoolthink.png
new file mode 100644
index 0000000..7cdfcc8
--- /dev/null
+++ b/example/assets/blobcoolthink.png
Binary files differ
diff --git a/example/assets/blobshadow.png b/example/assets/blobshadow.png
new file mode 100644
index 0000000..ff981cb
--- /dev/null
+++ b/example/assets/blobshadow.png
Binary files differ
diff --git a/example/assets/bricktexture.png b/example/assets/bricktexture.png
new file mode 100644
index 0000000..4ef86e0
--- /dev/null
+++ b/example/assets/bricktexture.png
Binary files differ
diff --git a/example/assets/legacy_player.png b/example/assets/legacy_player.png
new file mode 100644
index 0000000..485e972
--- /dev/null
+++ b/example/assets/legacy_player.png
Binary files differ
diff --git a/example/assets/sprites/alienpls-56.png b/example/assets/sprites/alienpls-56.png
new file mode 100644
index 0000000..af9452a
--- /dev/null
+++ b/example/assets/sprites/alienpls-56.png
Binary files differ
diff --git a/example/assets/sprites/blobdance-128.png b/example/assets/sprites/blobdance-128.png
new file mode 100644
index 0000000..6326ec0
--- /dev/null
+++ b/example/assets/sprites/blobdance-128.png
Binary files differ
diff --git a/example/assets/sprites/dance2-112.png b/example/assets/sprites/dance2-112.png
new file mode 100644
index 0000000..2d69026
--- /dev/null
+++ b/example/assets/sprites/dance2-112.png
Binary files differ
diff --git a/example/assets/sprites/dancer-128.png b/example/assets/sprites/dancer-128.png
new file mode 100644
index 0000000..c3fdbc0
--- /dev/null
+++ b/example/assets/sprites/dancer-128.png
Binary files differ
diff --git a/example/assets/sprites/mainblob-128.png b/example/assets/sprites/mainblob-128.png
new file mode 100644
index 0000000..ca78578
--- /dev/null
+++ b/example/assets/sprites/mainblob-128.png
Binary files differ
diff --git a/example/house.rb b/example/house.rb
new file mode 100644
index 0000000..c6257ee
--- /dev/null
+++ b/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/room.rb b/example/room.rb
new file mode 100644
index 0000000..27c002d
--- /dev/null
+++ b/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/run.rb b/example/run.rb
new file mode 100644
index 0000000..96ab9ab
--- /dev/null
+++ b/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
+)
[email protected] 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