小场景:自动跟随的老鼠

This commit is contained in:
cakipaul 2025-01-23 14:26:57 +08:00
parent 097594f22b
commit 055b9dd1f8
7 changed files with 290 additions and 14 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 351 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bebirk1fwldg2"
path="res://.godot/imported/老鼠.png-ec72ce9b9d545fd97136d899edf3f074.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://asset/art/little_game/老鼠/老鼠.png"
dest_files=["res://.godot/imported/老鼠.png-ec72ce9b9d545fd97136d899edf3f074.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

View File

@ -82,6 +82,7 @@
吕萍: (疑惑——)[ID:c01_s05_夜行船2] 吕萍: (疑惑——)[ID:c01_s05_夜行船2]
=> END => END
~ c01_s05_院长房间_开门 ~ c01_s06_谈论鬼差
音效: 开门 [ID:音效_开门] 小孩甲: 鬼差...你知道吗? [ID:c01_s06_谈论鬼差1]
小孩乙: 我听奶奶说... [ID:c01_s06_谈论鬼差2]
=> END => END

View File

@ -723,17 +723,6 @@ height = 50.0
[node name="MainPlayer" parent="Ground" index="5"] [node name="MainPlayer" parent="Ground" index="5"]
character = "小小蝶" character = "小小蝶"
[node name="CameraFocusMarker" parent="Ground" index="6"]
force_offset = Vector2(0, 0)
lock_horizontal = true
speed = 5.0
half_screen_size = Vector2(282, 120)
shaded_height = 38
shake_enabled = false
shake_strength = 2.0
shake_recovery_speed = 8.0
zoom_ratio = 1.0
[node name="FGSprite2D" parent="Ground/ParallaxForeground/FGParallaxLayer" index="0"] [node name="FGSprite2D" parent="Ground/ParallaxForeground/FGParallaxLayer" index="0"]
position = Vector2(21, 39) position = Vector2(21, 39)
texture = ExtResource("3_vmr0f") texture = ExtResource("3_vmr0f")

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=30 format=3 uid="uid://bx16c8nn32f40"] [gd_scene load_steps=32 format=3 uid="uid://bx16c8nn32f40"]
[ext_resource type="PackedScene" uid="uid://dayyx4jerj7io" path="res://scene/ground/ground.tscn" id="1_bitx7"] [ext_resource type="PackedScene" uid="uid://dayyx4jerj7io" path="res://scene/ground/ground.tscn" id="1_bitx7"]
[ext_resource type="Script" path="res://scene/ground/scene/c01/s06_animation.gd" id="2_fkfhi"] [ext_resource type="Script" path="res://scene/ground/scene/c01/s06_animation.gd" id="2_fkfhi"]
@ -12,7 +12,9 @@
[ext_resource type="SpriteFrames" uid="uid://c6okvaeemoodq" path="res://asset/art/gif/c01_孤儿院围墙/frames.tres" id="7_dsj2r"] [ext_resource type="SpriteFrames" uid="uid://c6okvaeemoodq" path="res://asset/art/gif/c01_孤儿院围墙/frames.tres" id="7_dsj2r"]
[ext_resource type="Script" path="res://scene/entity/general/state_action_res.gd" id="9_lg0nw"] [ext_resource type="Script" path="res://scene/entity/general/state_action_res.gd" id="9_lg0nw"]
[ext_resource type="PackedScene" uid="uid://b50n0hvs4yh75" path="res://scene/entity/general/autoplay_animated_sprite.tscn" id="9_on7ga"] [ext_resource type="PackedScene" uid="uid://b50n0hvs4yh75" path="res://scene/entity/general/autoplay_animated_sprite.tscn" id="9_on7ga"]
[ext_resource type="PackedScene" uid="uid://nwpbl5yuh280" path="res://scene/ground/script/c01/s00_自动跟随的老鼠.tscn" id="11_7gmev"]
[ext_resource type="PackedScene" uid="uid://bnf3lkcbpx1ar" path="res://scene/entity/ambush.tscn" id="11_tudob"] [ext_resource type="PackedScene" uid="uid://bnf3lkcbpx1ar" path="res://scene/entity/ambush.tscn" id="11_tudob"]
[ext_resource type="PackedScene" uid="uid://jr1yd46wm5je" path="res://scene/entity/note.tscn" id="12_28t76"]
[sub_resource type="Animation" id="Animation_723yg"] [sub_resource type="Animation" id="Animation_723yg"]
length = 0.001 length = 0.001
@ -254,6 +256,13 @@ sprite_frames = ExtResource("7_dsj2r")
animation = &"【胖小孩背着残疾小孩】-呼吸" animation = &"【胖小孩背着残疾小孩】-呼吸"
state_configs = Array[ExtResource("9_lg0nw")]([SubResource("Resource_wae3q"), SubResource("Resource_1yioc")]) state_configs = Array[ExtResource("9_lg0nw")]([SubResource("Resource_wae3q"), SubResource("Resource_1yioc")])
[node name="自动跟随的老鼠" parent="Ground/DeployLayer" index="12" instance=ExtResource("11_7gmev")]
position = Vector2(2507, 75)
[node name="Note院长房间" parent="Ground/DeployLayer" index="13" instance=ExtResource("12_28t76")]
[node name="Note小孩房间" parent="Ground/DeployLayer" index="14" instance=ExtResource("12_28t76")]
[node name="PointLight2D" type="PointLight2D" parent="Ground/AmbientLayer" index="0"] [node name="PointLight2D" type="PointLight2D" parent="Ground/AmbientLayer" index="0"]
energy = 0.6 energy = 0.6
range_layer_max = 2 range_layer_max = 2

View File

@ -0,0 +1,178 @@
@tool
extends Node2D
@export var enabled := true
@export var amounts := 5
# 老鼠活动区域
@export var action_area := Vector2(200, 30):
set(val):
action_area = val
queue_redraw()
@export var gizmo_outline_color := Color(0.565, 0.271, 0.318, 0.8):
set(val):
gizmo_outline_color = val
queue_redraw()
@export var move_speed := 100.0
@export var distance_to_player_range := Vector2(50, 270)
@export var random_wait_time_range := Vector2(0.5, 1.0)
@export var random_wait_possibility := 0.4
@export var random_mov_distace_range := Vector2(20, 50)
@export var scatter_on_start := false
@export var debug_scatter := false:
set(val):
debug_scatter = false
if Engine.is_editor_hint():
_init_mice(true)
var mice = []
# 大于 0 时等待,小于 0 时移动abs()<0.1 时重新生成时间
var random_wait_time := [] as Array[float]
var random_directions := [] as Array[Vector2]
var random_positions := [] as Array[Vector2]
var player
func _ready() -> void:
if Engine.is_editor_hint():
queue_redraw()
_init_mice()
func _enter_tree() -> void:
if is_node_ready():
_init_mice()
func flush_right_and_disable() -> void:
enabled = false
var tween = create_tween()
for i in range(mice.size()):
var m = mice[i]
m.flip_h = true
if i == 0:
tween.tween_property(m, "position:x", m.position.x + 10000, 100.0)
else:
tween.parallel().tween_property(m, "position:x", m.position.x + 10000, 100.0)
func _init_mice(scatter := scatter_on_start) -> void:
mice.clear()
for c in get_children():
if c is AnimatedSprite2D:
mice.append(c)
random_wait_time.resize(mice.size())
random_directions.resize(mice.size())
random_positions.resize(mice.size())
for i in range(mice.size()):
var m = mice[i]
# clamp to action area
if scatter:
var pos = Vector2(randf() * action_area.x, randf() * action_area.y)
m.position = pos
random_positions[i] = m.position
random_directions[i] = Vector2.ONE
random_wait_time[i] = 0
func _physics_process(delta: float) -> void:
if Engine.is_editor_hint() or not enabled:
return
if not player:
player = SceneManager.get_player()
if not player:
return
var player_global_position = player.global_position
# clamp to action area, 允许稍微大一点,防止卡位
player_global_position.x = clampf(
player_global_position.x, global_position.x - 2, global_position.x + action_area.x + 4
)
for i in range(mice.size()):
var m = mice[i]
var m_pos = m.position
var m_global_pos = m.global_position
var squared_distance = m_global_pos.distance_squared_to(player_global_position)
var facing_right := true
var direction = m_global_pos.direction_to(player_global_position)
if squared_distance < pow(distance_to_player_range.x, 2):
# move away
facing_right = direction.x < 0
if facing_right:
m.position.x += move_speed * delta
else:
m.position.x -= move_speed * delta
m.position.y -= direction.y * move_speed * delta
random_wait_time[i] = -0.5 # 进入运动状态
random_positions[i] = m.position
elif squared_distance > pow(distance_to_player_range.y, 2):
# move closer
facing_right = direction.x > 0
if facing_right:
m.position.x += move_speed * delta
else:
m.position.x -= move_speed * delta
m.position.y += direction.y * move_speed * delta
random_wait_time[i] = -0.5 # 进入运动状态
random_positions[i] = m.position
else:
if abs(random_wait_time[i]) < 0.1:
random_wait_time[i] = (
random_wait_time_range.x
+ randf() * (random_wait_time_range.y - random_wait_time_range.x)
)
if randf() > random_wait_possibility:
random_wait_time[i] *= -1
# 一半概率等待,一半概率移动
elif random_wait_time[i] > 0:
random_wait_time[i] -= delta
# random move, 每次移动后随机生成新的目标位置,避免老鼠在原地打转
else:
random_wait_time[i] += delta
var target = random_positions[i]
if target.distance_squared_to(m_pos) < 9:
direction.y = randf_range(-0.5, 0.5)
var x_diff = player_global_position.x - m_global_pos.x
var diff_sign = signf(x_diff)
# 大概率向 distance_to_player_range 的中间移动
x_diff = (
x_diff
- (
diff_sign
* (distance_to_player_range.x + distance_to_player_range.y)
* 0.5
)
)
# 75% 的概率向 x_diff 移动
if signf(x_diff) > 0:
direction.x = 1 if randf() < 0.7 else -1
else:
direction.x = -1 if randf() < 0.7 else 1
random_directions[i] = direction
var random_dis = (
random_mov_distace_range.x
+ randf() * (random_mov_distace_range.y - random_mov_distace_range.x)
)
target = direction * random_dis + m_pos
# clamp to action area稍小于 action_area避免老鼠在边缘打转
# random_positions[i] = target.clamp(Vector2(2, 2), action_area - Vector2(4, 4))
random_positions[i] = target.clamp(Vector2.ZERO, action_area)
facing_right = target.x > m_pos.x
var delta_max = move_speed * delta
m_pos = m.position
m.position.x = move_toward(m_pos.x, target.x, delta_max)
m.position.y = move_toward(m_pos.y, target.y, delta_max)
# set flip h
m.flip_h = facing_right
# clamp to action area
m.position = m.position.clamp(Vector2.ZERO, action_area)
func _draw() -> void:
if Engine.is_editor_hint():
# draw gizmo: 老鼠活动区域
var action_area_rect = Rect2(Vector2.ZERO, action_area)
# fill
var fill_color = gizmo_outline_color
fill_color.a = 0.4
draw_rect(action_area_rect, fill_color)
# outline
draw_rect(action_area_rect, gizmo_outline_color, false, 1.0)

View File

@ -0,0 +1,65 @@
[gd_scene load_steps=6 format=3 uid="uid://nwpbl5yuh280"]
[ext_resource type="Script" path="res://scene/ground/script/c01/s00_自动跟随的老鼠.gd" id="1_lesfu"]
[ext_resource type="Texture2D" uid="uid://bebirk1fwldg2" path="res://asset/art/little_game/老鼠/老鼠.png" id="2_6u35i"]
[sub_resource type="AtlasTexture" id="AtlasTexture_6ljga"]
atlas = ExtResource("2_6u35i")
region = Rect2(0, 0, 16, 10)
[sub_resource type="AtlasTexture" id="AtlasTexture_p46st"]
atlas = ExtResource("2_6u35i")
region = Rect2(32, 0, 16, 10)
[sub_resource type="SpriteFrames" id="SpriteFrames_bqotn"]
animations = [{
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_6ljga")
}],
"loop": true,
"name": &"mouse1",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_p46st")
}],
"loop": true,
"name": &"mouse2",
"speed": 5.0
}]
[node name="自动跟随的老鼠" type="Node2D"]
script = ExtResource("1_lesfu")
action_area = Vector2(700, 40)
[node name="mouse1" type="AnimatedSprite2D" parent="."]
y_sort_enabled = true
position = Vector2(341.946, 19.4184)
sprite_frames = SubResource("SpriteFrames_bqotn")
animation = &"mouse1"
[node name="mouse2" type="AnimatedSprite2D" parent="."]
y_sort_enabled = true
position = Vector2(441.315, 7.66069)
sprite_frames = SubResource("SpriteFrames_bqotn")
animation = &"mouse2"
[node name="mouse3" type="AnimatedSprite2D" parent="."]
y_sort_enabled = true
position = Vector2(602.687, 37.4692)
sprite_frames = SubResource("SpriteFrames_bqotn")
animation = &"mouse2"
[node name="mouse4" type="AnimatedSprite2D" parent="."]
y_sort_enabled = true
position = Vector2(59.7088, 20.6539)
sprite_frames = SubResource("SpriteFrames_bqotn")
animation = &"mouse1"
[node name="mouse5" type="AnimatedSprite2D" parent="."]
y_sort_enabled = true
position = Vector2(198.783, 2.87105)
sprite_frames = SubResource("SpriteFrames_bqotn")
animation = &"mouse2"