Skip to content

Commit

Permalink
Refs 114: character sprite scaling regions update (#141)
Browse files Browse the repository at this point in the history
* scaling regions added

* lines length reduced according to mepedorr comment

* fix to update characters' position just after  room load

* sprite scale change strictly limited to region's top and bottom scale

* check and update character scale on room load fix

* line length reduced according to mapedorr comment

* update to avoid caching default regions parameters preventing scaling rooms other than main
  • Loading branch information
Whyshchuck authored and mapedorr committed Feb 18, 2024
1 parent e4e850d commit e5be351
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
6 changes: 5 additions & 1 deletion addons/popochiu/engine/objects/region/popochiu_region.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func _update_scaling_region(chr: PopochiuCharacter) -> void:
chr.on_scaling_region= {
'region_description': self.description,
'scale_top': self.scale_top,
'scale_bottom': self.scale_bottom,
'scale_bottom': self.scale_bottom,
'scale_max': [self.scale_top, self.scale_bottom].max(),
'scale_min': [self.scale_top, self.scale_bottom].min(),
'polygon_top_y': (
polygon_y_array.min()+self.position.y+get_node("InteractionPolygon").position.y
if self.position
Expand All @@ -65,9 +67,11 @@ func _check_area(area: PopochiuCharacter, entered: bool) -> void:
_on_character_entered(area)
if scaling:
_update_scaling_region(area)
E.current_room.update_character_scale(area)
else:
_on_character_exited(area)
_clear_scaling_region(area)
E.current_room.update_character_scale(area)


func _set_enabled(value: bool) -> void:
Expand Down
61 changes: 34 additions & 27 deletions addons/popochiu/engine/objects/room/popochiu_room.gd
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,44 @@ func clean_characters() -> void:
if c is PopochiuCharacter:
c.queue_free()

func update_character_scale(chr):
if chr.on_scaling_region:
var polygon_range = (
chr.on_scaling_region['polygon_bottom_y'] - chr.on_scaling_region['polygon_top_y']
)
var scale_range = (
chr.on_scaling_region['scale_bottom'] - chr.on_scaling_region['scale_top']
)

var position_from_the_top_of_region = (
chr.position.y-chr.on_scaling_region['polygon_top_y']
)

var scale_for_position = (
chr.on_scaling_region['scale_top']+(
scale_range/polygon_range*position_from_the_top_of_region
)
)
chr.scale.x = [
[scale_for_position, chr.on_scaling_region['scale_min']].max(),
chr.on_scaling_region['scale_max']
].min()
chr.scale.y = [
[scale_for_position, chr.on_scaling_region['scale_min']].max(),
chr.on_scaling_region['scale_max']
].min()
chr.walk_speed = chr.default_walk_speed/chr.default_scale.x*scale_for_position
else:
chr.scale = chr.default_scale
chr.walk_speed = chr.default_walk_speed

func update_characters_position(character):
character.position = (
character.position_stored
if character.position_stored
else character.position
)
_update_character_scale(character)
update_character_scale(character)

# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ SET & GET ░░░░
func get_marker(marker_name: String) -> Marker2D:
Expand Down Expand Up @@ -294,30 +325,6 @@ func set_active_walkable_area(walkable_area_name: String) -> void:


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ PRIVATE ░░░░
func _update_character_scale(chr):
if chr.on_scaling_region:
var polygon_range = (
chr.on_scaling_region['polygon_bottom_y'] - chr.on_scaling_region['polygon_top_y']
)
var scale_range = (
chr.on_scaling_region['scale_bottom'] - chr.on_scaling_region['scale_top']
)

var position_from_the_top_of_region = (
chr.position.y-chr.on_scaling_region['polygon_top_y']
)

var scale_for_position = (
chr.on_scaling_region['scale_top']+(
scale_range/polygon_range*position_from_the_top_of_region
)
)
chr.scale.x = scale_for_position
chr.scale.y = scale_for_position
chr.walk_speed = chr.default_walk_speed/chr.default_scale.x*scale_for_position
else:
chr.scale = chr.default_scale
chr.walk_speed = chr.default_walk_speed

func _move_along_path(distance: float, moving_character_data: Dictionary):
var last_point: Vector2 =(
Expand All @@ -339,15 +346,15 @@ func _move_along_path(distance: float, moving_character_data: Dictionary):
moving_character_data.character.position_stored = next_position
else:
moving_character_data.character.position = next_position
_update_character_scale(moving_character_data.character)
update_character_scale(moving_character_data.character)
return

distance -= distance_between_points
last_point = moving_character_data.path[0]
moving_character_data.path.remove_at(0)

moving_character_data.character.position = last_point
_update_character_scale(moving_character_data.character)
update_character_scale(moving_character_data.character)
_clear_navigation_path(moving_character_data.character)


Expand Down
5 changes: 4 additions & 1 deletion addons/popochiu/popochiu_resources.gd
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ const WALKABLE_AREAS_IGNORE := [
]
const REGIONS_IGNORE := [
'description',
'tint'
'tint',
'scaling',
'scale_top',
'scale_bottom'
]
const SNGL_TEMPLATE := '@tool\n' +\
'extends "%s"\n\n' +\
Expand Down

0 comments on commit e5be351

Please sign in to comment.