Skip to content

Commit

Permalink
Changes before pocketpy 2.0 upgrade (#231)
Browse files Browse the repository at this point in the history
* Temp changes to get variable delta time for game proto.
* Disabling collision rendering temporarily and using script instance data to create python instance from packed scene.
* Fixing bug with changing an animation from within the 'animation_finished' event.
  • Loading branch information
Chukobyte authored Sep 18, 2024
1 parent 9b6431f commit b5fc5b8
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 32 deletions.
4 changes: 4 additions & 0 deletions crescent_py_api/pocketpy/crescent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,10 @@ def get_time_dilation() -> float:
def get_delta_time() -> float:
return crescent_internal.world_get_delta_time()

@staticmethod
def get_variable_delta_time() -> float:
return crescent_internal.world_get_variable_delta_time()


class CollisionHandler:
"""
Expand Down
4 changes: 4 additions & 0 deletions crescent_py_api/pocketpy/crescent_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ def world_get_delta_time() -> float:
return 0.1


def world_get_variable_delta_time() -> float:
return 0.1


# --- Audio Source --- #

def audio_source_get_pitch(path: str) -> float:
Expand Down
2 changes: 2 additions & 0 deletions engine/src/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "game_properties.h"
#include "engine_context.h"
#include "embedded_assets.h"
#include "world.h"
#include "utils/command_line_args_util.h"
#include "ecs/ecs_manager.h"
#include "scene/scene_manager.h"
Expand Down Expand Up @@ -218,6 +219,7 @@ void cre_process_game_update() {

// Variable Time Step
const f32 variableDeltaTime = (f32) (ska_get_ticks() - lastFrameTime) / (f32) MILLISECONDS_PER_TICK;
cre_world_set_frame_delta_time(variableDeltaTime);
ska_ecs_system_event_update_systems(variableDeltaTime);

// Fixed Time Step
Expand Down
24 changes: 11 additions & 13 deletions engine/src/core/ecs/components/animated_sprite_component.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void animated_sprite_component_add_animation(AnimatedSpriteComponent* animatedSp
bool animated_sprite_component_set_animation(AnimatedSpriteComponent* animatedSpriteComponent, const char* name) {
for (size_t i = 0; i < animatedSpriteComponent->animationCount; i++) {
if (strcmp(animatedSpriteComponent->animations[i].name, name) == 0) {
animatedSpriteComponent->currentAnimation = animatedSpriteComponent->animations[i];
animatedSpriteComponent->currentAnimation = &animatedSpriteComponent->animations[i];
return true;
}
}
Expand All @@ -67,7 +67,7 @@ bool animated_sprite_component_play_animation(AnimatedSpriteComponent *animatedS
// First checks if new animation, if not just play the currently set animation (if not playing)
bool isPlayingNewAnimation = false;
bool playAnimationSuccess = false;
if (strcmp(animatedSpriteComponent->currentAnimation.name, name) != 0) {
if (strcmp(animatedSpriteComponent->currentAnimation->name, name) != 0) {
playAnimationSuccess = animated_sprite_component_set_animation(animatedSpriteComponent, name);
if (playAnimationSuccess) {
isPlayingNewAnimation = true;
Expand Down Expand Up @@ -115,26 +115,24 @@ AnimatedSpriteComponent* animated_sprite_component_data_copy_to_animated_sprite(
copiedNode->flipH = animatedSpriteComponentData->flipH;
copiedNode->flipV = animatedSpriteComponentData->flipV;
copiedNode->staggerStartAnimationTimes = animatedSpriteComponentData->staggerStartAnimationTimes;
ska_strcpy(copiedNode->currentAnimation.name, animatedSpriteComponentData->currentAnimation.name);
for (size_t animationIndex = 0; animationIndex < animatedSpriteComponentData->animationCount; animationIndex++) {
const AnimationData* animationData = &animatedSpriteComponentData->animations[animationIndex];
CreAnimation animation;
ska_strcpy(animation.name, animationData->name);
animation.doesLoop = animationData->doesLoop;
animation.speed = animationData->speed;
animation.isValid = true;
animation.currentFrame = 0;
animation.frameCount = animationData->frameCount;
CreAnimation* animation = &copiedNode->animations[animationIndex];
ska_strcpy(animation->name, animationData->name);
animation->doesLoop = animationData->doesLoop;
animation->speed = animationData->speed;
animation->isValid = true;
animation->currentFrame = 0;
animation->frameCount = animationData->frameCount;
for (size_t frameIndex = 0; (int32) frameIndex < animationData->frameCount; frameIndex++) {
const AnimationFrameData* animationFrameData = &animationData->animationFrames[frameIndex];
CreAnimationFrame animationFrame;
animationFrame.texture = ska_asset_manager_get_texture(animationFrameData->texturePath);
animationFrame.drawSource = animationFrameData->drawSource;
animationFrame.frame = animationFrameData->frame;
animation.animationFrames[frameIndex] = animationFrame;
animation->animationFrames[frameIndex] = animationFrame;
}
copiedNode->animations[animationIndex] = animation;
if (strcmp(animatedSpriteComponentData->currentAnimation.name, animation.name) == 0) {
if (strcmp(animatedSpriteComponentData->currentAnimation.name, animation->name) == 0) {
copiedNode->currentAnimation = animation;
}
}
Expand Down
2 changes: 1 addition & 1 deletion engine/src/core/ecs/components/animated_sprite_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ typedef struct AnimatedSpriteAnimationFinishedPayload {

typedef struct AnimatedSpriteComponent {
CreAnimation animations[ANIMATED_SPRITE_COMPONENT_MAX_ANIMATIONS];
CreAnimation currentAnimation;
CreAnimation* currentAnimation;
size_t animationCount;
SkaColor modulate;
bool isPlaying;
Expand Down
28 changes: 15 additions & 13 deletions engine/src/core/ecs/systems/animated_sprite_rendering_ec_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,43 @@ void animated_sprite_render(SkaECSSystem* system) {
const SkaEntity entity = system->entities[i];
Transform2DComponent* spriteTransformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX);
AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX);
CreAnimationFrame currentFrame = animatedSpriteComponent->currentAnimation.animationFrames[animatedSpriteComponent->currentAnimation.currentFrame];
CreAnimationFrame* currentFrame = &animatedSpriteComponent->currentAnimation->animationFrames[animatedSpriteComponent->currentAnimation->currentFrame];
if (animatedSpriteComponent->isPlaying) {
const f32 entityTimeDilation = cre_scene_manager_get_node_full_time_dilation(entity);
const f32 spriteCurrentTickTime = (f32) currentTickTime + (f32) animatedSpriteComponent->randomStaggerTime;
const int32 tickRate = (int32) (((spriteCurrentTickTime - (f32) animatedSpriteComponent->startAnimationTickTime) / (f32) animatedSpriteComponent->currentAnimation.speed) * entityTimeDilation);
const int32 newIndex = tickRate % animatedSpriteComponent->currentAnimation.frameCount;
if (newIndex != animatedSpriteComponent->currentAnimation.currentFrame) {
const int32 tickRate = (int32) (((spriteCurrentTickTime - (f32) animatedSpriteComponent->startAnimationTickTime) / (f32) animatedSpriteComponent->currentAnimation->speed) * entityTimeDilation);
const int32 newIndex = tickRate % animatedSpriteComponent->currentAnimation->frameCount;
if (newIndex != animatedSpriteComponent->currentAnimation->currentFrame) {
// Notify observers that frame has changed
ska_event_notify_observers(&animatedSpriteComponent->onFrameChanged, &(SkaSubjectNotifyPayload){
.data = &(AnimatedSpriteFrameChangedPayload){ .entity = entity, .newFrame = newIndex }
});

currentFrame = animatedSpriteComponent->currentAnimation.animationFrames[newIndex];
if (newIndex + 1 == animatedSpriteComponent->currentAnimation.frameCount) {
currentFrame = &animatedSpriteComponent->currentAnimation->animationFrames[newIndex];
const CreAnimation* animationBeforeNotification = animatedSpriteComponent->currentAnimation;
if (newIndex + 1 == animatedSpriteComponent->currentAnimation->frameCount) {
// Notify the observers that the animation has finished
ska_event_notify_observers(&animatedSpriteComponent->onAnimationFinished, &(SkaSubjectNotifyPayload){
.data = &(AnimatedSpriteAnimationFinishedPayload){ .entity = entity, .animation = &animatedSpriteComponent->currentAnimation }
.data = &(AnimatedSpriteAnimationFinishedPayload){ .entity = entity, .animation = animatedSpriteComponent->currentAnimation }
});
if (!animatedSpriteComponent->currentAnimation.doesLoop) {
if (!animatedSpriteComponent->currentAnimation->doesLoop) {
animatedSpriteComponent->isPlaying = false;
}
}
animatedSpriteComponent->currentAnimation.currentFrame = newIndex;
// Make sure it's the same animation before assinging new index, if not reset to 0 index
animatedSpriteComponent->currentAnimation->currentFrame = animationBeforeNotification == animatedSpriteComponent->currentAnimation ? newIndex : 0;
}
}
const CRECamera2D* renderCamera = spriteTransformComp->ignoreCamera ? defaultCamera : camera2D;
const SceneNodeRenderResource renderResource = cre_scene_manager_get_scene_node_global_render_resource(entity, spriteTransformComp, &animatedSpriteComponent->origin);
const SkaSize2D destinationSize = {
.w = currentFrame.drawSource.w * renderCamera->zoom.x,
.h = currentFrame.drawSource.h * renderCamera->zoom.y
.w = currentFrame->drawSource.w * renderCamera->zoom.x,
.h = currentFrame->drawSource.h * renderCamera->zoom.y
};

ska_renderer_queue_sprite_draw(
currentFrame.texture,
currentFrame.drawSource,
currentFrame->texture,
currentFrame->drawSource,
destinationSize,
animatedSpriteComponent->modulate,
animatedSpriteComponent->flipH,
Expand Down
2 changes: 1 addition & 1 deletion engine/src/core/ecs/systems/collision_ec_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void cre_collision_ec_system_create_and_register() {
systemTemplate.on_entity_unregistered_func = on_entity_unregistered;
systemTemplate.on_entity_entered_scene_func = on_entity_entered_scene;
systemTemplate.fixed_update_func = fixed_update;
systemTemplate.render_func = collision_render;
// systemTemplate.render_func = collision_render; // TODO: Make it based on if collision debug is enabled
SKA_ECS_SYSTEM_REGISTER_FROM_TEMPLATE(&systemTemplate, Transform2DComponent, Collider2DComponent);
}

Expand Down
16 changes: 15 additions & 1 deletion engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "../../../../camera/camera_manager.h"
#include "../../../../physics/collision/collision.h"
#include "../../../../ecs/ecs_globals.h"
#include "../../../../ecs/components/script_component.h"

// Shader Instance
static int cre_pkpy_api_shader_instance_delete(pkpy_vm* vm);
Expand Down Expand Up @@ -115,6 +116,7 @@ static int cre_pkpy_api_camera2d_unfollow_node(pkpy_vm* vm);
static int cre_pkpy_api_world_set_time_dilation(pkpy_vm* vm);
static int cre_pkpy_api_world_get_time_dilation(pkpy_vm* vm);
static int cre_pkpy_api_world_get_delta_time(pkpy_vm* vm);
static int cre_pkpy_api_world_get_variable_delta_time(pkpy_vm* vm);

// Audio Source
static int cre_pkpy_api_audio_source_set_pitch(pkpy_vm* vm);
Expand Down Expand Up @@ -318,6 +320,7 @@ void cre_pkpy_api_load_internal_modules(pkpy_vm* vm) {
{.signature = "world_set_time_dilation(time_dilation: float) -> None", .function = cre_pkpy_api_world_set_time_dilation},
{.signature = "world_get_time_dilation() -> float", .function = cre_pkpy_api_world_get_time_dilation},
{.signature = "world_get_delta_time() -> float", .function = cre_pkpy_api_world_get_delta_time},
{.signature = "world_get_variable_delta_time() -> float", .function = cre_pkpy_api_world_get_variable_delta_time},
// Audio Source
{.signature = "audio_source_set_pitch(path: str, pitch: float) -> None", .function = cre_pkpy_api_audio_source_set_pitch},
{.signature = "audio_source_get_pitch(path: str) -> float", .function = cre_pkpy_api_audio_source_get_pitch},
Expand Down Expand Up @@ -1109,6 +1112,11 @@ int cre_pkpy_api_world_get_delta_time(pkpy_vm* vm) {
return 1;
}

int cre_pkpy_api_world_get_variable_delta_time(pkpy_vm* vm) {
pkpy_push_float(vm, (f64)cre_world_get_frame_delta_time());
return 1;
}

//--- AUDIO SOURCE ---//

int cre_pkpy_api_audio_source_set_pitch(pkpy_vm* vm) {
Expand Down Expand Up @@ -1217,7 +1225,13 @@ int cre_pkpy_api_packed_scene_create_instance(pkpy_vm* vm) {
JsonSceneNode* sceneNode = cre_scene_template_cache_get_scene(cacheId);
SceneTreeNode* rootNode = cre_scene_manager_stage_scene_nodes_from_json(sceneNode);

cre_pkpy_script_context_create_instance(rootNode->entity, CRE_PKPY_MODULE_NAME_CRESCENT, node_get_base_type_string(sceneNode->type));
// Get class path and name from root node if it has a script component
if (sceneNode->components[SCRIPT_COMPONENT_INDEX] != NULL) {
ScriptComponent* scriptComp = (ScriptComponent*)sceneNode->components[SCRIPT_COMPONENT_INDEX];
cre_pkpy_script_context_create_instance(rootNode->entity, scriptComp->classPath, scriptComp->className);
} else {
cre_pkpy_script_context_create_instance(rootNode->entity, CRE_PKPY_MODULE_NAME_CRESCENT, node_get_base_type_string(sceneNode->type));
}
cre_pkpy_entity_instance_cache_push_entity_instance(vm, rootNode->entity);
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ int32 cre_pkpy_api_node_get_parent(pkpy_vm* vm) {
const SkaEntity childEntity = (SkaEntity)childEntityId;
if (cre_scene_manager_has_entity_tree_node(childEntity)) {
const SceneTreeNode* treeNode = cre_scene_manager_get_entity_tree_node(childEntity);
cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(treeNode->entity);
if (treeNode->parent) {
cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(treeNode->parent->entity);
} else {
pkpy_push_none(vm);
}
return 1;
}
return 0;
Expand Down Expand Up @@ -680,7 +684,7 @@ int32 cre_pkpy_api_animated_sprite_set_current_animation_frame(pkpy_vm* vm) {

const SkaEntity entity = (SkaEntity)pyEntityId;
AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX);
animatedSpriteComponent->currentAnimation.currentFrame = ska_math_clamp_int(pyFrame, 0, animatedSpriteComponent->currentAnimation.frameCount - 1);
animatedSpriteComponent->currentAnimation->currentFrame = ska_math_clamp_int(pyFrame, 0, animatedSpriteComponent->currentAnimation->frameCount - 1);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,10 @@
" def get_delta_time() -> float:\n"\
" return crescent_internal.world_get_delta_time()\n"\
"\n"\
" @staticmethod\n"\
" def get_variable_delta_time() -> float:\n"\
" return crescent_internal.world_get_variable_delta_time()\n"\
"\n"\
"\n"\
"class CollisionHandler:\n"\
" \"\"\"\n"\
Expand Down
11 changes: 10 additions & 1 deletion engine/src/core/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// Struct is private now, may want to expose later...
typedef struct CreWorld {
f32 timeDilation;
f32 variableDeltaTime; // frame's variable delta time
} CreWorld;

CreWorld globalWorld = { .timeDilation = 1.0f };
CreWorld globalWorld = { .timeDilation = 1.0f, .variableDeltaTime = 0.0f };

void cre_world_set_time_dilation(f32 timeDilation) {
globalWorld.timeDilation = timeDilation;
Expand All @@ -14,3 +15,11 @@ void cre_world_set_time_dilation(f32 timeDilation) {
f32 cre_world_get_time_dilation() {
return globalWorld.timeDilation;
}

void cre_world_set_frame_delta_time(f32 frameDeltaTime) {
globalWorld.variableDeltaTime = frameDeltaTime;
}

f32 cre_world_get_frame_delta_time() {
return globalWorld.variableDeltaTime;
}
2 changes: 2 additions & 0 deletions engine/src/core/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

void cre_world_set_time_dilation(f32 timeDilation);
f32 cre_world_get_time_dilation();
void cre_world_set_frame_delta_time(f32 frameDeltaTime);
f32 cre_world_get_frame_delta_time();

0 comments on commit b5fc5b8

Please sign in to comment.