Allora questo script l'ho realizzato questa notte e l'autore sono io. Ne vado fiero perché è il mio primo vero script nella
sezione RGSS.
Premessa:
Tutto nasce dal fatto che volevo capire come funzionasse il discorso dei template dei charaset, quale fosse lo schema
che permetteva di decidere le proporzioni (tra Frame e Pose). Per invogliarmi a studiare la cosa ho creato un tutorial
che spiega passo passo come sono arrivato a realizzare questo script che penso sia molto più intuitivo di certuni che
si vedono in giro. (quelli che usano le SDK mi terrorizzano O.O'' ) Questo invece è puro codice di default, comunque vi
posto il link del tutorial (che ho finito questa notte verso 00:53) ve lo posto:
http://www.rpg2s.net...xp/#entry412299
Allora vi spiego un pochettino come funge:
Quello che non troverete nel tutorial è un Modulo che faciliterà meglio i settaggi.
L'ho nominato infatti apposta 'Impostazioni' e l'ho messo all'apice dello script.
In sostanza Lui:
############################################################################### module Impostazioni NUMERO_DI_FRAME_CHARACTER = 6 # default: @cw = bitmap.width / 4 NUMERO_DI_POSE_CHARACTER = 8 # default: @ch = bitmap.height / 4 CAMMINATA_8DIR = true # se false abilita quella tradizionale end ################################################################################
Sono tre costanti di cui:
con la prima possiamo scegliere qualsiasi numero di frames vogliamo.
con la seconda si possono scegliere il numero delle pose e anche qui possiamo sbizzarrirci.
con la terza si sceglie se attivare la camminata a 8 direzioni oppure a 4.
Ecco la risorsa:
################################################################################ module Impostazioni NUMERO_DI_FRAME_CHARACTER = 6 # default: @cw = bitmap.width / 4 NUMERO_DI_POSE_CHARACTER = 8 # default: @ch = bitmap.height / 4 CAMMINATA_8DIR = false # se false abilita quella tradizionale end ################################################################################ class Sprite_Character < RPG::Sprite #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor :character # character #-------------------------------------------------------------------------- # * Object Initialization # viewport : viewport # character : character (Game_Character) #-------------------------------------------------------------------------- def initialize(viewport, character = nil) super(viewport) @character = character update end def update super # If tile ID, file name, or hue are different from current ones if @tile_id != @character.tile_id or @character_name != @character.character_name or @character_hue != @character.character_hue # Remember tile ID, file name, and hue @tile_id = @character.tile_id @character_name = @character.character_name @character_hue = @character.character_hue # If tile ID value is valid if @tile_id >= 384 self.bitmap = RPG::Cache.tile($game_map.tileset_name, @tile_id, @character.character_hue) self.src_rect.set(0, 0, 32, 32) self.ox = 16 self.oy = 32 # If tile ID value is invalid else self.bitmap = RPG::Cache.character(@character.character_name, @character.character_hue) @cw = bitmap.width / Impostazioni::NUMERO_DI_FRAME_CHARACTER @ch = bitmap.height / Impostazioni::NUMERO_DI_POSE_CHARACTER self.ox = @cw / 2 self.oy = @ch end end # Set visible situation self.visible = (not @character.transparent) # If graphic is character if @tile_id == 0 # Set rectangular transfer sx = @character.pattern * @cw if @character.is_a?(Game_Player) case @character.direction when 1 sy = 0 when 2 sy = @ch when 3 sy = 2 * @ch when 4 sy = 3 * @ch when 6 sy = 4 * @ch when 7 sy = 5 * @ch when 8 sy = 6 * @ch when 9 sy = 7 * @ch end else sy = (@character.direction - 2) / 2 * @ch end self.src_rect.set(sx, sy, @cw, @ch) end # Set sprite coordinates self.x = @character.screen_x self.y = @character.screen_y self.z = @character.screen_z(@ch) # Set opacity level, blend method, and bush depth self.opacity = @character.opacity self.blend_type = @character.blend_type self.bush_depth = @character.bush_depth # Animation if @character.animation_id != 0 animation = $data_animations[@character.animation_id] animation(animation, true) @character.animation_id = 0 end end end ################################################################################ ###########################################################FINE SPRITE_CHARACTER #INIZIO GAME_PLAYER############################################################# ################################################################################ #============================================================================== # ** Game_Player #------------------------------------------------------------------------------ # This class handles the player. Its functions include event starting # determinants and map scrolling. Refer to "$game_player" for the one # instance of this class. #============================================================================== class Game_Player < Game_Character #-------------------------------------------------------------------------- # * Invariables #-------------------------------------------------------------------------- CENTER_X = (320 - 16) * 4 # Center screen x-coordinate * 4 CENTER_Y = (240 - 16) * 4 # Center screen y-coordinate * 4 #-------------------------------------------------------------------------- # * Passable Determinants # x : x-coordinate # y : y-coordinate # d : direction (0,2,4,6,8) # * 0 = Determines if all directions are impassable (for jumping) #-------------------------------------------------------------------------- def passable?(x, y, d) # Get new coordinates new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0) new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0) # If coordinates are outside of map unless $game_map.valid?(new_x, new_y) # Impassable return false end # If debug mode is ON and ctrl key was pressed if $DEBUG and Input.press?(Input::CTRL) # Passable return true end super end #-------------------------------------------------------------------------- # * Set Map Display Position to Center of Screen #-------------------------------------------------------------------------- def center(x, y) max_x = ($game_map.width - 20) * 128 max_y = ($game_map.height - 15) * 128 $game_map.display_x = [0, [x * 128 - CENTER_X, max_x].min].max $game_map.display_y = [0, [y * 128 - CENTER_Y, max_y].min].max end #-------------------------------------------------------------------------- # * Move to Designated Position # x : x-coordinate # y : y-coordinate #-------------------------------------------------------------------------- def moveto(x, y) super # Centering center(x, y) # Make encounter count make_encounter_count end #-------------------------------------------------------------------------- # * Increaase Steps #-------------------------------------------------------------------------- def increase_steps super # If move route is not forcing unless @move_route_forcing # Increase steps $game_party.increase_steps # Number of steps are an even number if $game_party.steps % 2 == 0 # Slip damage check $game_party.check_map_slip_damage end end end #-------------------------------------------------------------------------- # * Get Encounter Count #-------------------------------------------------------------------------- def encounter_count return @encounter_count end #-------------------------------------------------------------------------- # * Make Encounter Count #-------------------------------------------------------------------------- def make_encounter_count # Image of two dice rolling if $game_map.map_id != 0 n = $game_map.encounter_step @encounter_count = rand(n) + rand(n) + 1 end end #-------------------------------------------------------------------------- # * Refresh #-------------------------------------------------------------------------- def refresh # If party members = 0 if $game_party.actors.size == 0 # Clear character file name and hue @character_name = "" @character_hue = 0 # End method return end # Get lead actor actor = $game_party.actors[0] # Set character file name and hue @character_name = actor.character_name @character_hue = actor.character_hue # Initialize opacity level and blending method @opacity = 255 @blend_type = 0 end #-------------------------------------------------------------------------- # * Same Position Starting Determinant #-------------------------------------------------------------------------- def check_event_trigger_here(triggers) result = false # If event is running if $game_system.map_interpreter.running? return result end # All event loops for event in $game_map.events.values # If event coordinates and triggers are consistent if event.x == @x and event.y == @y and triggers.include?(event.trigger) # If starting determinant is same position event (other than jumping) if not event.jumping? and event.over_trigger? event.start result = true end end end return result end #-------------------------------------------------------------------------- # * Front Envent Starting Determinant #-------------------------------------------------------------------------- def check_event_trigger_there(triggers) result = false # If event is running if $game_system.map_interpreter.running? return result end # Calculate front event coordinates new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 : 0) new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 : 0) # All event loops for event in $game_map.events.values # If event coordinates and triggers are consistent if event.x == new_x and event.y == new_y and triggers.include?(event.trigger) # If starting determinant is front event (other than jumping) if not event.jumping? and not event.over_trigger? event.start result = true end end end # If fitting event is not found if result == false # If front tile is a counter if $game_map.counter?(new_x, new_y) # Calculate 1 tile inside coordinates new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 : 0) new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 : 0) # All event loops for event in $game_map.events.values # If event coordinates and triggers are consistent if event.x == new_x and event.y == new_y and triggers.include?(event.trigger) # If starting determinant is front event (other than jumping) if not event.jumping? and not event.over_trigger? event.start result = true end end end end end return result end #-------------------------------------------------------------------------- # * Touch Event Starting Determinant #-------------------------------------------------------------------------- def check_event_trigger_touch(x, y) result = false # If event is running if $game_system.map_interpreter.running? return result end # All event loops for event in $game_map.events.values # If event coordinates and triggers are consistent if event.x == x and event.y == y and [1,2].include?(event.trigger) # If starting determinant is front event (other than jumping) if not event.jumping? and not event.over_trigger? event.start result = true end end end return result end #-------------------------------------------------------------------------- # * Frame Update #-------------------------------------------------------------------------- def update # Remember whether or not moving in local variables last_moving = moving? # If moving, event running, move route forcing, and message window # display are all not occurring unless moving? or $game_system.map_interpreter.running? or @move_route_forcing or $game_temp.message_window_showing if Impostazioni::CAMMINATA_8DIR != true # Move player in the direction the directional button is being pressed case Input.dir4 when 2 move_down when 4 move_left when 6 move_right when 8 move_up end else case Input.dir8 when 1 move_lower_left when 2 move_down when 3 move_lower_right when 4 move_left when 6 move_right when 7 move_upper_left when 8 move_up when 9 move_upper_right end end end # Remember coordinates in local variables last_real_x = @real_x last_real_y = @real_y super # If character moves down and is positioned lower than the center # of the screen if @real_y > last_real_y and @real_y - $game_map.display_y > CENTER_Y # Scroll map down $game_map.scroll_down(@real_y - last_real_y) end # If character moves left and is positioned more let on-screen than # center if @real_x < last_real_x and @real_x - $game_map.display_x < CENTER_X # Scroll map left $game_map.scroll_left(last_real_x - @real_x) end # If character moves right and is positioned more right on-screen than # center if @real_x > last_real_x and @real_x - $game_map.display_x > CENTER_X # Scroll map right $game_map.scroll_right(@real_x - last_real_x) end # If character moves up and is positioned higher than the center # of the screen if @real_y < last_real_y and @real_y - $game_map.display_y < CENTER_Y # Scroll map up $game_map.scroll_up(last_real_y - @real_y) end # If not moving unless moving? # If player was moving last time if last_moving # Event determinant is via touch of same position event result = check_event_trigger_here([1,2]) # If event which started does not exist if result == false # Disregard if debug mode is ON and ctrl key was pressed unless $DEBUG and Input.press?(Input::CTRL) # Encounter countdown if @encounter_count > 0 @encounter_count -= 1 end end end end # If C button was pressed if Input.trigger?(Input::C) # Same position and front event determinant check_event_trigger_here([0]) check_event_trigger_there([0,1,2]) end end end end ################################################################################ ################################################################FINE GAME_PLAYER #INIZIO GAME_CHARACTER3######################################################### ################################################################################ #============================================================================== # ** Game_Character (part 3) #------------------------------------------------------------------------------ # This class deals with characters. It's used as a superclass for the # Game_Player and Game_Event classes. #============================================================================== class Game_Character #-------------------------------------------------------------------------- # * Move Down # turn_enabled : a flag permits direction change on that spot #-------------------------------------------------------------------------- def move_down(turn_enabled = true) # Turn down if turn_enabled turn_down end # If passable if passable?(@x, @y, 2) # Turn down turn_down # Update coordinates @y += 1 # Increase steps increase_steps # If impassable else # Determine if touch event is triggered check_event_trigger_touch(@x, @y+1) end end #-------------------------------------------------------------------------- # * Move Left # turn_enabled : a flag permits direction change on that spot #-------------------------------------------------------------------------- def move_left(turn_enabled = true) # Turn left if turn_enabled turn_left end # If passable if passable?(@x, @y, 4) # Turn left turn_left # Update coordinates @x -= 1 # Increase steps increase_steps # If impassable else # Determine if touch event is triggered check_event_trigger_touch(@x-1, @y) end end #-------------------------------------------------------------------------- # * Move Right # turn_enabled : a flag permits direction change on that spot #-------------------------------------------------------------------------- def move_right(turn_enabled = true) # Turn right if turn_enabled turn_right end # If passable if passable?(@x, @y, 6) # Turn right turn_right # Update coordinates @x += 1 # Increase steps increase_steps # If impassable else # Determine if touch event is triggered check_event_trigger_touch(@x+1, @y) end end #-------------------------------------------------------------------------- # * Move up # turn_enabled : a flag permits direction change on that spot #-------------------------------------------------------------------------- def move_up(turn_enabled = true) # Turn up if turn_enabled turn_up end # If passable if passable?(@x, @y, 8) # Turn up turn_up # Update coordinates @y -= 1 # Increase steps increase_steps # If impassable else # Determine if touch event is triggered check_event_trigger_touch(@x, @y-1) end end #-------------------------------------------------------------------------- # * Move Lower Left #-------------------------------------------------------------------------- def move_lower_left(turn_enabled = true) # Turn Lower Left if turn_enabled turn_lower_left end # If no direction fix unless @direction_fix # Face down is facing right or up @direction = (@direction == 6 ? 4 : @direction == 8 ? 2 : @direction) end # When a down to left or a left to down course is passable if (passable?(@x, @y, 2) and passable?(@x, @y + 1, 4)) or (passable?(@x, @y, 4) and passable?(@x - 1, @y, 2)) # Update coordinates @x -= 1 @y += 1 # Increase steps increase_steps end return passable?(@x,@y,1) end #-------------------------------------------------------------------------- # * Move Lower Right #-------------------------------------------------------------------------- def move_lower_right(turn_enabled = true) # Turn Lower right if turn_enabled turn_lower_right end # If no direction fix unless @direction_fix # Face right if facing left, and face down if facing up @direction = (@direction == 4 ? 6 : @direction == 8 ? 2 : @direction) end # When a down to right or a right to down course is passable if (passable?(@x, @y, 2) and passable?(@x, @y + 1, 6)) or (passable?(@x, @y, 6) and passable?(@x + 1, @y, 2)) # Update coordinates @x += 1 @y += 1 # Increase steps increase_steps end return passable?(@x, @y, 3) end #-------------------------------------------------------------------------- # * Move Upper Left #-------------------------------------------------------------------------- def move_upper_left(turn_enabled = true) # Turn Upper Left if turn_enabled turn_upper_left end # If no direction fix unless @direction_fix # Face left if facing right, and face up if facing down @direction = (@direction == 6 ? 4 : @direction == 2 ? 8 : @direction) end # When an up to left or a left to up course is passable if (passable?(@x, @y, 8) and passable?(@x, @y - 1, 4)) or (passable?(@x, @y, 4) and passable?(@x - 1, @y, 8)) # Update coordinates @x -= 1 @y -= 1 # Increase steps increase_steps end return passable?(@x, @y, 7) end #-------------------------------------------------------------------------- # * Move Upper Right #-------------------------------------------------------------------------- def move_upper_right(turn_enabled = true) # Turn Upper Right if turn_enabled turn_upper_right end # If no direction fix unless @direction_fix # Face right if facing left, and face up if facing down @direction = (@direction == 4 ? 6 : @direction == 2 ? 8 : @direction) end # When an up to right or a right to up course is passable if (passable?(@x, @y, 8) and passable?(@x, @y - 1, 6)) or (passable?(@x, @y, 6) and passable?(@x + 1, @y, 8)) # Update coordinates @x += 1 @y -= 1 # Increase steps increase_steps end return passable?(@x, @y, 9) end #-------------------------------------------------------------------------- # * Move at Random #-------------------------------------------------------------------------- def move_random case rand(4) when 0 # Move down move_down(false) when 1 # Move left move_left(false) when 2 # Move right move_right(false) when 3 # Move up move_up(false) end end #-------------------------------------------------------------------------- # * Move toward Player #-------------------------------------------------------------------------- def move_toward_player # Get difference in player coordinates sx = @x - $game_player.x sy = @y - $game_player.y # If coordinates are equal if sx == 0 and sy == 0 return end # Get absolute value of difference abs_sx = sx.abs abs_sy = sy.abs # If horizontal and vertical distances are equal if abs_sx == abs_sy # Increase one of them randomly by 1 rand(2) == 0 ? abs_sx += 1 : abs_sy += 1 end # If horizontal distance is longer if abs_sx > abs_sy # Move towards player, prioritize left and right directions sx > 0 ? move_left : move_right if not moving? and sy != 0 sy > 0 ? move_up : move_down end # If vertical distance is longer else # Move towards player, prioritize up and down directions sy > 0 ? move_up : move_down if not moving? and sx != 0 sx > 0 ? move_left : move_right end end end #-------------------------------------------------------------------------- # * Move away from Player #-------------------------------------------------------------------------- def move_away_from_player # Get difference in player coordinates sx = @x - $game_player.x sy = @y - $game_player.y # If coordinates are equal if sx == 0 and sy == 0 return end # Get absolute value of difference abs_sx = sx.abs abs_sy = sy.abs # If horizontal and vertical distances are equal if abs_sx == abs_sy # Increase one of them randomly by 1 rand(2) == 0 ? abs_sx += 1 : abs_sy += 1 end # If horizontal distance is longer if abs_sx > abs_sy # Move away from player, prioritize left and right directions sx > 0 ? move_right : move_left if not moving? and sy != 0 sy > 0 ? move_down : move_up end # If vertical distance is longer else # Move away from player, prioritize up and down directions sy > 0 ? move_down : move_up if not moving? and sx != 0 sx > 0 ? move_right : move_left end end end #-------------------------------------------------------------------------- # * 1 Step Forward #-------------------------------------------------------------------------- def move_forward case @direction when 2 move_down(false) when 4 move_left(false) when 6 move_right(false) when 8 move_up(false) end end #-------------------------------------------------------------------------- # * 1 Step Backward #-------------------------------------------------------------------------- def move_backward # Remember direction fix situation last_direction_fix = @direction_fix # Force directino fix @direction_fix = true # Branch by direction case @direction when 2 # Down move_up(false) when 4 # Left move_right(false) when 6 # Right move_left(false) when 8 # Up move_down(false) end # Return direction fix situation back to normal @direction_fix = last_direction_fix end #-------------------------------------------------------------------------- # * Jump # x_plus : x-coordinate plus value # y_plus : y-coordinate plus value #-------------------------------------------------------------------------- def jump(x_plus, y_plus) # If plus value is not (0,0) if x_plus != 0 or y_plus != 0 # If horizontal distnace is longer if x_plus.abs > y_plus.abs # Change direction to left or right x_plus < 0 ? turn_left : turn_right # If vertical distance is longer, or equal else # Change direction to up or down y_plus < 0 ? turn_up : turn_down end end # Calculate new coordinates new_x = @x + x_plus new_y = @y + y_plus # If plus value is (0,0) or jump destination is passable if (x_plus == 0 and y_plus == 0) or passable?(new_x, new_y, 0) # Straighten position straighten # Update coordinates @x = new_x @y = new_y # Calculate distance distance = Math.sqrt(x_plus * x_plus + y_plus * y_plus).round # Set jump count @jump_peak = 10 + distance - @move_speed @jump_count = @jump_peak * 2 # Clear stop count @stop_count = 0 end end #-------------------------------------------------------------------------- # * Turn Down #-------------------------------------------------------------------------- def turn_down unless @direction_fix @direction = 2 @stop_count = 0 end end #-------------------------------------------------------------------------- # * Turn Left #-------------------------------------------------------------------------- def turn_left unless @direction_fix @direction = 4 @stop_count = 0 end end #-------------------------------------------------------------------------- Autore: Lomax_Iced Pa, 08/06/2016 07:16 # * Turn Right #-------------------------------------------------------------------------- def turn_right unless @direction_fix @direction = 6 @stop_count = 0 end end #-------------------------------------------------------------------------- # * Turn Up #-------------------------------------------------------------------------- def turn_up unless @direction_fix @direction = 8 @stop_count = 0 end end #-------------------------------------------------------------------------- # * Turn Lower_Left #-------------------------------------------------------------------------- def turn_lower_left unless @direction_fix @direction = 1 @stop_count = 0 end end #-------------------------------------------------------------------------- # * Turn Lower_Right #-------------------------------------------------------------------------- def turn_lower_right unless @direction_fix @direction = 3 @stop_count = 0 end end #-------------------------------------------------------------------------- # * Turn Upper_Left #-------------------------------------------------------------------------- def turn_upper_left unless @direction_fix @direction = 7 @stop_count = 0 end end #-------------------------------------------------------------------------- # * Turn Upper_Right #-------------------------------------------------------------------------- def turn_upper_right unless @direction_fix @direction = 9 @stop_count = 0 end end #-------------------------------------------------------------------------- # * Turn 90° Right #-------------------------------------------------------------------------- def turn_right_90 case @direction when 2 turn_left when 4 turn_up when 6 turn_down when 8 turn_right end end #-------------------------------------------------------------------------- # * Turn 90° Left #-------------------------------------------------------------------------- def turn_left_90 case @direction when 2 turn_right when 4 turn_down when 6 turn_up when 8 turn_left end end #-------------------------------------------------------------------------- # * Turn 180° #-------------------------------------------------------------------------- def turn_180 case @direction when 2 turn_up when 4 turn_right when 6 turn_left when 8 turn_down end end #-------------------------------------------------------------------------- # * Turn 90° Right or Left #-------------------------------------------------------------------------- def turn_right_or_left_90 if rand(2) == 0 turn_right_90 else turn_left_90 end end #-------------------------------------------------------------------------- # * Turn at Random #-------------------------------------------------------------------------- def turn_random case rand(4) when 0 turn_up when 1 turn_right when 2 turn_left when 3 turn_down end end #-------------------------------------------------------------------------- # * Turn Towards Player #-------------------------------------------------------------------------- def turn_toward_player # Get difference in player coordinates sx = @x - $game_player.x sy = @y - $game_player.y # If coordinates are equal if sx == 0 and sy == 0 return end # If horizontal distance is longer if sx.abs > sy.abs # Turn to the right or left towards player sx > 0 ? turn_left : turn_right # If vertical distance is longer else # Turn up or down towards player sy > 0 ? turn_up : turn_down end end #-------------------------------------------------------------------------- # * Turn Away from Player Autore: Lomax_Iced Pa, 08/06/2016 07:16 #-------------------------------------------------------------------------- def turn_away_from_player # Get difference in player coordinates sx = @x - $game_player.x sy = @y - $game_player.y # If coordinates are equal if sx == 0 and sy == 0 return end # If horizontal distance is longer if sx.abs > sy.abs # Turn to the right or left away from player sx > 0 ? turn_right : turn_left # If vertical distance is longer else # Turn up or down away from player sy > 0 ? turn_down : turn_up end end end ################################################################################ ############################################################FINE GAME_CHARACTER3 ################################################################################ ########################################################## FINE SCRIPT ..per ora #Autore: Lomax_Iced #Pa, 08/06/2016 07:16
Ovviamente è un argomento che desidero ampliare e forse questo script si arricchirà di nuove funzioni.
Un'idea bella potrebbe essere quella di utilizzare un unico charaset per tutti i movimenti sfruttando anche
i Bs. Ma questa è un'altra storia.. non ora (almeno io..)
Ad ogni modo spero che la risorsa vi sia utile, non ho riscontrato conflitti di alcun tipo e buona Giornata ;)
AH! Quasi dimenticavo!
Lo script sostituisce totalmente i vostri Sprite_Character, Game_Character3 e Game_Player, quindi per utilizzarlo correttamente
o li gelate, oppure li eliminate dall'editor, fate voi. ciao.
Modificato da Lomax_Iced, 08 June 2016 - 06:52 AM.