Skip to content

Commit

Permalink
Merge pull request #77 from fallahn/golf-1.12
Browse files Browse the repository at this point in the history
Golf 1.12.4
  • Loading branch information
fallahn authored May 14, 2023
2 parents ce9ecf7 + 1234265 commit 6ed2d9d
Show file tree
Hide file tree
Showing 24 changed files with 305 additions and 59 deletions.
19 changes: 16 additions & 3 deletions crogine/include/crogine/ecs/components/Sprite.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*-----------------------------------------------------------------------
Matt Marchant 2017 - 2020
Matt Marchant 2017 - 2023
http://trederia.blogspot.com
crogine - Zlib license.
Expand Down Expand Up @@ -66,12 +66,25 @@ namespace cro
class CRO_EXPORT_API Sprite final
{
public:
/*!
\brief Default constructor
*/
Sprite();

/*!
\brief Construct a sprite with the given Texture
\brief Calls the default constructor passing in the desired initial BlendMode
By default Sprites use Material::BlendMode::Alpha. This blend mode is applied
to any Drawable2D or Model material when the Sprite is first added to the scene.
To modify the blend mode at runtime use Drawable2D::setBlendMode() or
Model::setMaterial() depending on whether the sprite is 2D or 3D.
*/
explicit Sprite(Material::BlendMode mode);

/*!
\brief Construct a sprite with the given Texture, and optional BlendMode
*/
explicit Sprite(const Texture&);
explicit Sprite(const Texture&, Material::BlendMode = Material::BlendMode::Alpha);

/*!
\brief Sets the texture used to render this sprite.
Expand Down
2 changes: 1 addition & 1 deletion crogine/include/crogine/ecs/systems/ModelRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace cro
\param cameraIndex The index of the camera's drawlist
\param passIndex the ID of the pass to query
*/
std::size_t getVisibleCount(std::size_t cameraIndex, std::int32_t passIndex) const;
std::size_t getVisibleCount(std::size_t cameraIndex, std::int32_t passIndex = 0) const;

void onEntityAdded(Entity) override;

Expand Down
4 changes: 4 additions & 0 deletions crogine/include/crogine/graphics/Colour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ namespace cro
float* asArray() { return &r; }
const float* asArray() const { return &r; }

explicit operator std::uint32_t() const;
explicit operator glm::vec4() const;
explicit operator float* ();
explicit operator const float* () const;

private:

Expand Down
2 changes: 1 addition & 1 deletion crogine/src/core/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void winAbort(int)
#include <algorithm>

#ifdef CRO_DEBUG_
#define DEBUG_NO_CONTROLLER
//#define DEBUG_NO_CONTROLLER
#endif // CRO_DEBUG_

using namespace cro;
Expand Down
16 changes: 13 additions & 3 deletions crogine/src/ecs/components/Sprite.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*-----------------------------------------------------------------------
Matt Marchant 2017 - 2020
Matt Marchant 2017 - 2023
http://trederia.blogspot.com
crogine - Zlib license.
Expand Down Expand Up @@ -42,8 +42,18 @@ Sprite::Sprite()

}

Sprite::Sprite(const Texture& texture)
: Sprite()
Sprite::Sprite(Material::BlendMode mode)
: m_texture (nullptr),
m_colour (Colour::White),
m_dirtyFlags (DirtyFlags::All),
m_overrideBlendMode (true),
m_blendMode (mode)
{

}

Sprite::Sprite(const Texture& texture, Material::BlendMode mode)
: Sprite(mode)
{
setTexture(texture);
}
Expand Down
2 changes: 1 addition & 1 deletion crogine/src/ecs/systems/ModelRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void ModelRenderer::render(Entity camera, const RenderTarget& rt)

std::size_t ModelRenderer::getVisibleCount(std::size_t cameraIndex, std::int32_t passIndex) const
{
CRO_ASSERT(m_drawLists.size() < cameraIndex, "");
CRO_ASSERT(cameraIndex < m_drawLists.size(), "");
switch (passIndex)
{
default: return 0;
Expand Down
15 changes: 14 additions & 1 deletion crogine/src/ecs/systems/UISystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,18 @@ void UISystem::process(float)

updateGroupAssignments();

//track this group, if a callback changes it we want to quit the
//current loop else new groups have the existing events applied to them...
auto currentGroup = m_activeGroup;

std::size_t currentIndex = 0;
for (auto& e : m_groups[m_activeGroup])
{
auto& input = e.getComponent<UIInput>();

auto area = input.m_worldArea;
bool contains = false;
if (contains = area.contains(m_eventPosition); contains && input.enabled)
if (contains = area.contains(m_eventPosition) && !cro::App::getWindow().getMouseCaptured(); contains && input.enabled)
{
if (!input.active)
{
Expand Down Expand Up @@ -403,16 +407,19 @@ void UISystem::process(float)
}
}


//only do mouse/touch events if they're within the bounds of an input
if (contains && input.enabled)
{
for (const auto& f : m_mouseDownEvents)
{
m_buttonCallbacks[input.callbacks[UIInput::ButtonDown]](e, f);
if (currentGroup != m_activeGroup) goto updateEnd;
}
for (const auto& f : m_mouseUpEvents)
{
m_buttonCallbacks[input.callbacks[UIInput::ButtonUp]](e, f);
if (currentGroup != m_activeGroup) goto updateEnd;
}
}

Expand All @@ -421,10 +428,12 @@ void UISystem::process(float)
for (const auto& f : m_buttonDownEvents)
{
m_buttonCallbacks[input.callbacks[UIInput::ButtonDown]](e, f);
if (currentGroup != m_activeGroup) goto updateEnd;
}
for (const auto& f : m_buttonUpEvents)
{
m_buttonCallbacks[input.callbacks[UIInput::ButtonUp]](e, f);
if (currentGroup != m_activeGroup) goto updateEnd;
}
}

Expand All @@ -433,6 +442,7 @@ void UISystem::process(float)

//DPRINT("Window Pos", std::to_string(m_eventPosition.x) + ", " + std::to_string(m_eventPosition.y));

updateEnd:
m_previousEventPosition = m_eventPosition;
m_mouseUpEvents.clear();
m_buttonUpEvents.clear();
Expand Down Expand Up @@ -555,6 +565,9 @@ glm::vec2 UISystem::toWorldCoords(float x, float y)

//scale to vp
auto vp = getScene()->getActiveCamera().getComponent<Camera>().viewport;
x -= vp.left;
x /= vp.width;

y -= vp.bottom;
y /= vp.height;

Expand Down
20 changes: 20 additions & 0 deletions crogine/src/graphics/Colour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,26 @@ glm::vec4 cro::Colour::getVec4() const
}

//operators
cro::Colour::operator std::uint32_t() const
{
return (getRedByte() << 24 | getGreenByte() << 16 | getBlueByte() << 8 | getAlphaByte());
}

cro::Colour::operator glm::vec4() const
{
return { r,g,b,a };
}

cro::Colour::operator float*()
{
return &r;
}

cro::Colour::operator const float* () const
{
return &r;
}

bool cro::operator == (const Colour& l, const Colour& r)
{
return ((l.r == r.r) &&
Expand Down
2 changes: 1 addition & 1 deletion libsocial/include/Social.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ source distribution.
//(terrain vertex data and materials changed 1100 -> 1110)
//(player avatar data format changed 1110 -> 1120)
static constexpr std::uint16_t CURRENT_VER = 1120;
static const std::string StringVer("1.12.3");
static const std::string StringVer("1.12.4");


class Social final
Expand Down
1 change: 1 addition & 0 deletions samples/golf/src/GolfGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ bool GolfGame::initialise()
cro::AudioMixer::setLabel("Menu", MixerChannel::Menu);
cro::AudioMixer::setLabel("Announcer", MixerChannel::Voice);
cro::AudioMixer::setLabel("Vehicles", MixerChannel::Vehicles);
cro::AudioMixer::setLabel("Environment", MixerChannel::Environment);

m_sharedData.clientConnection.netClient.create(ConstVal::MaxClients);
m_sharedData.sharedResources = std::make_unique<cro::ResourceCollection>();
Expand Down
29 changes: 21 additions & 8 deletions samples/golf/src/golf/Clubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ namespace
ClubStat({9.11f, 10.f}, {9.11f, 10.f}, {9.11f, 10.f}) //except this which is dynamic
};

static constexpr float ToYards = 1.094f;
static constexpr float ToFeet = 3.281f;
//static constexpr float ToInches = 12.f;


constexpr std::size_t DebugLevel = 35;
}

Expand All @@ -88,18 +93,14 @@ std::string Club::getName(bool imperial, float distanceToPin) const

if (imperial)
{
static constexpr float ToYards = 1.094f;
static constexpr float ToFeet = 3.281f;
//static constexpr float ToInches = 12.f;

if (getPower(distanceToPin) > 10.f)
if (getPower(distanceToPin, imperial) > 10.f)
{
auto dist = static_cast<std::int32_t>(t * ToYards);
return m_name + std::to_string(dist) + "yds";
}
else
{
auto dist = static_cast<std::int32_t>(t * ToFeet);
auto dist = static_cast<std::int32_t>(std::round(t * ToFeet));
return m_name + std::to_string(dist) + "ft";
}
}
Expand All @@ -116,11 +117,23 @@ std::string Club::getName(bool imperial, float distanceToPin) const
}
}

float Club::getPower(float distanceToPin) const
float Club::getPower(float distanceToPin, bool imperial) const
{
if (m_id == ClubID::Putter)
{
return getScaledValue(ClubStats[m_id].stats[0].target, distanceToPin);
//looks like a bug, but turns out we need the extra power.
auto p = getScaledValue(ClubStats[m_id].stats[0].target, distanceToPin);
//return getScaledValue(ClubStats[m_id].stats[0].power, distanceToPin);

//because imperial display is rounded we need to apply this to the power too
//if (imperial
// && p < 10.f) //only diplayed in feet < 10m
//{
// p = std::round(p * ToFeet);
// p /= ToFeet; //still need to actually physic in metres
//}

return p;
}

//check player level and return further distance
Expand Down
4 changes: 2 additions & 2 deletions samples/golf/src/golf/Clubs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Club final

std::string getLabel() const { return m_name; } //doesn't include distance

float getPower(float distanceToPin) const;
float getPower(float distanceToPin, bool imperial) const;

float getAngle() const { return m_angle; }

Expand All @@ -101,7 +101,7 @@ class Club final

//putter below this is rescaled
static constexpr float ShortRange = 1.f / 3.f;
static constexpr float ShortRangeThreshold = ShortRange * 0.65f;
static constexpr float ShortRangeThreshold = ShortRange * 0.8f;
static constexpr float TinyRange = 1.f / 10.f;
static constexpr float TinyRangeThreshold = TinyRange * 0.5f;

Expand Down
11 changes: 10 additions & 1 deletion samples/golf/src/golf/DrivingRangeDirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void DrivingRangeDirector::handleMessage(const cro::Message& msg)
case sv::MessageID::GolfMessage:
{
const auto& data = msg.getData<GolfBallEvent>();
if (data.type == GolfBallEvent::TurnEnded)
if (data.type == GolfBallEvent::TurnEnded
|| data.type == GolfBallEvent::Holed)
{
auto hole = getCurrentHole();
auto idx = m_totalHoleCount - m_holeCount;
Expand All @@ -81,6 +82,7 @@ void DrivingRangeDirector::handleMessage(const cro::Message& msg)
difficulty -= 1.f;

distance += 10.f * difficulty;
distance = std::max(0.0001f, distance);

float score = 1.f - std::max(0.f, std::min(1.f, glm::length(data.position - m_holeData[hole].pin) / distance));
m_scores[idx] = cro::Util::Easing::easeOutQuad(score) * 100.f; //grade on a curve
Expand Down Expand Up @@ -108,6 +110,13 @@ void DrivingRangeDirector::setHoleCount(std::int32_t count, std::int32_t holeInd
{
std::shuffle(m_holeData.begin(), m_holeData.end(), cro::Util::Random::rndEngine);
}
else
{
std::sort(m_holeData.begin(), m_holeData.end(), [](const HoleData& a, const HoleData& b)
{
return a.par < b.par;
});
}
std::fill(m_scores.begin(), m_scores.end(), 0.f);
}

Expand Down
10 changes: 7 additions & 3 deletions samples/golf/src/golf/DrivingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ void DrivingState::handleMessage(const cro::Message& msg)
case sv::MessageID::GolfMessage:
{
const auto& data = msg.getData<GolfBallEvent>();
if (data.type == GolfBallEvent::TurnEnded)
if (data.type == GolfBallEvent::TurnEnded
|| data.type == GolfBallEvent::Holed)
{
//display a message with score
showMessage(glm::length(PlayerPosition - data.position));
Expand Down Expand Up @@ -580,7 +581,7 @@ void DrivingState::handleMessage(const cro::Message& msg)
cmd.action = [&](cro::Entity e, float)
{
//distance is zero because we should never select a putter here.
float scale = Clubs[m_inputParser.getClub()].getPower(0.f) / Clubs[ClubID::Driver].getPower(0.f);
float scale = Clubs[m_inputParser.getClub()].getPower(0.f, m_sharedData.imperialMeasurements) / Clubs[ClubID::Driver].getPower(0.f, m_sharedData.imperialMeasurements);
e.getComponent<cro::Transform>().setScale({ scale, 1.f });
};
m_gameScene.getSystem<cro::CommandSystem>()->sendCommand(cmd);
Expand Down Expand Up @@ -1250,6 +1251,8 @@ void DrivingState::createScene()
return;
}

std::int32_t holeIndex = 0;

const auto& properties = cfg.getProperties();
for (const auto& p : properties)
{
Expand All @@ -1260,6 +1263,7 @@ void DrivingState::createScene()
data.pin = p.getValue<glm::vec3>();
data.target = data.pin;
data.tee = PlayerPosition;
data.par = holeIndex++; //we use this to sort the hole data back into order if it was shuffled
//TODO this should be parsed from the cmt file
data.modelPath = "assets/golf/models/driving_range.cmb"; //needed for ball system to load collision mesh
//TODO check ball system for which properties are needed
Expand Down Expand Up @@ -3086,7 +3090,7 @@ void DrivingState::forceRestart()
m_mapTexture.display();
m_gameScene.setActiveCamera(oldCam);

m_gameScene.getDirector<DrivingRangeDirector>()->setHoleCount(0);
m_gameScene.getDirector<DrivingRangeDirector>()->setHoleCount(0, 0); //setting this to 0 makes sure the holes are returned to order if they were shuffled
setActiveCamera(CameraID::Player);

//reset any open messages
Expand Down
14 changes: 11 additions & 3 deletions samples/golf/src/golf/GameConsts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct MixerChannel final
enum
{
Music, Effects, Menu,
Voice, Vehicles,
Voice, Vehicles, Environment,

Count
};
Expand Down Expand Up @@ -964,8 +964,16 @@ static inline void formatDistanceString(float distance, cro::Text& target, bool
distance *= ToFeet;
if (distance > 1)
{
auto dist = static_cast<std::int32_t>(distance);
target.setString("Distance: " + std::to_string(dist) + "ft");
std::stringstream ss;
ss.precision(1);
ss << "Distance: ";
ss << std::fixed << distance;
ss << "ft";

target.setString(ss.str());

/*auto dist = static_cast<std::int32_t>(distance);
target.setString("Distance: " + std::to_string(dist) + "ft");*/
}
else
{
Expand Down
Loading

0 comments on commit 6ed2d9d

Please sign in to comment.