[Resolved] Small edit of this Stamina script so it follows the player on screen

Started by assi23q, October 12, 2017, 12:11:49 pm

Previous topic - Next topic

assi23q

Hi, so I've been using this Stamina Script on my game for a while, but I have adjusted it to display the stamina bar right underneath the player so the player can easily visualize how much stamina they have without having to look at the edge of the screen (similar to how BOTW handles the stamina bar). The only problem is that the coordinates are set positions of the middle of the map, so when the player is on the edge the stamina bar stays down while the player moves off it. Can anyone help me fix this script so it follows the player.


#===============================================================================
# Stamina System
# By Jet10985 (Jet)
# Help by: Mithran
#===============================================================================
# This snippet will add a stamina system to your game. Stamina, meaning that
# when the player dashes, they will lose stamina. When the stamina runs out
# the player will not be able to dash until it is re-charged.
# This script has: 20 customization options.
#===============================================================================
# Overwritten Methods:
# None
#-------------------------------------------------------------------------------
# Aliased methods:
# Game_Character: jump, increase_steps
# Scene_Map: start, update, terminate
# Game_Player: dash?
# Scene_Menu: start, update, terminate
# Scene_File: write_save_data, read_save_data
#===============================================================================

=begin

How to use:

To increase the max stamina level, you can use this event "Script..." command:

increase_max_stamina(amount)
amount = the amount you want to increase max stamina by

To stop stamina loss, therefore giving infinite stamina while on, use this:

stamina_loss(option)
option = either true or false. True gives them infinite stamina, false makes
the stamina go donw once again.

=end


module Jet_Stamina

# Use an icon or a letter to show with the item gauge?
USE_LETTER = true

# Icon id if you chose false in the above config.
ICON_STAMINA_ID = 7443

# This the the letters that will be displayed with the stamina bar to
# represent that the bar is for stamina.
STAMINA_INITIAL = ""

# These are the level up of stamina. By default, it has 4 specified.
# these represent levels 1 through 4 of the stamina. The level where it
# comes from is derived from a below configuration.
STAMINA_LEVELS = [50, 100, 100, 100]

# This actor's level will determine the stamina level set above.
ACTOR_STAMINA_ID = 0

# This is how much the stamina level will raise for any unspecified levels
# in the above configuration.
BASE_STAMINA_RAISE = 0

# This is how much stamina will be drained per square moved will dashing.
STAMINA_DOWN_PER_SQUARE = 0.5

# This is how much stamina will be re-gained every second when not moving.
STAMINA_REGEN_PER_SECOND = 2

# This is a variable that will hold the current amount of stamina.
STAMINA_TO_VARIABLE_ID = 55

# Show the stamina on the map?
STAMINA_ON_MAP = true

# This is where the window shall be shown if the window is shown on the map.
MAP_WINDOW_COORDS = [228, 194] #386, 50

# This is the switch that if on, the map's window will be visible.
MAP_ONOFF_SWITCH = 61

# This is how transperant the map's stamina window is.
# 0 is just the bar, no window, 255 is a completely solid window.
MAP_WINDOW_OPACITY = 0

# Show stamina in the menu?
STAMINA_IN_MENU = true

# Do you want them to regenerate stamina while in the menu?
UPDATE_STAMINA_IN_MENU = true

# This is where the window shall be shown if the window is shown on the menu.
MENU_WINDOW_COORDS = [0, 360]

# Show numbers with the stamina bar, or just the bar?
STAMINA_BAR_WITH_NUMBERS = false

# These are the stamina bar's colors. By default it is goldenrod/dark goldenrod.
STAMINA_GAUGE_COLOR1 = Color.new(184, 134, 11)
STAMINA_GAUGE_COLOR2 = Color.new(218, 165, 32)

# If you are using a jump system, this will drain some stamina each time
# that the player jumps
JUMP_SYSTEM_COMPATABILITY = false

# This is how much stamina will be lost for each jump.
STAMINA_JUMP_LOSS = 6

end

#===============================================================================
# DON'T EDIT FURTHER UNLESS YOU KNOW WHAT TO DO.
#===============================================================================
class Game_Stamina

include Jet_Stamina

attr_accessor :stamina
attr_accessor :max_stamina
attr_accessor :stamina_gain
attr_accessor :max_stamina_plus
attr_accessor :disable_stamina_loss
attr_accessor :got_initial_stamina

def initialize
@stamina = 0
@stamina_gain = 0
@max_stamina = 0
@max_stamina_plus = 0
@disable_stamina_loss = true
@got_initial_stamina = false
end

def update_stamina
get_max_stamina
$game_stamina.stamina_gain += 1 unless $game_player.moving? && $game_player.dash?
if $game_stamina.stamina_gain == 60
$game_stamina.stamina += STAMINA_REGEN_PER_SECOND
$game_stamina.stamina_gain = 0
if $game_stamina.stamina > $game_stamina.max_stamina
$game_stamina.stamina = $game_stamina.max_stamina
end
end
$game_variables[STAMINA_TO_VARIABLE_ID] = $game_stamina.stamina
end

def get_initial_stamina
if $game_party.members[ACTOR_STAMINA_ID].level < STAMINA_LEVELS.size
$game_stamina.stamina = STAMINA_LEVELS[$game_party.members[ACTOR_STAMINA_ID].level - 1]
else
$game_stamina.stamina = STAMINA_LEVELS.max + (($game_party.members[ACTOR_STAMINA_ID].level - (STAMINA_LEVELS.size - 1)) * BASE_STAMINA_RAISE)
end
end

def get_max_stamina
if $game_party.members[ACTOR_STAMINA_ID].level > STAMINA_LEVELS.size
$game_stamina.max_stamina = STAMINA_LEVELS.max + (($game_party.members[ACTOR_STAMINA_ID].level - (STAMINA_LEVELS.size - 1)) * BASE_STAMINA_RAISE) + $game_stamina.max_stamina_plus
else
$game_stamina.max_stamina = STAMINA_LEVELS[$game_party.members[ACTOR_STAMINA_ID].level - 1] + $game_stamina.max_stamina_plus
end
return $game_stamina.max_stamina
end
end

$game_stamina = Game_Stamina.new

class Game_Interpreter

def stamina_loss(option)
if option == true || option == false
$game_stamina.disable_stamina_loss = option
else
p "The option you chose was neither true or false. Please error check and try again"
end
end

def increase_max_stamina(amount)
$game_stamina.max_stamina_plus += amount
end
end

class Game_Character

include Jet_Stamina

if JUMP_SYSTEM_COMPATABILITY
alias jet5902_jump jump unless $@
def jump(*args)
$game_stamina.stamina -= STAMINA_JUMP_LOSS
jet5902_jump(*args)
end
end

alias jet9211_increase_steps increase_steps unless $@
def increase_steps(*args)
$game_stamina.stamina -= STAMINA_DOWN_PER_SQUARE if $game_player.dash? && $game_stamina.disable_stamina_loss && self.is_a?(Game_Player)
jet9211_increase_steps(*args)
end
end

class Window_Stamina < Window_Base

def initialize(x, y)
super(x, y, 160, WLH + 32) #160 32
self.opacity = MAP_WINDOW_OPACITY if $scene.is_a?(Scene_Map)
refresh
end

def refresh
self.contents.clear
draw_actor_stamina(0, 0)
end

def update
refresh
end
end

class Window_Base

include Jet_Stamina

def stamina_color
return crisis_color if $game_stamina.stamina < $game_stamina.get_max_stamina / 4
return normal_color
end

def draw_actor_stamina(x, y, width = 120)
draw_actor_stamina_gauge(x, y, width)
self.contents.font.color = system_color
if USE_LETTER
self.contents.draw_text(x, y, 30, WLH, STAMINA_INITIAL)
else
draw_icon(ICON_STAMINA_ID, x, y)
end
self.contents.font.color = stamina_color
last_font_size = self.contents.font.size
xr = x + width
if STAMINA_BAR_WITH_NUMBERS
if width < 120
self.contents.draw_text(xr - 44, y, 44, WLH, $game_stamina.stamina, 2)
else
self.contents.draw_text(xr - 99, y, 44, WLH, $game_stamina.stamina, 2)
self.contents.font.color = normal_color
self.contents.draw_text(xr - 55, y, 11, WLH, "/", 2)
self.contents.draw_text(xr - 44, y, 44, WLH, $game_stamina.get_max_stamina, 2)
end
end
end

def draw_actor_stamina_gauge(x, y, width = 120)
gw = width * ($game_stamina.stamina / $game_stamina.max_stamina) / 2.2
gc1 = STAMINA_GAUGE_COLOR1
gc2 = STAMINA_GAUGE_COLOR2
self.contents.fill_rect(x, y + WLH - 8, width/2.2, 6, gauge_back_color)
self.contents.gradient_fill_rect(x, y + WLH - 8, gw, 6, gc1, gc2)
end
end

class Scene_Map

include Jet_Stamina

alias jet0221_start start unless $@
def start
$game_stamina.update_stamina
unless $game_stamina.got_initial_stamina
$game_stamina.get_initial_stamina
$game_stamina.got_initial_stamina = true
end
@stamina_window = Window_Stamina.new(MAP_WINDOW_COORDS[0], MAP_WINDOW_COORDS[1]) if STAMINA_ON_MAP
@stamina_window.visible = false
jet0221_start
end

alias jet5021_update update unless $@
def update
$game_stamina.update_stamina
if !$game_switches[MAP_ONOFF_SWITCH] && STAMINA_ON_MAP
@stamina_window.visible = false
elsif STAMINA_ON_MAP
@stamina_window.visible = true
end
@stamina_window.update if STAMINA_ON_MAP
if @stamina_window.visible && !$game_player.dash?
@stamina_window.opacity -= 1
if @stamina_window.opacity == 3
@stamina_window.openness -= 1
end
elsif @stamina_window.visible && $game_player.dash?
@stamina_window.opacity += 1 unless @stamina_window.opacity == 5
@stamina_window.openness += 1 unless @stamina_window.openness == 5
end
jet5021_update
end

alias jet2048_terminate terminate unless $@
def terminate
@stamina_window.dispose if STAMINA_ON_MAP
jet2048_terminate
end
end

class Game_Player

alias jet6902_dash? dash? unless $@
def dash?
return false if $game_stamina.stamina < Jet_Stamina::STAMINA_DOWN_PER_SQUARE
jet6902_dash?
end
end

class Scene_Menu

include Jet_Stamina

alias jet5893_start start unless $@
def start
jet5893_start
@stamina_window = Window_Stamina.new(MENU_WINDOW_COORDS[0], MENU_WINDOW_COORDS[1]) if STAMINA_IN_MENU
end

alias jet6942_update update unless $@
def update
jet6942_update
$game_stamina.update_stamina if UPDATE_STAMINA_IN_MENU
@stamina_window.update if STAMINA_IN_MENU
end

alias jet7692_terminate terminate unless $@
def terminate
@stamina_window.dispose if STAMINA_IN_MENU
jet7692_terminate
end
end

class Scene_File

alias jet3891_write_save_data write_save_data unless $@
def write_save_data(file)
jet3891_write_save_data(file)
Marshal.dump($game_stamina, file)
end

alias jet5931_read_save_data read_save_data unless $@
def read_save_data(file)
jet5931_read_save_data(file)
$game_stamina = Marshal.load(file)
end
end

unless $engine_scripts.nil?
JetEngine.active("Stamina System", 1)
end



My solution is to make x and y variables of the player and set them at the MAP_WINDOW_COORDS, but I have no idea how to go about this since I've never actually used Ruby before. Also not sure if this will keep updating while the player is moving or if it only sets itself once.

Thanks for any help

KK20

Nobody uses VX around here, so your help will be limited.

You are right that you will need to update the stamina window's coordinates based on the Game Player's sprite coordinates. Where this should be done is within Scene_Map#update:

alias jet5021_update update unless $@
def update
$game_stamina.update_stamina
if !$game_switches[MAP_ONOFF_SWITCH] && STAMINA_ON_MAP
@stamina_window.visible = false
elsif STAMINA_ON_MAP
@stamina_window.visible = true
end

#==========================================
if STAMINA_ON_MAP
  @stamina_window.x = # Game_Player sprite X + offset
  @stamina_window.y = # Game_Player sprite Y + offset
  @stamina_window.update
end
#==========================================

if @stamina_window.visible && !$game_player.dash?
@stamina_window.opacity -= 1
if @stamina_window.opacity == 3
@stamina_window.openness -= 1
end
elsif @stamina_window.visible && $game_player.dash?
@stamina_window.opacity += 1 unless @stamina_window.opacity == 5
@stamina_window.openness += 1 unless @stamina_window.openness == 5
end
jet5021_update
end

You will need to figure out what methods fill in the blanks.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

assi23q

Thanks heaps!
I added in

#========================================== 
if STAMINA_ON_MAP
  @stamina_window.x = $game_player.screen_x-45
  @stamina_window.y = $game_player.screen_y-27
  @stamina_window.update
end
#==========================================


And it works perfectly!
Here's a little screenshot to show it off  :D :D