Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main ai game #34

Merged
merged 6 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "Graphics/BartaGraphicsBridgeInterface.h"
#include "pch.h"
#include <BartaObjectManager.h>
#include <ObjectManagerInterface.h>
#include <Predefines.h>

namespace Barta {
Expand All @@ -28,7 +27,11 @@ class Application {
dynamicsUpdateStrategy(std::move(dynamicsUpdateStrategy)),
collisionEventsLogger({}),
collisionExecutor(CollisionCoreExecutor(std::move(collisionDetectionStrategy))),
objectLists({}) {}
objectLists({}) {
// this->collisionEventsLogger.logSubscriber(std::unique_ptr<Subscribers::RigidObjectRigidObject>(
// new Barta::StaticCollisionResponseSubscriberType<RigidObjectInterface, RigidObjectInterface>(*this->postDynamicsEventLogger)
// ));
}

Application(const Application&) = delete;
Application(Application&&) = delete;
Expand Down Expand Up @@ -86,7 +89,7 @@ class Application {

virtual void postDynamicUpdate() {}

virtual bool isRunning() const { return true; }
virtual bool isRunning() { return true; }

protected:
std::string windowName;
Expand Down
22 changes: 22 additions & 0 deletions include/Debug/DebugGuardSubscriber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by bartanakin on 11/10/24.
//

#pragma once
#include <Events/Events/KeyPressedEvent.h>

namespace Barta::Debug {

class DebugGuardSubscriber: virtual public KeyPressedSubscriberInterface {
public:
static bool debugGuard;
DebugGuardSubscriber() noexcept = default;

bool handle(KeyPressedEvent& event) override;

bool isValid() const noexcept override;

private:
};

} // Barta::Debug
8 changes: 5 additions & 3 deletions include/Dynamics/ConstVelocityDynamicsUpdateStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ class ConstVelocityDynamicsUpdateStrategy: public DynamicsUpdateStrategyInterfac
continue;
}

nextDynamics.velocity = applyAllowedDirections(nextDynamics.allowedDirections, nextDynamics.velocity);
nextDynamics.velocity = applyAllowedDirections(nextDynamics.allowedDirections, nextDynamics.velocity).zeroised();

// TODO add response force handling
nextDynamics.force = applyAllowedDirections(nextDynamics.allowedDirections, nextDynamics.force);
nextDynamics.force = applyAllowedDirections(nextDynamics.allowedDirections, nextDynamics.force).zeroised();

object->move(applyAllowedDirections(nextDynamics.allowedDirections, nextDynamics.massCenter - dynamics.massCenter));
auto shift = applyAllowedDirections(nextDynamics.allowedDirections, nextDynamics.massCenter - dynamics.massCenter).zeroised();
nextDynamics.massCenter = dynamics.massCenter + shift;
object->move(shift);
// TODO
// object->rotate(
// nextDynamics.rotationVelocity * object.deltaTime,
Expand Down
10 changes: 10 additions & 0 deletions include/Dynamics/DynamicsAwareInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../Geometrics/Vector2f.h"
#include "../ReduceableList.h"
#include "../pch.h"
#include "DynamicsAwareListConcept.h"
#include "DynamicsDTO.h"
#include "DynamicsDTOCollection.h"
#include <BartaObjectInterface.h>
Expand All @@ -20,6 +21,15 @@ class DynamicsAwareInterface: public virtual BartaObjectInterface {

virtual void rotate(float, Vector2f) = 0;

/**
* @deprecated use getCurrentDynamicsData() or getNextDynamicsData() instead
*/
virtual DynamicsDTOCollection& getDynamicsDTOs() = 0;

virtual const DynamicsDTO& getCurrentDynamicsData() final { return this->getDynamicsDTOs()[Barta::DynamicsDTOIteration::CURRENT]; }

virtual DynamicsDTO& getNextDynamicsData() final { return this->getDynamicsDTOs()[Barta::DynamicsDTOIteration::NEXT]; }
};

static_assert(DynamicsAwareConcept<DynamicsAwareInterface>);
}
14 changes: 5 additions & 9 deletions include/Dynamics/DynamicsAwareListConcept.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

#pragma once
#include "DynamicsDTO.h"
#include "DynamicsDTOCollection.h"
#include "pch.h"

template<typename T>
concept DynamicsAwarePointerConcept = requires(T t, Barta::Vector2f v, float rotation) {
{ t->getDynamicsDTOs() } -> std::same_as<Barta::DynamicsDTOCollection&>;
{ t->move(v) } -> std::same_as<void>;
{ t->rotate(rotation) } -> std::same_as<void>;
};

template<typename T>
concept DynamicsAwareConcept = requires(T t, Barta::Vector2f v, float rotation) {
{ t.getDynamicsDTOs() } -> std::same_as<Barta::DynamicsDTOCollection&>;
{ t.getCurrentDynamicsData() } -> std::same_as<const Barta::DynamicsDTO&>;
{ t.getNextDynamicsData() } -> std::same_as<Barta::DynamicsDTO&>;
{ t.isToBeDeleted() } -> std::same_as<bool>;
{ t.move(v) } -> std::same_as<void>;
{ t.rotate(rotation) } -> std::same_as<void>;
{ t.rotate(rotation, v) } -> std::same_as<void>;
};

template<typename T>
Expand All @@ -26,5 +23,4 @@ concept DynamicsAwareListConcept = requires(T t) {
{ t.end() } -> std::sentinel_for<decltype(t.begin())>;

// TODO add concept for list entry
// requires DynamicsAwarePointerConcept<std::iter_value_t<T>>;
};
5 changes: 3 additions & 2 deletions include/Geometrics/BartaShapes/AABB.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AABB {
LEFT_TOP = TOP + LEFT
};

AABB() = default;
AABB(const Vector2f& leftTop, const Vector2f& widthHeight);

/*
Expand Down Expand Up @@ -74,8 +75,8 @@ class AABB {
VoronoiRegion findVoronoiRegionType(const Vector2f& point) const noexcept;

private:
const Vector2f leftTop;
const Vector2f widthHeight;
Vector2f leftTop;
Vector2f widthHeight;
};

inline AABB::VoronoiRegion operator|(
Expand Down
2 changes: 2 additions & 0 deletions include/Geometrics/BartaShapes/OBB.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class OBB: protected AABB {

PointDistance closestPointTo(Vector2f) const noexcept;

Vector2f getCenter() const noexcept;

private:
// radians
float rotation;
Expand Down
68 changes: 6 additions & 62 deletions include/Geometrics/Intersections.h
Original file line number Diff line number Diff line change
@@ -1,76 +1,20 @@
#pragma once
#include "../Utilities/QuadraticEquation.h"
#include "BartaShapes/AABB.h"
#include "BartaShapes/Circle.h"
#include "Ray.h"
#include "Segment.h"

namespace Barta {
namespace Intersections {
auto segmentAndCircle(
const Segment& I,
const Circle& c
) noexcept {
auto d = I.getEnd() - I.getBeginning();
auto m = I.getBeginning() - c.getCenter();
auto eq = Utils::QuadraticEquation(d * d, m * d * 2.f, m * m - c.getRadius() * c.getRadius());
eq.solve();
auto sol = eq.getSolutions();

sol.erase(std::remove_if(sol.begin(), sol.end(), [](float t) { return t < 0.f || 1.f < t; }), sol.end());
float lineAndLineRespectToFirst(const Segment& I1, const Segment& I2) noexcept;

return sol;
}
std::vector<std::tuple<float, float>> segmentAndSegment(const Segment& I1, const Segment& I2) noexcept;

float lineAndLineRespectToFirst(
const Segment& I1,
const Segment& I2
) noexcept {
auto perpendicular = (I2.getEnd() - I2.getBeginning()).perpendicular();
auto denom = (perpendicular * (I1.getEnd() - I1.getBeginning()));
if (denom == 0.f) {
return std::numeric_limits<float>::infinity();
}
std::vector<std::tuple<float, float>> segmentAndAABB(const Segment& I, const AABB& aabb);

return (perpendicular * (I2.getBeginning() - I1.getBeginning())) / denom;
}
std::vector<float> rayAndCircle(const Ray& ray, const Circle& c) noexcept;

auto segmentAndSegment(
const Segment& I1,
const Segment& I2
) noexcept {
std::vector<std::tuple<float, float>> result;
auto barCoords = std::tuple<float, float>(lineAndLineRespectToFirst(I1, I2), lineAndLineRespectToFirst(I2, I1));

if (0.f <= std::get<0>(barCoords) && std::get<0>(barCoords) <= 1.f && 0.f <= std::get<1>(barCoords) && std::get<1>(barCoords) <= 1.f) {
result.push_back(barCoords);
}

return result;
}

auto segmentAndAABB(
const Segment& I,
const AABB& aabb
) {
decltype(segmentAndSegment(I, I)) intersections;
for (const auto& side: aabb.getSides()) {
auto sideIntersections = segmentAndSegment(I, side);
intersections.insert(intersections.end(), sideIntersections.begin(), sideIntersections.end());
}

if (intersections.size() == 2) {
if (std::get<0>(intersections[0]) > std::get<0>(intersections[1])) {
auto temp = intersections[0];
intersections[0] = intersections[1];
intersections[1] = temp;
}
}

if (intersections.size() > 2) {
throw std::runtime_error("size too long");
}

return intersections;
}
std::vector<float> segmentAndCircle(const Segment& I, const Circle& c) noexcept;
}
}
16 changes: 16 additions & 0 deletions include/Geometrics/Ray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by bartanakin on 11/15/24.
//

#pragma once
#include "Vector2f.h"

namespace Barta {

struct Ray {
Vector2f origin;
Vector2f direction;

Ray(Vector2f origin, Vector2f direction) noexcept;
};
}
6 changes: 4 additions & 2 deletions include/Geometrics/Vector2f.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Barta {

class Vector2f {
public:
static constexpr float ZEROING_EDGE = 0.005f;
static constexpr float ZEROING_EDGE = 0.001f;
float x;
float y;
constexpr Vector2f() noexcept = default;
Expand Down Expand Up @@ -55,7 +55,7 @@ class Vector2f {

Vector2f rotated(float radians) const noexcept;

float angleTo(Vector2f) const noexcept;
float angleTo(Vector2f = {1.f, 0.f}) const noexcept;

Vector2f projection(Vector2f) const noexcept;

Expand All @@ -71,6 +71,8 @@ class Vector2f {

bool isZero() const noexcept;

float length() const noexcept;

/*
* @deprecated
*/
Expand Down
50 changes: 25 additions & 25 deletions include/Graphics/SpriteBuilder/RectangleWithColorsSprite.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#pragma once
#include "../Color.h"
#include "../../Geometrics/BartaShapes/AABB.h"
#include "../Color.h"

namespace Barta{
struct RectangleWithColorsSprite {
public:
RectangleWithColorsSprite(
Vector2f origin,
Vector2f size,
Color leftTopColor,
Color rightTopColor,
Color rightBottomColor,
Color leftBottomColor
) noexcept :
aabb({origin, size}),
leftTopColor(leftTopColor),
rightTopColor(rightTopColor),
rightBottomColor(rightBottomColor),
leftBottomColor(leftBottomColor)
{}
namespace Barta {
struct RectangleWithColorsSprite {
public:
RectangleWithColorsSprite() noexcept = default;

AABB aabb;
Color leftTopColor;
Color rightTopColor;
Color rightBottomColor;
Color leftBottomColor;
};
}
RectangleWithColorsSprite(
Vector2f origin,
Vector2f size,
Color leftTopColor,
Color rightTopColor,
Color rightBottomColor,
Color leftBottomColor
) noexcept:
aabb({origin, size}),
leftTopColor(leftTopColor),
rightTopColor(rightTopColor),
rightBottomColor(rightBottomColor),
leftBottomColor(leftBottomColor) {}

AABB aabb;
Color leftTopColor;
Color rightTopColor;
Color rightBottomColor;
Color leftBottomColor;
};
}
2 changes: 2 additions & 0 deletions include/Hitbox/AABB_Hitbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class AABB_Hitbox: public HitboxInterface {

bool isWithin(const Vector2f& position) const override;

std::vector<float> intersectsWithRay(const Ray& ray) const override;

CollisionTestResult intersects(
const HitboxInterface& secondHitbox,
const CollisionDetectionStrategyInterface& collisionDetector,
Expand Down
2 changes: 2 additions & 0 deletions include/Hitbox/CircleHitbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class CircleHitbox final: public HitboxInterface {

bool isWithin(const Vector2f& position) const override;

std::vector<float> intersectsWithRay(const Ray& ray) const override;

CollisionTestResult intersects(
const HitboxInterface& secondHitbox,
const CollisionDetectionStrategyInterface& collisionDetector,
Expand Down
15 changes: 15 additions & 0 deletions include/Hitbox/HitboxAwareConcept.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Created by bartanakin on 11/10/24.
//

#pragma once
#include "HitboxInterface.h"
#include "pch.h"

namespace Barta {
template<typename T>
concept HitboxAwareConcept = requires(const T t) {
{ t.getHitbox() } -> std::same_as<std::unique_ptr<const HitboxInterface>>;
};

}
20 changes: 11 additions & 9 deletions include/Hitbox/HitboxAwareInterface.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#pragma once
#include "../Geometrics/TransformableInterface.h"
#include "../pch.h"
#include "HitboxAwareConcept.h"
#include "HitboxInterface.h"
#include "../Geometrics/TransformableInterface.h"

namespace Barta{
namespace Barta {

class HitboxAware{
public:
HitboxAware() = default;
virtual ~HitboxAware() = default;
class HitboxAware {
public:
HitboxAware() = default;
virtual ~HitboxAware() = default;

virtual std::unique_ptr<const HitboxInterface> getHitbox() const = 0;
};
}
virtual std::unique_ptr<const HitboxInterface> getHitbox() const = 0;
};

static_assert(HitboxAwareConcept<HitboxAware>);
}
Loading
Loading