- New strata_core/feedback.lua: sound sets (stone, dirt, wood, leaves) via strata.node_sounds; nodes without sounds get a set from their dig group after all mods load. Walls get dirt/wood/stone explicitly. - Server-side break dust textured from the dug node, spawned in front of the plane so every player sees it. - Walls, torches and doors play their place sound from their custom on_place (strata.play_place_sound). - Avatar footsteps on two walk frames; on damage the avatar flashes red (strata.flash) and other players hear player_damage / player_falling_damage at it. The engine plays those for the hurt player. - Slimes flash via damage_texture_modifier and squelch when hit or killed. - Sounds are synthesized by the new tools/gen_sounds.py (CC0, no samples), listed in the new CREDITS.md. The swing animation was already done in #15. - 9 selftest cases in tests/feedback.lua. Closes #16 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
304 lines
10 KiB
Lua
304 lines
10 KiB
Lua
-- Strata player appearance: avatar animation, the held item and name tags.
|
|
--
|
|
-- * The avatar is an upright_sprite. Its frame is picked each step from a
|
|
-- one-row sprite sheet (strata_player_sheet.png, made by
|
|
-- tools/gen_textures.py) with the [sheet modifier, mirrored with
|
|
-- [transformFX when facing left. Textures are only re-sent when the frame
|
|
-- or facing changes.
|
|
-- * The wielded item is a separate wielditem entity attached to the avatar.
|
|
-- It pivots around the shoulder and sweeps an arc while dig or place is
|
|
-- held.
|
|
-- * Names: an entity nametag would show players their own name too, so each
|
|
-- player instead gets a HUD waypoint per other avatar on screen.
|
|
-- * Feedback: footsteps on walk frames, and a red flash plus a hurt sound
|
|
-- (for other players; the engine plays it for the hurt player) on damage.
|
|
|
|
local FRAME_COUNT = 9
|
|
local FRAME_IDLE = 0
|
|
local FRAME_WALK = 1 -- first of WALK_FRAMES
|
|
local WALK_FRAMES = 4
|
|
local FRAME_JUMP = 5
|
|
local FRAME_FALL = 6
|
|
local FRAME_SWING_UP = 7
|
|
local FRAME_SWING_DOWN = 8
|
|
local SHEET = "strata_player_sheet.png"
|
|
|
|
local WALK_MIN_SPEED = 0.5 -- nodes/s below which the avatar stands still
|
|
local WALK_FRAME_RATE = 1.6 -- walk frames per node travelled
|
|
local SWING_TIME = 0.32 -- seconds per swing
|
|
|
|
-- Held item, in nodes relative to the avatar's centre (facing right).
|
|
local SHOULDER = {x = 0.09, y = 0.22}
|
|
local HOLD_RADIUS = 0.8 -- shoulder to the item's centre
|
|
local HOLD_Z = -0.06 -- just in front of the sprite
|
|
local REST_ANGLE = -55 -- degrees from +x, counter-clockwise
|
|
local SWING_FROM = 115
|
|
local SWING_TO = -40
|
|
local ITEM_ANGLE = 45 -- item textures point from handle to tip at 45°
|
|
local HELD_SIZE = 0.45 -- wielditem visual_size
|
|
local ATTACH_SCALE = 10 -- attachment offsets are in 1/10 node
|
|
|
|
-- Name tags
|
|
local NAMETAG_INTERVAL = 0.1
|
|
local NAMETAG_RISE = 1.35 -- above the avatar's centre
|
|
local NAMETAG_RANGE_X = 32 -- only for avatars about on screen
|
|
local NAMETAG_RANGE_Y = 22
|
|
local NAMETAG_COLOR = 0xFFFFFF
|
|
|
|
-- Feedback
|
|
local STEP_FRAMES = {[FRAME_WALK] = true, [FRAME_WALK + 2] = true}
|
|
local FEET_DROP = 1.05 -- avatar centre to just below its feet
|
|
|
|
strata.avatar_frames = {
|
|
COUNT = FRAME_COUNT, IDLE = FRAME_IDLE, WALK = FRAME_WALK, WALK_FRAMES = WALK_FRAMES,
|
|
JUMP = FRAME_JUMP, FALL = FRAME_FALL, SWING_UP = FRAME_SWING_UP, SWING_DOWN = FRAME_SWING_DOWN,
|
|
SWING_TIME = SWING_TIME,
|
|
}
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Frames
|
|
--------------------------------------------------------------------------
|
|
|
|
--- The sheet frame for an avatar state.
|
|
-- s.on_ground, s.vx, s.vy: movement; s.swing: swing progress 0..1 or nil;
|
|
-- s.walk_phase: walk animation phase (frames travelled).
|
|
function strata.avatar_frame(s)
|
|
if s.swing then
|
|
return s.swing < 0.5 and FRAME_SWING_UP or FRAME_SWING_DOWN
|
|
elseif not s.on_ground then
|
|
return s.vy > 0 and FRAME_JUMP or FRAME_FALL
|
|
elseif math.abs(s.vx) >= WALK_MIN_SPEED then
|
|
return FRAME_WALK + math.floor(s.walk_phase or 0) % WALK_FRAMES
|
|
end
|
|
return FRAME_IDLE
|
|
end
|
|
|
|
--- Texture string for a frame, mirrored when facing left.
|
|
function strata.avatar_texture(frame, facing)
|
|
local tex = ("%s^[sheet:%dx1:%d,0"):format(SHEET, FRAME_COUNT, frame)
|
|
if facing < 0 then
|
|
tex = tex .. "^[transformFX"
|
|
end
|
|
return tex
|
|
end
|
|
|
|
--- Held item angle (degrees from forward, counter-clockwise) for a swing
|
|
-- progress 0..1, or the resting angle when not swinging.
|
|
function strata.held_angle(swing)
|
|
if not swing then
|
|
return REST_ANGLE
|
|
end
|
|
return SWING_FROM + (SWING_TO - SWING_FROM) * swing
|
|
end
|
|
|
|
--- Attachment offset (in 1/10 node) and rotation for the held item.
|
|
function strata.held_attachment(angle, facing)
|
|
local a = math.rad(angle)
|
|
local pos = {
|
|
x = (SHOULDER.x + HOLD_RADIUS * math.cos(a)) * facing * ATTACH_SCALE,
|
|
y = (SHOULDER.y + HOLD_RADIUS * math.sin(a)) * ATTACH_SCALE,
|
|
z = HOLD_Z * ATTACH_SCALE,
|
|
}
|
|
-- Rotation is applied Z first, then the yaw mirrors it for facing left.
|
|
local rot = {x = 0, y = facing < 0 and 180 or 0, z = angle - ITEM_ANGLE}
|
|
return pos, rot
|
|
end
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Held item entity
|
|
--------------------------------------------------------------------------
|
|
|
|
core.register_entity("strata_player:held", {
|
|
initial_properties = {
|
|
physical = false,
|
|
pointable = false,
|
|
visual = "wielditem",
|
|
visual_size = {x = HELD_SIZE, y = HELD_SIZE},
|
|
is_visible = false,
|
|
static_save = false,
|
|
},
|
|
on_step = function(self)
|
|
if not self.object:get_attach() then
|
|
self.object:remove()
|
|
end
|
|
end,
|
|
})
|
|
|
|
local function held_entity(self)
|
|
local held = self._held
|
|
if held and held:get_pos() then
|
|
return held
|
|
end
|
|
held = core.add_entity(self.object:get_pos(), "strata_player:held")
|
|
self._held = held
|
|
self._held_item, self._held_key = nil, nil
|
|
return held
|
|
end
|
|
|
|
local function update_held(self, item, swing)
|
|
local held = held_entity(self)
|
|
if not held then
|
|
return
|
|
end
|
|
if item ~= self._held_item then
|
|
self._held_item = item
|
|
if item == "" then
|
|
held:set_properties({is_visible = false})
|
|
else
|
|
held:set_properties({is_visible = true, wield_item = item})
|
|
end
|
|
end
|
|
local angle = strata.held_angle(swing)
|
|
local key = ("%d:%d"):format(self._facing, math.floor(angle + 0.5))
|
|
if key ~= self._held_key then
|
|
self._held_key = key
|
|
local pos, rot = strata.held_attachment(angle, self._facing)
|
|
held:set_attach(self.object, "", pos, rot)
|
|
end
|
|
end
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Per-step update, called from the avatar's on_step
|
|
--------------------------------------------------------------------------
|
|
|
|
--- Advance the avatar's animation and held item.
|
|
-- player may be nil for test avatars, which use _test_ctrl and _test_wield.
|
|
function strata.update_avatar_appearance(self, dtime, ctrl, vel, on_ground, player)
|
|
-- swinging: runs while dig/place is held, and finishes the current swing
|
|
local using = ctrl.dig or ctrl.place or ctrl.LMB or ctrl.RMB
|
|
if self._swing then
|
|
self._swing = self._swing + dtime / SWING_TIME
|
|
if self._swing >= 1 then
|
|
self._swing = using and 0 or nil
|
|
end
|
|
elseif using then
|
|
self._swing = 0
|
|
end
|
|
|
|
if on_ground and math.abs(vel.x) >= WALK_MIN_SPEED then
|
|
self._walk_phase = (self._walk_phase or 0) + math.abs(vel.x) * dtime * WALK_FRAME_RATE
|
|
else
|
|
self._walk_phase = 0
|
|
end
|
|
|
|
local frame = strata.avatar_frame({
|
|
on_ground = on_ground, vx = vel.x, vy = vel.y,
|
|
swing = self._swing, walk_phase = self._walk_phase,
|
|
})
|
|
if frame ~= self._frame and STEP_FRAMES[frame] then
|
|
strata.avatar_footstep(self.object:get_pos())
|
|
end
|
|
if frame ~= self._frame or self._facing ~= self._tex_facing then
|
|
self._frame, self._tex_facing = frame, self._facing
|
|
local tex = strata.avatar_texture(frame, self._facing)
|
|
self.object:set_properties({textures = {tex, tex}})
|
|
end
|
|
|
|
local item = self._test_wield
|
|
if not item then
|
|
item = player and player:get_wielded_item():get_name() or ""
|
|
end
|
|
update_held(self, item, self._swing)
|
|
end
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Footsteps and hurt feedback
|
|
--------------------------------------------------------------------------
|
|
|
|
--- Play the footstep of the node under an avatar at pos. Returns the
|
|
-- sound handle, or nil when there is nothing to step on.
|
|
function strata.avatar_footstep(pos)
|
|
local under = vector.round(vector.offset(pos, 0, -FEET_DROP, 0))
|
|
local def = core.registered_nodes[core.get_node(under).name]
|
|
local spec = def and def.sounds and def.sounds.footstep
|
|
if not spec then
|
|
return
|
|
end
|
|
return core.sound_play(spec, {pos = pos, max_hear_distance = strata.SOUND_HEAR_DISTANCE})
|
|
end
|
|
|
|
--- Flash a player's avatar and let everyone else hear the hit.
|
|
function strata.avatar_hurt_feedback(name, reason_type)
|
|
local avatar = strata.get_avatar(name)
|
|
if not avatar then
|
|
return
|
|
end
|
|
strata.flash(avatar)
|
|
core.sound_play(reason_type == "fall" and "player_falling_damage" or "player_damage", {
|
|
pos = avatar:get_pos(),
|
|
max_hear_distance = strata.SOUND_HEAR_DISTANCE,
|
|
exclude_player = name,
|
|
}, true)
|
|
end
|
|
|
|
core.register_on_player_hpchange(function(player, hp_change, reason)
|
|
if hp_change < 0 then
|
|
strata.avatar_hurt_feedback(player:get_player_name(), reason and reason.type)
|
|
end
|
|
end, false)
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Name tags: a waypoint per other avatar on each player's screen
|
|
--------------------------------------------------------------------------
|
|
|
|
--- Which of the other avatars a viewer at viewer_pos should see a name
|
|
-- tag for. positions maps player name -> avatar position.
|
|
function strata.nametag_targets(viewer, viewer_pos, positions)
|
|
local out = {}
|
|
for name, pos in pairs(positions) do
|
|
if name ~= viewer and math.abs(pos.x - viewer_pos.x) <= NAMETAG_RANGE_X
|
|
and math.abs(pos.y - viewer_pos.y) <= NAMETAG_RANGE_Y then
|
|
out[name] = vector.offset(pos, 0, NAMETAG_RISE, 0)
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
local tags = {} -- viewer name -> {shown name -> hud id}
|
|
|
|
local tag_timer = 0
|
|
core.register_globalstep(function(dtime)
|
|
tag_timer = tag_timer + dtime
|
|
if tag_timer < NAMETAG_INTERVAL then
|
|
return
|
|
end
|
|
tag_timer = 0
|
|
local players = core.get_connected_players()
|
|
local positions = {}
|
|
for _, player in ipairs(players) do
|
|
local name = player:get_player_name()
|
|
positions[name] = strata.get_avatar_pos(name)
|
|
end
|
|
for _, viewer in ipairs(players) do
|
|
local vname = viewer:get_player_name()
|
|
local mine = tags[vname] or {}
|
|
tags[vname] = mine
|
|
local targets = positions[vname] and strata.nametag_targets(vname, positions[vname], positions) or {}
|
|
for name, id in pairs(mine) do
|
|
if not targets[name] then
|
|
viewer:hud_remove(id)
|
|
mine[name] = nil
|
|
end
|
|
end
|
|
for name, pos in pairs(targets) do
|
|
if mine[name] then
|
|
viewer:hud_change(mine[name], "world_pos", pos)
|
|
else
|
|
mine[name] = viewer:hud_add({
|
|
type = "waypoint",
|
|
name = name,
|
|
text = "",
|
|
precision = 0,
|
|
number = NAMETAG_COLOR,
|
|
world_pos = pos,
|
|
alignment = {x = 0, y = -1},
|
|
})
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
|
|
core.register_on_leaveplayer(function(player)
|
|
tags[player:get_player_name()] = nil
|
|
end)
|