Skip to content

Commit

Permalink
refs 114: character sprite scaling regions (#127)
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
  • Loading branch information
Whyshchuck authored Dec 14, 2023
1 parent 1b75d97 commit 69f59ac
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var last_room := ''
var anim_suffix := ''
var is_moving := false
var emotion := ''
var on_scaling_region: Dictionary = {}
var default_walk_speed: int
var default_scale: Vector2

var _looking_dir: int = Looking.DOWN

Expand All @@ -37,7 +40,8 @@ var _looking_dir: int = Looking.DOWN
# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ GODOT ░░░░
func _ready():
super()

default_walk_speed = walk_speed
default_scale = Vector2(scale)
if not Engine.is_editor_hint():
set_process(follow_player)
else:
Expand Down
41 changes: 35 additions & 6 deletions addons/popochiu/engine/objects/region/popochiu_region.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ extends Area2D
# TODO: If walkable is false, characters should not be able to walk through this.
#export var walkable := true
@export var tint := Color.WHITE
# TODO: Make the scale of the character change depending checked where it is placed in
# the area.
#export var scale_top := 1.0
#export var scale_bottom := 1.0
@export var scaling :bool = false
@export var scale_top :float = 1.0
@export var scale_bottom :float = 1.0


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ GODOT ░░░░
Expand All @@ -33,12 +32,42 @@ func _on_character_entered(chr: PopochiuCharacter) -> void:
func _on_character_exited(chr: PopochiuCharacter) -> void:
pass

func _update_scaling_region(chr: PopochiuCharacter) -> void:
var polygon_y_array = []
for x in get_node("InteractionPolygon").get_polygon():
polygon_y_array.append(x.y)

chr.on_scaling_region= {
'region_description': self.description,
'scale_top': self.scale_top,
'scale_bottom': self.scale_bottom,
'polygon_top_y': (
polygon_y_array.min()+self.position.y+get_node("InteractionPolygon").position.y
if self.position
else ''
),
'polygon_bottom_y': (
polygon_y_array.max()+self.position.y+get_node("InteractionPolygon").position.y
if self.position
else ''),

}


func _clear_scaling_region(chr: PopochiuCharacter) -> void:
if chr.on_scaling_region and chr.on_scaling_region['region_description'] == self.description:
chr.on_scaling_region = {}

# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ PRIVATE ░░░░
func _check_area(area: PopochiuCharacter, entered: bool) -> void:
if area is PopochiuCharacter:
if entered: _on_character_entered(area)
else: _on_character_exited(area)
if entered:
_on_character_entered(area)
if scaling:
_update_scaling_region(area)
else:
_on_character_exited(area)
_clear_scaling_region(area)


func _set_enabled(value: bool) -> void:
Expand Down
31 changes: 29 additions & 2 deletions addons/popochiu/engine/objects/room/popochiu_room.gd
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func add_character(chr: PopochiuCharacter) -> void:
#warning-ignore:return_value_discarded
chr.started_walk_to.connect(_update_navigation_path)
chr.stopped_walk.connect(_clear_navigation_path.bind(chr))

update_characters_position(chr)
if chr.follow_player:
C.player.started_walk_to.connect(_follow_player.bind(chr))

Expand Down Expand Up @@ -188,6 +188,7 @@ func update_characters_position(character):
if character.position_stored
else character.position
)
_update_character_scale(character)

# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ SET & GET ░░░░
func get_marker(marker_name: String) -> Marker2D:
Expand Down Expand Up @@ -293,6 +294,31 @@ 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 =(
moving_character_data.character.position_stored
Expand All @@ -313,14 +339,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)
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)
_clear_navigation_path(moving_character_data.character)


Expand Down

0 comments on commit 69f59ac

Please sign in to comment.