Skip to content

Commit

Permalink
Merge pull request #303 from Laguna1989/Feature/FixesFromFGJ2023
Browse files Browse the repository at this point in the history
Add fixes/improvements from FGJ2023
  • Loading branch information
Laguna1989 authored Nov 8, 2023
2 parents 8b83929 + ec8db36 commit 79a72a2
Show file tree
Hide file tree
Showing 47 changed files with 807 additions and 93 deletions.
2 changes: 2 additions & 0 deletions impl/gamelib/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ void Hud::doCreate()
m_scoreP1Text = std::make_shared<jt::Text>();
m_scoreP1Text = jt::dh::createText(renderTarget(), "", 16, jt::Color { 248, 249, 254 });
m_scoreP1Text->setTextAlign(jt::Text::TextAlign::LEFT);
m_scoreP1Text->setIgnoreCamMovement(true);
m_scoreP1Text->setPosition({ 10, 4 });

m_scoreP1Display = std::make_shared<ScoreDisplay>(m_scoreP1Text, "P1 Score: ");

m_scoreP2Text = jt::dh::createText(renderTarget(), "", 16, jt::Color { 248, 249, 254 });
m_scoreP2Text->setTextAlign(jt::Text::TextAlign::RIGHT);
m_scoreP2Text->setIgnoreCamMovement(true);
m_scoreP2Text->setPosition({ GP::GetScreenSize().x - 10, 4 });

m_scoreP2Display = std::make_shared<ScoreDisplay>(m_scoreP2Text, "P2 Score: ");
Expand Down
50 changes: 46 additions & 4 deletions impl/jamtemplate/common/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <nlohmann.hpp>
#include <sprite.hpp>
#include <strutils.hpp>
#include <system_helper.hpp>
#include <texture_manager_interface.hpp>
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -186,7 +187,7 @@ bool jt::Animation::hasAnimation(std::string const& animationName) const
return (m_frames.count(animationName) != 0);
}

std::vector<std::string> jt::Animation::getAllAvailableAnimationsNames() const
std::vector<std::string> jt::Animation::getAllAvailableAnimationNames() const
{
std::vector<std::string> names;
names.resize(m_frames.size());
Expand All @@ -196,6 +197,16 @@ std::vector<std::string> jt::Animation::getAllAvailableAnimationsNames() const
return names;
}

std::string jt::Animation::getRandomAnimationName() const
{
if (m_frames.empty()) {
throw std::invalid_argument {
"can not get random animation name if no animation has been added"
};
}
return jt::SystemHelper::select_randomly(m_frames.cbegin(), m_frames.cend())->first;
}

void jt::Animation::play(std::string const& animationName, size_t startFrameIndex, bool restart)
{
m_isValid = hasAnimation(animationName);
Expand Down Expand Up @@ -333,7 +344,7 @@ void jt::Animation::doUpdate(float elapsed)
m_frameTime -= m_time[m_currentAnimName][m_currentIdx];
m_currentIdx++;
if (m_currentIdx >= m_frames.at(m_currentAnimName).size()) {
if (getIsLooping()) {
if (getCurrentAnimationIsLooping()) {
m_currentIdx = 0;
} else {
m_currentIdx = m_frames.at(m_currentAnimName).size() - 1;
Expand Down Expand Up @@ -370,7 +381,23 @@ float jt::Animation::getCurrentAnimationSingleFrameTime() const

float jt::Animation::getCurrentAnimTotalTime() const
{
return getCurrentAnimationSingleFrameTime() * getNumberOfFramesInCurrentAnimation();
float sum = 0.0f;
for (auto frameTime : m_time.at(m_currentAnimName)) {
sum += frameTime;
}
return sum;
}

float jt::Animation::getAnimTotalTimeFor(std::string const& animName)
{
if (!hasAnimation(animName)) {
throw std::invalid_argument { "no animation with name " + animName };
}
float sum = 0.0f;
for (auto frameTime : m_time.at(animName)) {
sum += frameTime;
}
return sum;
}

std::size_t jt::Animation::getNumberOfFramesInCurrentAnimation() const
Expand All @@ -380,14 +407,22 @@ std::size_t jt::Animation::getNumberOfFramesInCurrentAnimation() const

std::string jt::Animation::getCurrentAnimationName() const { return m_currentAnimName; }

bool jt::Animation::getIsLooping() const
bool jt::Animation::getCurrentAnimationIsLooping() const
{
if (!hasAnimation(m_currentAnimName)) {
return true;
}
return m_isLooping.at(m_currentAnimName);
}

bool jt::Animation::getIsLoopingFor(std::string const& animName) const
{
if (!hasAnimation(animName)) {
throw std::invalid_argument { "no animation with name " + animName };
}
return m_isLooping.at(animName);
}

void jt::Animation::setLooping(std::string const& animName, bool isLooping)
{
if (!hasAnimation(animName)) {
Expand All @@ -396,6 +431,13 @@ void jt::Animation::setLooping(std::string const& animName, bool isLooping)
m_isLooping[animName] = isLooping;
}

void jt::Animation::setLoopingAll(bool isLooping)
{
for (auto& l : m_isLooping) {
l.second = isLooping;
}
}

std::size_t jt::Animation::getCurrentAnimationFrameIndex() const { return m_currentIdx; }

void jt::Animation::setFrameTimes(
Expand Down
56 changes: 44 additions & 12 deletions impl/jamtemplate/common/animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,15 @@ class Animation : public DrawableImpl {
bool hasAnimation(std::string const& animationName) const;

/// Get all animation names
/// \return the vector of the animation names
std::vector<std::string> getAllAvailableAnimationsNames() const;
/// \return vector of the animation names
std::vector<std::string> getAllAvailableAnimationNames() const;

/// Get a random animation name
///
/// Will raise an exception if no animations have been added.
///
/// \return random animation name
std::string getRandomAnimationName() const;

/// Start playing an animation from the pool
///
Expand All @@ -81,15 +88,30 @@ class Animation : public DrawableImpl {
/// continue if already playing
void play(std::string const& animationName, size_t startFrameIndex = 0, bool restart = false);

/// Set animation to looping
/// true by default.
/// Set animation looping
/// By default an animations is looping (true).
/// If an animation is not looping, it will remain at the last frame of the animation.
/// \param isLooping value
void setLooping(std::string const& animName, bool isLooping);

/// Get looping
/// Set all animations looping
/// By default an animations is looping (true).
/// If an animation is not looping, it will remain at the last frame of the animation.
/// \param isLooping
void setLoopingAll(bool isLooping);

/// Get looping value for currently playing animation
///
/// Returns false if no animation is playing
///
/// \return true if animation is looping, false otherwise
bool getIsLooping() const;
bool getCurrentAnimationIsLooping() const;

/// Get looping value for a specific animation
/// Raises an exception if no animation with animName found
/// \param animName the animation to check
/// \return
bool getIsLoopingFor(std::string const& animName) const;

void setColor(jt::Color const& col) override;
jt::Color getColor() const override;
Expand All @@ -112,32 +134,42 @@ class Animation : public DrawableImpl {

/// Get the frame time for one single frame in the current animation
///
/// \return the time set in add for the currently playing animation
/// \return time set in add for the currently playing animation
/// will raise an exception if no valid animation is playing
float getCurrentAnimationSingleFrameTime() const;

/// Get the total time of the current animation
///
/// \return the time for the complete animation to play
/// \return time for the complete animation to play
float getCurrentAnimTotalTime() const;

/// Get the total time of the animation with name animName
///
/// Raises an exception if animName has not been added
///
/// \param animName the name of the animation
/// \return time for the complete animation to play
float getAnimTotalTimeFor(std::string const& animName);

/// Get the number of frames in this animation
///
/// \return the number of frames in this animation
/// \return number of frames in this animation
std::size_t getNumberOfFramesInCurrentAnimation() const;

/// Get the name of the current animation
///
/// \return the name of the currently playing animation
/// \return name of the currently playing animation
std::string getCurrentAnimationName() const;

std::size_t getCurrentAnimationFrameIndex() const;

/// Set the animation speed
/// \param factor the factor. Normal value is 1.0, can be in range from -inf to inf.
/// \param factor the animation speed factor. Normal value is 1.0, can be in range from -inf to
/// inf.
void setAnimationSpeedFactor(float factor);

/// Get the animation speed
/// \return the factor. Normal value is 1.0, can be in range from -inf to inf.
/// \return animation speed factor. Normal value is 1.0, can be in range from -inf to inf.
float getAnimationSpeedFactor() const;

private:
Expand Down
4 changes: 2 additions & 2 deletions impl/jamtemplate/common/audio/audio/audio_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ class AudioInterface {
virtual ~AudioInterface() = default;

// no copy, no move
AudioInterface(const AudioInterface&) = delete;
AudioInterface(AudioInterface const&) = delete;
AudioInterface(AudioInterface&&) = delete;
AudioInterface& operator=(const AudioInterface&) = delete;
AudioInterface& operator=(AudioInterface const&) = delete;
AudioInterface& operator=(AudioInterface&&) = delete;

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ jt::LoggingSoundFadeManager::LoggingSoundFadeManager(
void jt::LoggingSoundFadeManager::volumeFade(std::weak_ptr<SoundInterface> sound,
float durationInSeconds, float startVolume, float endVolume)
{
m_logger.info("Create sound fade", { "jt", "audio", "sound fade" });
m_logger.debug("Create sound fade", { "jt", "audio", "sound fade" });
m_decoratee.volumeFade(sound, durationInSeconds, startVolume, endVolume);
}

void jt::LoggingSoundFadeManager::update(float elapsed) {
void jt::LoggingSoundFadeManager::update(float elapsed)
{
m_logger.verbose("sound fade manager update", { "jt", "audio", "sound fade" });
m_decoratee.update(elapsed); }
m_decoratee.update(elapsed);
}

size_t jt::LoggingSoundFadeManager::size() const {
size_t jt::LoggingSoundFadeManager::size() const
{
m_logger.verbose("sound fade manager size", { "jt", "audio", "sound fade" });
return m_decoratee.size(); }
return m_decoratee.size();
}
2 changes: 0 additions & 2 deletions impl/jamtemplate/common/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jt::Bar::Bar(
{
m_shapeFull->makeRect(jt::Vector2f { m_width, m_height }, textureManager);
m_shapeFull->setColor(jt::colors::Gray);
m_shapeFull->setIgnoreCamMovement(true);

if (m_horizontal) {
auto const progressHeightFactor = 0.9f;
Expand All @@ -27,7 +26,6 @@ jt::Bar::Bar(
m_shapeProgress->setPosition(jt::Vector2f { 0 + 1, m_height });
}
m_shapeProgress->setColor(jt::colors::White);
m_shapeProgress->setIgnoreCamMovement(true);
}

void jt::Bar::setFrontColor(jt::Color const& col) { m_shapeProgress->setColor(col); }
Expand Down
5 changes: 5 additions & 0 deletions impl/jamtemplate/common/box2dwrapper/box2d_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ void jt::Box2DObject::addVelocity(jt::Vector2f const& v)
m_body->SetLinearVelocity(Conversion::vec(oldV + v));
}

void jt::Box2DObject::addForceToCenter(jt::Vector2f const& f)
{
m_body->ApplyForceToCenter(Conversion::vec(f), true);
}

float jt::Box2DObject::getRotation() const { return jt::MathHelper::rad2deg(m_body->GetAngle()); }

b2Body* jt::Box2DObject::getB2Body() { return m_body; }
Expand Down
28 changes: 16 additions & 12 deletions impl/jamtemplate/common/box2dwrapper/box2d_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,32 @@ class Box2DObject {

virtual ~Box2DObject();

/// Get the position
/// \return the position
/// Get position
/// \return position
jt::Vector2f getPosition() const;

/// Set the position (overwrite box2d simulation)
/// \param position the new position
/// Set position (overwrite box2d simulation)
/// \param position new position
void setPosition(jt::Vector2f const& position);

/// Get the velocity
/// \return the velocity
/// Get velocity
/// \return velocity
jt::Vector2f getVelocity() const;

/// Set the velocity (overwrite box2d simulation)
/// \param v the new velocity
/// Set velocity (overwrite box2d simulation)
/// \param v new velocity
void setVelocity(jt::Vector2f const& v);

/// Add the velocity (overwirte box2d simulation)
/// \param v the velocity to be added
/// Add velocity (overwrite box2d simulation)
/// \param v velocity to be added
void addVelocity(jt::Vector2f const& v);

/// Get the rotation in degree
/// \return the rotation in degree
/// Add Force to center
/// \param f force
void addForceToCenter(Vector2f const& f);

/// Get rotation in degree
/// \return rotation in degree
float getRotation() const;

/// Get the low level Box2d body pointer
Expand Down
1 change: 0 additions & 1 deletion impl/jamtemplate/common/ease/ease_from_points.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "ease_from_points.hpp"
#include <ease/linear.hpp>
#include <algorithm>
#include <stdexcept>

Expand Down
8 changes: 4 additions & 4 deletions impl/jamtemplate/common/lerp.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef JAMTEMPLATE_LERP_HPP
#define JAMTEMPLATE_LERP_HPP

#include <linterp.hpp>
#include <cassert>
#include <cmath>

namespace jt {
Expand All @@ -13,23 +13,23 @@ static T cosine(T const& a, T const& b, T const& t)
{
assert(t >= 0 && t <= 1);
float tRemapCosine = (1.0f - static_cast<float>(cos(t * 3.1415926f))) * 0.5f;
return linear(a, b, tRemapCosine);
return std::lerp(a, b, tRemapCosine);
}

template <typename T>
static T cubic(T const& a, T const& b, T const& t)
{
assert(t >= 0 && t <= 1);
float cub = t * t * t;
return linear(a, b, cub);
return std::lerp(a, b, cub);
}

template <typename T>
static T cubicInvers(T const& a, T const& b, T const& t)
{
assert(t >= 0 && t <= 1);
float cub = (1 - t) * (1 - t) * (1 - t);
return linear(a, b, cub);
return std::lerp(a, b, cub);
}

template <typename T>
Expand Down
7 changes: 4 additions & 3 deletions impl/jamtemplate/common/linterp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define JAMTEMPLATE_LINTERP_HPP

#include <assert.h>
#include <cmath>

namespace jt {

Expand All @@ -21,12 +22,12 @@ static T precheck(T const& ti)

namespace Lerp {

// linear interpolation between values a and b with t between 0 and 1
/// linear interpolation between values a and b with t between 0 and 1
template <typename T>
static T linear(T const& a, T const& b, T const& ti)
{
auto t = precheck(ti);
return (1.0f - t) * a + t * b;
auto const t = precheck(ti);
return std::lerp(a, b, t);
}

} // namespace Lerp
Expand Down
Loading

0 comments on commit 79a72a2

Please sign in to comment.