[Cursor] Majora's Mask / Project Zelda Engine cursor

Started by MetalZelda, June 25, 2017, 12:59:44 PM

Previous topic - Next topic
June 25, 2017, 12:59:44 PM Last Edit: June 25, 2017, 02:07:24 PM by MetalZelda
This is a straight port of a RUBY script. Made by MaximusMaxy, from Project Zelda Engine
This script allow to have an animated cursor for your pause menu, like Majora's Mask, of course, it can be adapted for other stuffs

This is an alternative solution for avoiding a spriteset creation (frame by frame cursor animation), this script calculate the animation for you.

You need:

-> a cursor png (1 part or 4 sides) which you should save in /sprites/menus/src/pause_submenus/cursor.png (You can change the file bellow
-> using 4 side cursor ? Just set use_rotation to false
-> Using 1 part cursor ? Set use_rotation to true

What can you do ?

-> You can modify the radius of the circle (minimum, maximum)
-> You can modify the speed of the animation (speed, where 1 = super slow and 9 = 1 lightyear per sanic)
-> Disable the rotation system and only keep the offset system
-> Disable the fading animation that occurs when the animation is drawn

Code (lua) Select
function submenu:create_cursor()
  -- Set the minimum and maximum radius of the rotation
  local minimum, maximum = 10, 12
 
  -- Speed of the animation (1 = slow, 9 = fast)
  local speed = 5
 
  -- Use the rotation system or just the zoom ? (true = use the rotation, false = just the zoom)
  local use_rotation = true
 
  -- Use the fading option
  local use_fade = true
 
  -- Cursor file
  local cursor = sol.surface.create("menus/src/pause_submenus/cursor.png")
  local cursor_width, cursor_height = cursor:get_size()
 
  -- Determine the region parts depending on use_rotation state
  if not use_rotation then
    cursor_width = cursor_width / 2
    cursor_height = cursor_height / 2
  end
 
  -- Don't touch this
  local count = 0
 
  function submenu:draw_cursor(dst_surface, x, y)
    -- Increment the rotation
    count = (count + speed) % 360
 
    -- Compute the sin and cos of the rotation
    local sin = math.sin(count * math.pi / 180)
    local cos = math.cos(count * math.pi / 180)
 
    -- Compute the zoom scaling
    local offset = minimum + (maximum - minimum) / 2 + (maximum - minimum) / 2 * sin

-- Update the cursor opacity
if use_fade then
  cursor:set_opacity(205 - (0.5 + sin) * 100)
end

-- Not using the rotation system, no rotation
if not use_rotation then
  cos, sin = 1, 1
end

local rotation_w = use_rotation and 0 or cursor_width
local rotation_h = use_rotation and 0 or cursor_height

-- Draw the cursors
-- Upper Right
    cursor:draw_region(rotation_w, 0, cursor_width, cursor_height, dst_surface, x + (sin * offset), y + (cos * -offset))

-- Lower Left
    cursor:draw_region(0, rotation_h, cursor_width, cursor_height, dst_surface, x + (sin * -offset), y + (cos * offset))

-- Lower Right
    cursor:draw_region(rotation_w, rotation_h, cursor_width, cursor_height, dst_surface, x + (cos * offset), y + (sin * offset))

-- Upper Left
    cursor:draw_region(0, 0, cursor_width, cursor_height, dst_surface, x + (cos * -offset), y + (sin * -offset))
  end
end
submenu:create_cursor()


This is plug and play, at least if you use the MoS submenu system, you just have to call submenu:draw_cursor(dst_surface, x, y) in yout submenu script

You can also delete the rotation system for a zooming cursor, all you need to zoom is the local "offset"