# Open up a window
init_window(800, 480, "Taylor Application")
# Setup audio so we can play sounds
init_audio_device
# Get the current monitor frame rate and set our target framerate to match.
set_target_fps(get_monitor_refresh_rate(get_current_monitor))
$shader = Shader.load(fragment_shader_code: <<-SHADER)
#version #{GLSL_VERSION}
precision mediump float;
// Input vertex attributes (from vertex shader)
varying vec2 fragTexCoord;
varying vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
void main() {
// Texel color fetching from texture sampler
vec4 texelColor = texture2D(texture0, fragTexCoord)*colDiffuse*fragColor;
// Convert texel color to grayscale using NTSC conversion weights
float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114));
// Calculate final fragment color
gl_FragColor = vec4(gray, gray, gray, texelColor.a);
}
SHADER
$scarfy = Texture2D.load("assets/scarfy.png")
$scarfy_destination = Rectangle.new(
(get_screen_width - $scarfy.width) / 2.0,
(get_screen_height - $scarfy.height) / 2.0,
$scarfy.width, $scarfy.height
)
# Define your main method
def main
# Get the amount of time passed since the last frame was rendered
delta = get_frame_time
# Your update logic goes here
drawing do
# Your drawing logic goes here
clear
if key_down?(KEY_SPACE) || mouse_button_down?(MOUSE_LEFT_BUTTON)
$shader.draw do
$scarfy.draw(destination: $scarfy_destination)
end
else
$scarfy.draw(destination: $scarfy_destination)
end
draw_text(
"Hold space or click the screen to apply the shader",
160, 10, 20, DARKGRAY
)
end
end
if browser?
set_main_loop 'main'
else
# Detect window close button or ESC key
main until window_should_close?
end
close_audio_device
close_window
# Open up a window
init_window(800, 480, "Taylor Application")
# Setup audio so we can play sounds
init_audio_device
# Get the current monitor frame rate and set our target framerate to match.
set_target_fps(get_monitor_refresh_rate(get_current_monitor))
# Sprites from https://kenney.nl/assets/pixel-platformer
$characters = Texture2D.load('./assets/characters.png')
$backgrounds = Texture2D.load('./assets/backgrounds.png')
# Fonts from https://kenney.nl/assets/kenney-fonts (only kenney_pixel.ttf and kenney_blocks.ttf)
$pixel_font = Font.load('./assets/kenney_pixel.ttf', size: 32)
class Character
def initialize
@timer = 0.5
@colour = [:green, :blue, :pink, :yellow, :cream].sample
@destination = Rectangle.new(388, 228, 24, 24)
@direction = Vector2.new(
50 - (rand * 100),
50 - (rand * 100)
)
default_sprite_position
end
def update(delta)
@destination.x += @direction.x * delta
@destination.y += @direction.y * delta
@direction.x *= -1 if @destination.x < 0 || @destination.x > 776
@direction.y *= -1 if @destination.y < 0 || @destination.y > 456
@timer -= delta
toggle_sprite if @timer <= 0
end
def draw
$characters.draw(
source: @sprite_position,
destination: @destination
)
end
private
def toggle_sprite
@timer = 0.25 + (rand * 0.5)
if @sprite_position.x % 48 == 0
@sprite_position.x += 24
else
@sprite_position.x -= 24
end
end
def default_sprite_position
x, y = case @colour
when :green
[0, 0]
when :blue
[48, 0]
when :pink
[96, 0]
when :yellow
[144, 0]
when :cream
[0, 24]
end
@sprite_position = Rectangle.new(x, y, 24, 24)
end
end
class Tilemap
def initialize(image:, size:)
@image = Image.load(image)
@size = size
end
def tile_count
(@image.width / @size) * (@image.height / @size)
end
def tiles_per_row
@image.width / @size
end
def tile_for(id)
x = id % tiles_per_row
y = (id / tiles_per_row.to_f).floor
Rectangle.new(x * @size, y * @size, @size, @size)
end
def generate_from(csv)
start_time = Time.now
ids = File.read(csv).each_line.map { |line| line.split(',').map(&:to_i) }
@width = ids.first.size
@height = ids.size
map = Image.generate(width: @width * 18, height: @height * 18, colour: BLUE)
ids.each.with_index { |row, y|
row.each.with_index { |id, x|
unless id.negative?
map.draw!(
image: @image,
source: tile_for(id),
destination: Rectangle.new(x * @size, y * @size, @size, @size)
)
end
}
}
puts "INFO: Generated map from #{csv} in #{Time.now - start_time} seconds"
map
end
end
$chars = 1000.times.map { Character.new }
$text_position = Vector2.new(20, 20)
$tilemap = Tilemap.new(image: './assets/tiles.png', size: 18)
$map = $tilemap.generate_from('./assets/map.csv').to_texture
def main
delta = get_frame_time
if get_fps > 60
50.times { $chars << Character.new }
elsif get_fps < 55
5.times { $chars.shift }
end
$chars.each { |char| char.update(delta) }
drawing do
clear
$map.draw(destination: Rectangle.new(-14, 480 - $map.height, $map.width, $map.height))
$chars.each(&:draw)
$pixel_font.draw("FPS: #{get_fps}\nAliens: #{$chars.size}", size: $pixel_font.base_size, position: $text_position)
end
end
if browser?
set_main_loop 'main'
else
# Detect window close button or ESC key
main until window_should_close?
end
close_audio_device
close_window
# This is left intentionally blank
# Adapted from https://github.com/raysan5/raylib/blob/master/examples/core/core_input_gamepad.c
# NOTE: This example requires a Gamepad connected to the system
# raylib is configured to work with the following gamepads:
# - Xbox 360 Controller (Xbox 360, Xbox One)
# - PLAYSTATION(R)3 Controller
# NOTE: Gamepad name ID depends on drivers and OS
XBOX360_NAME_IDS = [
"Xbox Controller",
"Microsoft X-Box 360 pad",
"Xbox 360 Controller"
]
PS3_NAME_ID = "PLAYSTATION(R)3 Controller"
$triangle_left = Vector2.new(436, 168)
$triangle_right = Vector2.new(436, 185)
$triangle_top = Vector2.new(464, 177)
# Initialization
screen_width = 800
screen_height = 450
set_config_flags(FLAG_MSAA_4X_HINT) # Set MSAA 4X hint before windows creation
init_window(screen_width, screen_height, "raylib [core] example - gamepad input")
$ps3_pad = Texture2D.load("assets/ps3.png")
$xbox_pad = Texture2D.load("assets/xbox.png")
set_target_fps(60) # Set our game to run at 60 frames-per-second
# Main game loop
def main
# Update
# Draw
drawing do
clear_background(RAYWHITE)
if gamepad_available?(0)
gamepad_name = get_gamepad_name(0)
draw_text("GP1: #{gamepad_name}", 10, 10, 10, BLACK)
if XBOX360_NAME_IDS.include?(gamepad_name)
draw_texture($xbox_pad, 0, 0, DARKGRAY)
# Draw buttons: xbox home
draw_circle(394, 89, 19, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_MIDDLE)
# Draw buttons: basic
draw_circle(436, 150, 9, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_MIDDLE_RIGHT)
draw_circle(352, 150, 9, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_MIDDLE_LEFT)
draw_circle(501, 151, 15, BLUE) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)
draw_circle(536, 187, 15, LIME) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)
draw_circle(572, 151, 15, MAROON) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)
draw_circle(536, 115, 15, GOLD) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_FACE_UP)
# Draw buttons: d-pad
draw_rectangle(317, 202, 19, 71, BLACK)
draw_rectangle(293, 228, 69, 19, BLACK)
draw_rectangle(317, 202, 19, 26, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_FACE_UP)
draw_rectangle(317, 202 + 45, 19, 26, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN)
draw_rectangle(292, 228, 25, 19, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)
draw_rectangle(292 + 44, 228, 26, 19, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)
# Draw buttons: left-right back
draw_circle(259, 61, 20, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_TRIGGER_1)
draw_circle(536, 61, 20, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)
# Draw axis: left joystick
draw_circle(259, 152, 39, BLACK)
draw_circle(259, 152, 34, LIGHTGRAY)
draw_circle(259 + (get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_X)*20),
152 + (get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_Y)*20), 25, BLACK)
# Draw axis: right joystick
draw_circle(461, 237, 38, BLACK)
draw_circle(461, 237, 33, LIGHTGRAY)
draw_circle(461 + (get_gamepad_axis_movement(0, GAMEPAD_AXIS_RIGHT_X)*20),
237 + (get_gamepad_axis_movement(0, GAMEPAD_AXIS_RIGHT_Y)*20), 25, BLACK)
# Draw axis: left-right triggers
draw_rectangle(170, 30, 15, 70, GRAY)
draw_rectangle(604, 30, 15, 70, GRAY)
draw_rectangle(170, 30, 15, (((1 + get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_TRIGGER))/2)*70), RED)
draw_rectangle(604, 30, 15, (((1 + get_gamepad_axis_movement(0, GAMEPAD_AXIS_RIGHT_TRIGGER))/2)*70), RED)
elsif PS3_NAME_ID == gamepad_name
draw_texture($ps3_pad, 0, 0, DARKGRAY)
# Draw buttons: ps
draw_circle(396, 222, 13, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_MIDDLE)
# Draw buttons: basic
draw_rectangle(328, 170, 32, 13, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_MIDDLE_LEFT)
draw_triangle($triangle_left, $triangle_right, $triangle_top, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_MIDDLE_RIGHT)
draw_circle(557, 144, 13, LIME) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_FACE_UP)
draw_circle(586, 173, 13, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)
draw_circle(557, 203, 13, VIOLET) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)
draw_circle(527, 173, 13, PINK) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)
# Draw buttons: d-pad
draw_rectangle(225, 132, 24, 84, BLACK)
draw_rectangle(195, 161, 84, 25, BLACK)
draw_rectangle(225, 132, 24, 29, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_FACE_UP)
draw_rectangle(225, 132 + 54, 24, 30, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN)
draw_rectangle(195, 161, 30, 25, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)
draw_rectangle(195 + 54, 161, 30, 25, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)
# Draw buttons: left-right back buttons
draw_circle(239, 82, 20, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_LEFT_TRIGGER_1)
draw_circle(557, 82, 20, RED) if gamepad_button_down?(0, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)
# Draw axis: left joystick
draw_circle(319, 255, 35, BLACK)
draw_circle(319, 255, 31, LIGHTGRAY)
draw_circle(319 + (get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_X) * 20),
255 + (get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_Y) * 20), 25, BLACK)
# Draw axis: right joystick
draw_circle(475, 255, 35, BLACK)
draw_circle(475, 255, 31, LIGHTGRAY)
draw_circle(475 + (get_gamepad_axis_movement(0, GAMEPAD_AXIS_RIGHT_X)*20),
255 + (get_gamepad_axis_movement(0, GAMEPAD_AXIS_RIGHT_Y)*20), 25, BLACK)
# Draw axis: left-right triggers
draw_rectangle(169, 48, 15, 70, GRAY)
draw_rectangle(611, 48, 15, 70, GRAY)
draw_rectangle(169, 48, 15, (((1 - get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_TRIGGER)) / 2) * 70), RED)
draw_rectangle(611, 48, 15, (((1 - get_gamepad_axis_movement(0, GAMEPAD_AXIS_RIGHT_TRIGGER)) / 2) * 70), RED)
else
draw_text("- GENERIC GAMEPAD -", 280, 180, 20, GRAY)
# TODO: Draw generic gamepad
end
draw_text("DETECTED AXIS #{get_gamepad_axis_count(0)}", 10, 50, 10, MAROON)
(get_gamepad_axis_count(0) + 1).times do |i|
draw_text("AXIS #{i}: #{get_gamepad_axis_movement(0, i)}", 20, 70 + 20 * i, 10, DARKGRAY)
end
if get_gamepad_button_pressed != -1
draw_text("DETECTED BUTTON: #{get_gamepad_button_pressed}", 10, 430, 10, RED)
else
draw_text("DETECTED BUTTON: NONE", 10, 430, 10, GRAY)
end
else
draw_text("GP1: NOT DETECTED", 10, 10, 10, GRAY)
draw_texture($xbox_pad, 0, 0, LIGHTGRAY)
end
end
end
if browser?
set_main_loop 'main'
else
# Detect window close button or ESC key
main until window_should_close?
end
# De-Initialization
$ps3_pad.unload
$xbox_pad.unload
close_window # Close window and OpenGL context
# Adapted from https://github.com/raysan5/raylib/blob/master/examples/core/core_input_multitouch.c
MAX_GESTURE_STRINGS = 20
# Initialization
$screen_width = 800
$screen_height = 450
init_window($screen_width, $screen_height, "raylib [core] example - input gestures")
$touch_position = Vector2.new(0, 0)
$touch_area = Rectangle.new(220, 10, $screen_width - 230, $screen_height - 20)
$gestures = []
$current_gesture = GESTURE_NONE
$last_gesture = GESTURE_NONE
#set_gestures_enabled(0b0000000000001001) # Enable only some gestures to be detected
set_target_fps(60) # Set our game to run at 60 frames-per-second
# Main game loop
def main
# Update
$last_gesture = $current_gesture;
$current_gesture = get_gesture_detected
$touch_position = get_touch_position(0)
if check_collision_point_rec($touch_position, $touch_area) && $current_gesture != GESTURE_NONE
if $current_gesture != $last_gesture
# Store gesture string
case $current_gesture
when GESTURE_TAP
$gestures.push("GESTURE TAP")
when GESTURE_DOUBLETAP
$gestures.push("GESTURE DOUBLETAP")
when GESTURE_HOLD
$gestures.push("GESTURE HOLD")
when GESTURE_DRAG
$gestures.push("GESTURE DRAG")
when GESTURE_SWIPE_RIGHT
$gestures.push("GESTURE SWIPE RIGHT")
when GESTURE_SWIPE_LEFT
$gestures.push("GESTURE SWIPE LEFT")
when GESTURE_SWIPE_UP
$gestures.push("GESTURE SWIPE UP")
when GESTURE_SWIPE_DOWN
$gestures.push("GESTURE SWIPE DOWN")
when GESTURE_PINCH_IN
$gestures.push("GESTURE PINCH IN")
when GESTURE_PINCH_OUT
$gestures.push("GESTURE PINCH OUT")
end
# Reset gestures strings
if $gestures.size >= MAX_GESTURE_STRINGS
$gestures = []
end
end
end
# Draw
begin_drawing
clear_background(RAYWHITE)
draw_rectangle_rec($touch_area, GRAY)
draw_rectangle(225, 15, $screen_width - 240, $screen_height - 30, RAYWHITE)
draw_text("GESTURES TEST AREA", $screen_width - 270, $screen_height - 40, 20, fade(GRAY, 0.5))
$gestures.each_with_index do |gesture, index|
if index % 2 == 0
draw_rectangle(10, 30 + 20 * index, 200, 20, fade(LIGHTGRAY, 0.5))
else
draw_rectangle(10, 30 + 20 * index, 200, 20, fade(LIGHTGRAY, 0.3))
end
if (index < $gestures.size - 1)
draw_text($gestures[index], 35, 36 + 20 * index, 10, DARKGRAY)
else
draw_text($gestures[index], 35, 36 + 20 * index, 10, MAROON)
end
end
draw_rectangle_lines(10, 29, 200, $screen_height - 50, GRAY);
draw_text("DETECTED GESTURES", 50, 15, 10, GRAY);
draw_circle_v($touch_position, 30, MAROON) if $current_gesture != GESTURE_NONE
end_drawing
end
if browser?
set_main_loop 'main'
else
# Detect window close button or ESC key
main until window_should_close?
end
# De-Initialization
close_window # Close window and OpenGL context
# Open up a window
init_window(800, 480, "Taylor Application")
# Setup audio so we can play sounds
init_audio_device
# Get the current monitor frame rate and set our target framerate to match.
set_target_fps(get_monitor_refresh_rate(get_current_monitor))
$mouse_outline = Rectangle.new(285, 70, 230, 340)
$left_button = Rectangle.new(295, 80, 80, 120)
$right_button = Rectangle.new(425, 80, 80, 120)
$scroll_wheel = Rectangle.new(385, 90, 30, 100)
$scroll_wheel_up = Rectangle.new(387, 92, 26, 46)
$scroll_wheel_down = Rectangle.new(387, 142, 26, 46)
# Define your main method
def main
# Get the amount of time passed since the last frame was rendered
delta = get_frame_time
mouse_position = get_mouse_position
# This doesn't work on web
set_mouse_position(10, 10) if mouse_button_down?(MOUSE_LEFT_BUTTON)
# Your update logic goes here
drawing do
# Your drawing logic goes here
clear
draw_text(
"Mouse: #{mouse_position.x}x#{mouse_position.y}",
10, 10, 20, DARKGRAY
)
$mouse_outline.draw(outline: true, rounded: true, radius: 0.2, colour: BLACK)
$left_button.draw(outline: false, rounded: true, colour: RED) if mouse_button_down?(MOUSE_LEFT_BUTTON)
$left_button.draw(outline: true, rounded: true, colour: BLACK)
$right_button.draw(outline: false, rounded: true, colour: RED) if mouse_button_down?(MOUSE_RIGHT_BUTTON)
$right_button.draw(outline: true, rounded: true, colour: BLACK)
$scroll_wheel.draw(outline: false, rounded: true, colour: RED) if mouse_button_down?(MOUSE_MIDDLE_BUTTON)
$scroll_wheel_up.draw(outline: false, rounded: true, colour: GREEN) if get_mouse_wheel_move > 0
$scroll_wheel_down.draw(outline: false, rounded: true, colour: GREEN) if get_mouse_wheel_move < 0
$scroll_wheel.draw(outline: true, rounded: true, colour: BLACK)
end
end
if browser?
set_main_loop 'main'
else
# Detect window close button or ESC key
main until window_should_close?
end
close_audio_device
close_window
# Adapted from https://github.com/raysan5/raylib/blob/master/examples/core/core_2d_camera_platformer.c
GRAVITY = 400
PLAYER_JUMP_SPD = 350.0
PLAYER_HOR_SPD = 200.0
class Player
attr_accessor :position, :speed, :can_jump
alias :can_jump? :can_jump
def initialize(position, speed)
@position = position
@speed = speed
@can_jump = false
end
end
class EnvItem
attr_accessor :rectangle, :blocking, :colour
def initialize(rectangle, blocking, colour)
@rectangle = rectangle
@blocking = blocking
@colour = colour
end
end
def update_player(player, env_items, delta)
player.position.x -= PLAYER_HOR_SPD * delta if key_down?(KEY_LEFT)
player.position.x += PLAYER_HOR_SPD*delta if key_down?(KEY_RIGHT)
if (key_down?(KEY_SPACE) && player.can_jump?)
player.speed = -PLAYER_JUMP_SPD
player.can_jump = false
end
hit_obstacle = false
env_items.each { |item|
if (item.blocking &&
item.rectangle.x <= player.position.x &&
item.rectangle.x + item.rectangle.width >= player.position.x &&
item.rectangle.y >= player.position.y &&
item.rectangle.y < player.position.y + player.speed * delta)
hit_obstacle = true
player.speed = 0
player.position.y = item.rectangle.y
end
}
if hit_obstacle
player.can_jump = true
else
player.position.y += player.speed * delta
player.speed += GRAVITY * delta
player.can_jump = false
end
end
# Initialization
$screen_width = 800;
$screen_height = 450;
init_window($screen_width, $screen_height, "raylib [core] example - 2d camera platformer");
$player = Player.new(Vector2.new(400, 280), 0)
$env_items = [
EnvItem.new(Rectangle.new(0, 0, 1000, 400 ), 0, LIGHTGRAY),
EnvItem.new(Rectangle.new(0, 400, 1000, 200 ), 1, GRAY),
EnvItem.new(Rectangle.new(300, 200, 400, 10 ), 1, GRAY),
EnvItem.new(Rectangle.new(250, 300, 100, 10 ), 1, GRAY),
EnvItem.new(Rectangle.new(650, 300, 100, 10 ), 1, GRAY),
]
$camera = Camera2D.new(
$player.position,
Vector2.new($screen_width/2.0, $screen_height/2.0),
0, 1,
)
def update_camera_center(camera, player, envItems, delta, width, height)
camera.offset.x = width / 2.0
camera.offset.y = height / 2.0
camera.target.x = player.position.x
camera.target.y = player.position.y
end
def update_camera_inside_map(camera, player, env_items, delta, width, height)
camera.target.x = player.position.x
camera.target.y = player.position.y
camera.offset.x = width / 2.0
camera.offset.y = height / 2.0
min_x = env_items.map { |item| item.rectangle.x }.min
min_y = env_items.map { |item| item.rectangle.y }.min
max_x = env_items.map { |item| item.rectangle.x + item.rectangle.width }.max
max_y = env_items.map { |item| item.rectangle.y + item.rectangle.height }.max
max = get_world_to_screen2D(Vector2.new(max_x, max_y), camera)
min = get_world_to_screen2D(Vector2.new(min_x, min_y), camera)
camera.offset.x = width - (max.x - width / 2) if (max.x < width)
camera.offset.y = height - (max.y - height / 2) if (max.y < height)
camera.offset.x = width / 2 - min.x if (min.x > 0)
camera.offset.y = height / 2 - min.y if (min.y > 0)
end
def update_camera_smooth_follow(camera, player, envItems, delta, width, height)
min_speed = 30
min_effect_length = 10
fraction_speed = 0.8
camera.offset.x = width / 2.0
camera.offset.y = height / 2.0
diff = player.position - camera.target
length = diff.length
if (length > min_effect_length)
speed = [fraction_speed * length, min_speed].max
camera.target += diff.scale(speed * delta / length)
end
end
EVEN_OUT_SPEED = 700
EVENING_OUT = false
EVEN_OUT_TARGET = $player.position.y
def update_camera_even_out_on_landing(camera, player, envItems, delta, width, height)
camera.offset.x = width / 2.0
camera.offset.y = height / 2.0
camera.target.x = player.position.x;
if EVENING_OUT
if EVEN_OUT_TARGET > camera.target.y
camera.target.y += EVEN_OUT_SPEED * delta
if camera.target.y > EVEN_OUT_TARGET
camera.target.y = EVEN_OUT_TARGET
EVENING_OUT = false
end
else
camera.target.y -= EVEN_OUT_SPEED * delta
if (camera.target.y < EVEN_OUT_TARGET)
camera.target.y = EVEN_OUT_TARGET
EVENING_OUT = false
end
end
else
if player.can_jump? && player.speed == 0 && player.position.y != camera.target.y
EVENING_OUT = true
EVEN_OUT_TARGET = player.position.y
end
end
end
def update_camera_player_bounds_push(camera, player, envItems, delta, width, height)
bbox = Vector2.new(0.2, 0.2)
bbox_world_min = get_screen_to_world2D(Vector2.new((1 - bbox.x) * 0.5 * width, (1 - bbox.y) * 0.5 * height), camera)
bbox_world_max = get_screen_to_world2D(Vector2.new((1 + bbox.x) * 0.5 * width, (1 + bbox.y) * 0.5 * height), camera)
camera.offset = Vector2.new((1 - bbox.x) * 0.5 * width, (1 - bbox.y) * 0.5 * height)
camera.target.x = player.position.x if (player.position.x < bbox_world_min.x)
camera.target.y = player.position.y if (player.position.y < bbox_world_min.y)
camera.target.x = bbox_world_min.x + (player.position.x - bbox_world_max.x) if (player.position.x > bbox_world_max.x)
camera.target.y = bbox_world_min.y + (player.position.y - bbox_world_max.y) if (player.position.y > bbox_world_max.y)
end
# call the currently selected camera option
def camera_updaters(option, camera, player, env_items, delta, width, height)
case option
when 0
update_camera_center(camera, player, env_items, delta, width, height)
when 1
update_camera_inside_map(camera, player, env_items, delta, width, height)
when 2
update_camera_smooth_follow(camera, player, env_items, delta, width, height)
when 3
update_camera_even_out_on_landing(camera, player, env_items, delta, width, height)
when 4
update_camera_player_bounds_push(camera, player, env_items, delta, width, height)
end
end
$camera_option = 3
$camera_descriptions = [
"Follow player center",
"Follow player center, but clamp to map edges",
"Follow player center; smoothed",
"Follow player center horizontally; updateplayer center vertically after landing",
"Player push camera on getting too close to screen edge",
]
set_target_fps(60);
# Main game loop
def main
# Update
delta = get_frame_time
update_player($player, $env_items, delta)
$camera.zoom += get_mouse_wheel_move * 0.05
if $camera.zoom > 3
$camera.zoom = 3.0
elsif $camera.zoom < 0.25
$camera.zoom = 0.25
end
if key_pressed?(KEY_R)
$camera.zoom = 1.0
$player.position.x = 400
$player.position.y = 280
end
if key_pressed?(KEY_C)
$camera_option += 1
$camera_option = 0 if $camera_option >= $camera_descriptions.length
end
# Call update camera function
camera_updaters($camera_option, $camera, $player, $env_items, delta, $screen_width, $screen_height)
# Draw
begin_drawing
clear_background(LIGHTGRAY)
begin_mode2D($camera)
$env_items.each { |item| draw_rectangle_rec(item.rectangle, item.colour) }
draw_rectangle($player.position.x - 20, $player.position.y - 40, 40, 40, RED)
end_mode2D
draw_text("Controls:", 20, 20, 10, BLACK)
draw_text("- Right/Left to move", 40, 40, 10, DARKGRAY)
draw_text("- Space to jump", 40, 60, 10, DARKGRAY)
draw_text("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, DARKGRAY)
draw_text("- C to change camera mode", 40, 100, 10, DARKGRAY)
draw_text("Current camera mode:", 20, 120, 10, BLACK)
draw_text($camera_descriptions[$camera_option], 40, 140, 10, DARKGRAY)
end_drawing
end
if browser?
set_main_loop 'main'
else
# Detect window close button or ESC key
main until window_should_close?
end
# De-Initialization
close_window # Close window and OpenGL context
# Adapted from https://github.com/raysan5/raylib/blob/master/examples/textures/textures_srcrec_dstrec.c
#
# Art used in this example is provided under a free and permissive license.
# - [Scarfy spritesheet](scarfy.png) by [Eiden Marsal](https://www.artstation.com/artist/marshall_z), licensed as [Creative Commons Attribution-NonCommercial 3.0](https://creativecommons.org/licenses/by-nc/3.0/legalcode)
# Initialization
$screen_width = 800
$screen_height = 450
init_window($screen_width, $screen_height, "raylib [textures] examples - texture source and destination rectangles")
# NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
$scarfy = Texture2D.load("assets/scarfy.png")
$frame_width = $scarfy.width / 6
$frame_height = $scarfy.height
# Source rectangle (part of the texture to use for drawing)
$source = Rectangle.new(0, 0, $frame_width, $frame_height)
# Destination rectangle (screen rectangle where drawing part of texture)
$destination = Rectangle.new(
$screen_width / 2.0, $screen_height / 2.0, $frame_width * 2, $frame_height * 2
)
# Origin of the texture (rotation/scale point), it's relative to destination rectangle size
$origin = Vector2.new($frame_width, $frame_height)
$rotation = 0
set_target_fps(60)
$frame_counter = 0
# Main game loop
def main
# Update
$rotation += 1
$frame_counter += 1
# Every 10 frames let's change frame of our animation
if $frame_counter % 10 == 0
$source.x += $frame_width
# Reset the animation when we get to the end
$source.x = 0 if $source.x >= $scarfy.width
end
# Draw
drawing do
clear_background(RAYWHITE)
# NOTE: Using Texture2D.draw() we can easily rotate and scale the part of the texture we draw
# source: defines the part of the texture we use for drawing
# destination: defines the rectangle where our texture part will fit (scaling it to fit)
# origin: defines the point of the texture used as reference for rotation and scaling
# rotation: defines the texture rotation (using origin as rotation point)
$scarfy.draw(
source: $source,
destination: $destination,
origin: $origin,
rotation: $rotation
)
draw_line($destination.x, 0, $destination.x, $screen_height, GRAY)
draw_line(0, $destination.y, $screen_width, $destination.y, GRAY)
draw_text("(c) Scarfy sprite by Eiden Marsal", $screen_width - 200, $screen_height - 20, 10, GRAY)
end
end
if browser?
set_main_loop 'main'
else
# Detect window close button or ESC key
main until window_should_close?
end
# De-Initialization
$scarfy.unload
close_window # Close window and OpenGL context
# Open up a window
init_window(800, 480, "Taylor Application")
# Setup audio so we can play sounds
init_audio_device
# Get the current monitor frame rate and set our target framerate to match.
set_target_fps(get_monitor_refresh_rate(get_current_monitor))
# Define your main method
def main
# Get the amount of time passed since the last frame was rendered
delta = get_frame_time
# Your update logic goes here
drawing do
# Your drawing logic goes here
clear
draw_text(
"Welcome to your first Taylor application!",
190, 200, 20, DARKGRAY
)
end
end
if browser?
set_main_loop 'main'
else
# Detect window close button or ESC key
main until window_should_close?
end
close_audio_device
close_window