Skip to content

Commit

Permalink
demo - play sound; property animation - option to animate entity's tr…
Browse files Browse the repository at this point in the history
…ansform
  • Loading branch information
nem0 committed Nov 9, 2024
1 parent fb9ace6 commit c7e3616
Show file tree
Hide file tree
Showing 18 changed files with 348 additions and 69 deletions.
2 changes: 2 additions & 0 deletions data/maps/demo/audio/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CC0 https://opengameart.org/content/town-theme-rpg
CC0 https://opengameart.org/content/512-sound-effects-8-bit-style
Binary file added data/maps/demo/audio/sfx_coin_single1.ogg
Binary file not shown.
2 changes: 2 additions & 0 deletions data/maps/demo/audio/sfx_coin_single1.ogg.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
volume = 1.000
looped = false
38 changes: 38 additions & 0 deletions data/maps/demo/button.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local math = require "scripts/math"
local interactive = false

label = {}
player = {}
sound = -1

Editor.setPropertyType(this, "label", Editor.ENTITY_PROPERTY)
Editor.setPropertyType(this, "player", Editor.ENTITY_PROPERTY)
Editor.setPropertyType(this, "sound", Editor.RESOURCE_PROPERTY, "clip")

function playSound(sound)
local path = this.world.lua_script:getResourcePath(sound)
this.world:getModule("audio"):play(this, path, false)
end

function update(time_delta)
-- check if player is close
local dist_squared = math.distXZSquared(this.position, player.position)
interactive = dist_squared < 2
-- animate the label if player is close
label.property_animator.enabled = interactive
end

function onInputEvent(event : InputEvent)
-- check if player pressed "F" and is close
if interactive and event.type == "button" and event.device.type == "keyboard" then
if event.key_id == string.byte("F") then
if event.down then
-- play the sound
playSound(sound)
-- press (move) the button
this.property_animator.enabled = false
this.property_animator.enabled = true
end
end
end
end
16 changes: 16 additions & 0 deletions data/maps/demo/button_label.anp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
type = "Scale X",
keyframes = [
0, 15, 30],
values = [
10.000000, 20.000000, 10.000000]
},

{
type = "Scale Y",
keyframes = [
0, 15, 30],
values = [
10.000000, 20.000000, 10.000000]
},

8 changes: 8 additions & 0 deletions data/maps/demo/button_press.anp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
type = "Local position Z",
keyframes = [
0, 8, 15],
values = [
-0.220000, -0.150000, -0.220000]
},

Binary file added data/maps/demo/demo.unv
Binary file not shown.
11 changes: 11 additions & 0 deletions data/maps/demo/red.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
shader "/shaders/standard.hlsl"
backface_culling true
layer "default"
texture ""
texture ""
texture ""
uniform "Material color", { 1.000000, 0.000000, 0.000000, 1.000000 }
uniform "Roughness", 0.000000
uniform "Metallic", 0.000000
uniform "Emission", 0.000000
uniform "Translucency", 0.000000
File renamed without changes.
14 changes: 14 additions & 0 deletions data/scripts/math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ return {
return {a[1] * f, a[2] * f, a[3] * f}
end,

distSquared = function(a, b)
local xd = a[1] - b[1]
local yd = a[2] - b[2]
local zd = a[3] - b[3]

return xd * xd + yd * yd + zd * zd
end,

distXZSquared = function(a, b)
local xd = a[1] - b[1]
local zd = a[3] - b[3]

return xd * xd + zd * zd
end,
}
18 changes: 0 additions & 18 deletions data/tests/property_animation/property_animation.anp

This file was deleted.

Binary file removed data/universes/demo.unv
Binary file not shown.
Binary file removed data/universes/tests/property_animation.unv
Binary file not shown.
117 changes: 89 additions & 28 deletions src/animation/animation_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,23 +386,31 @@ struct AnimationModuleImpl final : AnimationModule {
return animator.resource ? animator.resource->getPath() : Path("");
}

bool isPropertyAnimatorEnabled(EntityRef entity) override
{
return !isFlagSet(m_property_animators[entity].flags, PropertyAnimator::DISABLED);
bool isPropertyAnimatorLooped(EntityRef entity) {
return isFlagSet(m_property_animators[entity].flags, PropertyAnimator::LOOPED);
}

bool isPropertyAnimatorEnabled(EntityRef entity) override {
return !isFlagSet(m_property_animators[entity].flags, PropertyAnimator::DISABLED);
}

void enablePropertyAnimator(EntityRef entity, bool enabled) override
{
void enablePropertyAnimator(EntityRef entity, bool enabled) override {
PropertyAnimator& animator = m_property_animators[entity];
bool is_enabled = !isFlagSet(animator.flags, PropertyAnimator::DISABLED);
if (enabled == is_enabled) return;

setFlag(animator.flags, PropertyAnimator::DISABLED, !enabled);
animator.time = 0;
if (!enabled)
{
if (!enabled) {
applyPropertyAnimator(entity, animator);
}
}

void setPropertyAnimatorLooped(EntityRef entity, bool looped) {
PropertyAnimator& animator = m_property_animators[entity];
setFlag(animator.flags, PropertyAnimator::LOOPED, looped);
}


Path getPropertyAnimation(EntityRef entity) override
{
Expand Down Expand Up @@ -636,38 +644,90 @@ struct AnimationModuleImpl final : AnimationModule {
}
}

void applyPropertyAnimator(EntityRef entity, PropertyAnimator& animator)
{
void applyPropertyAnimator(EntityRef entity, PropertyAnimator& animator) {
const bool is_looped = animator.flags & PropertyAnimator::LOOPED;
const PropertyAnimation* animation = animator.animation;
int frame = int(animator.time * animation->fps + 0.5f);
frame = frame % animation->curves[0].frames.back();
for (PropertyAnimation::Curve& curve : animation->curves)
{
float frame = animator.time * animation->fps;
DVec3 local_pos = m_world.getLocalTransform(entity).pos;
DVec3 pos = m_world.getPosition(entity);
Vec3 scale = m_world.getScale(entity);
bool set_local_pos = false;
bool set_pos = false;
bool set_scale = false;
if (is_looped) {
frame = fmodf(frame, (float)animation->curves[0].frames.back());
}
else {
frame = minimum(frame, (float)animation->curves[0].frames.back());
}
for (PropertyAnimation::Curve& curve : animation->curves) {
if (curve.frames.size() < 2) continue;
for (int i = 1, n = curve.frames.size(); i < n; ++i)
{
if (frame <= curve.frames[i])
{
for (int i = 1, n = curve.frames.size(); i < n; ++i) {
if (frame <= curve.frames[i]) {
float t = (frame - curve.frames[i - 1]) / float(curve.frames[i] - curve.frames[i - 1]);
float v = curve.values[i] * t + curve.values[i - 1] * (1 - t);
ComponentUID cmp;
cmp.type = curve.cmp_type;
cmp.module = m_world.getModule(cmp.type);
cmp.entity = entity;
ASSERT(curve.property->setter);
curve.property->set(cmp, -1, v);
switch (curve.type) {
case PropertyAnimation::CurveType::PROPERTY: {
ComponentUID cmp;
cmp.type = curve.cmp_type;
cmp.module = m_world.getModule(cmp.type);
cmp.entity = entity;
ASSERT(curve.property->setter);
curve.property->set(cmp, -1, v);
break;
}
case PropertyAnimation::CurveType::LOCAL_POS_X:
local_pos.x = v;
set_local_pos = true;
break;
case PropertyAnimation::CurveType::LOCAL_POS_Y:
local_pos.y = v;
set_local_pos = true;
break;
case PropertyAnimation::CurveType::LOCAL_POS_Z:
local_pos.z = v;
set_local_pos = true;
break;
case PropertyAnimation::CurveType::POS_X:
pos.x = v;
set_pos = true;
break;
case PropertyAnimation::CurveType::POS_Y:
pos.y = v;
set_pos = true;
break;
case PropertyAnimation::CurveType::POS_Z:
set_pos = true;
pos.z = v;
break;
case PropertyAnimation::CurveType::NOT_SET:
ASSERT(false);
break;
case PropertyAnimation::CurveType::SCALE_X:
scale.x = v;
set_scale = true;
break;
case PropertyAnimation::CurveType::SCALE_Y:
scale.y = v;
set_scale = true;
break;
case PropertyAnimation::CurveType::SCALE_Z:
scale.z = v;
set_scale = true;
break;
}
break;
}
}
}
if (set_pos) m_world.setPosition(entity, pos);
if (set_local_pos) m_world.setLocalPosition(entity, local_pos);
if (set_scale) m_world.setScale(entity, scale);
}


void updatePropertyAnimators(float time_delta)
{
void updatePropertyAnimators(float time_delta) {
PROFILE_FUNCTION();
for (int anim_idx = 0, c = m_property_animators.size(); anim_idx < c; ++anim_idx)
{
for (int anim_idx = 0, c = m_property_animators.size(); anim_idx < c; ++anim_idx) {
EntityRef entity = m_property_animators.getKey(anim_idx);
PropertyAnimator& animator = m_property_animators.at(anim_idx);
const PropertyAnimation* animation = animator.animation;
Expand Down Expand Up @@ -787,6 +847,7 @@ void AnimationModule::reflect(Engine& engine) {
.LUMIX_CMP(PropertyAnimator, "property_animator", "Animation / Property animator")
.LUMIX_PROP(PropertyAnimation, "Animation").resourceAttribute(PropertyAnimation::TYPE)
.prop<&AnimationModule::isPropertyAnimatorEnabled, &AnimationModule::enablePropertyAnimator>("Enabled")
.prop<&AnimationModule::isPropertyAnimatorLooped, &AnimationModule::setPropertyAnimatorLooped>("Looped")
.LUMIX_CMP(Animator, "animator", "Animation / Animator")
.function<(void (AnimationModule::*)(EntityRef, u32, float))&AnimationModule::setAnimatorInput>("setFloatInput", "AnimationModule::setAnimatorInput")
.function<(void (AnimationModule::*)(EntityRef, u32, bool))&AnimationModule::setAnimatorInput>("setBoolInput", "AnimationModule::setAnimatorInput")
Expand Down
2 changes: 2 additions & 0 deletions src/animation/animation_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct AnimationModule : IModule {
virtual void setPropertyAnimation(EntityRef entity, const Path& path) = 0;
virtual bool isPropertyAnimatorEnabled(EntityRef entity) = 0;
virtual void enablePropertyAnimator(EntityRef entity, bool enabled) = 0;
virtual bool isPropertyAnimatorLooped(EntityRef entity) = 0;
virtual void setPropertyAnimatorLooped(EntityRef entity, bool looped) = 0;
virtual struct Animation* getAnimableAnimation(EntityRef entity) = 0;
virtual Path getAnimation(EntityRef entity) = 0;
virtual void setAnimation(EntityRef entity, const Path& path) = 0;
Expand Down
Loading

0 comments on commit c7e3616

Please sign in to comment.