From 8e6b6cec9bc7b6cc94c7497d9de4ae748c5a56ee Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Thu, 29 Feb 2024 11:22:52 +0800 Subject: [PATCH] Component intro --- Component_Physics.h | 36 +++++++++++++++++++++++ Entity.h | 23 +++++++++++++++ JacksEscape.vcxproj | 4 +++ JacksEscape.vcxproj.filters | 12 ++++++++ Types.c | 57 +++++++++++++++++++++++++++++++++++++ Types.h | 34 ++++++++++++++++++++++ 6 files changed, 166 insertions(+) create mode 100644 Component_Physics.h create mode 100644 Entity.h create mode 100644 Types.c create mode 100644 Types.h diff --git a/Component_Physics.h b/Component_Physics.h new file mode 100644 index 0000000..c05337e --- /dev/null +++ b/Component_Physics.h @@ -0,0 +1,36 @@ +#pragma once + +#include "Types.h" +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +typedef struct Entity; +extern double Physics_Gravity; + + +typedef struct { + Entity *super; + double x, y; + double vecX, vecY; +} Component_Position; + + +// Box is relative to Position if exists +// if not, Box is absolute +// +// Moving hitboxes only hits fixed hitboxes +typedef struct { + Entity *super; + Box2 box; + bool fixed; +} Component_Hitbox; + + +#ifdef __cplusplus +} +#endif diff --git a/Entity.h b/Entity.h new file mode 100644 index 0000000..f4d9d52 --- /dev/null +++ b/Entity.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include "util/vector.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +// Entity. +typedef struct { + uintptr_t type; + const char *name; + +} Entity; + + + +#ifdef __cplusplus +} +#endif diff --git a/JacksEscape.vcxproj b/JacksEscape.vcxproj index a9c53ef..d5f2d30 100644 --- a/JacksEscape.vcxproj +++ b/JacksEscape.vcxproj @@ -127,6 +127,9 @@ + + + @@ -134,6 +137,7 @@ + diff --git a/JacksEscape.vcxproj.filters b/JacksEscape.vcxproj.filters index 3f5ce82..d714b85 100644 --- a/JacksEscape.vcxproj.filters +++ b/JacksEscape.vcxproj.filters @@ -33,6 +33,15 @@ Util + + 头文件 + + + 头文件 + + + 头文件 + @@ -44,5 +53,8 @@ Util + + 源文件 + \ No newline at end of file diff --git a/Types.c b/Types.c new file mode 100644 index 0000000..75553b6 --- /dev/null +++ b/Types.c @@ -0,0 +1,57 @@ + +#include "Types.h" + +static inline double dmin(double x, double y) { + return x < y ? x : y; +} +static inline double dmax(double x, double y) { + return x > y ? x : y; +} + + +Vec2 vec2_Add(Vec2 x, Vec2 y) { + Vec2 result = { + .x = x.x + y.x, + .y = x.y + y.y}; + return result; +} + +Vec2 vec2_Scale(Vec2 v, double scale) { + Vec2 result = { + .x = v.x * scale, + .y = v.y * scale}; + return result; +} + +bool box2_Intersects(const Box2 x, const Box2 y, Box2 *out_intersection) { + // Compute the min and max of the first rectangle on both axes + const double r1MinX = dmin(x.lefttop.x, x.lefttop.x + x.size.x); + const double r1MaxX = dmax(x.lefttop.x, x.lefttop.x + x.size.x); + const double r1MinY = dmin(x.lefttop.y, x.lefttop.x + x.size.x); + const double r1MaxY = dmax(x.lefttop.y, x.lefttop.x + x.size.x); + + // Compute the min and max of the second rectangle on both axes + const double r2MinX = dmin(y.lefttop.x, y.lefttop.x + y.size.x); + const double r2MaxX = dmax(y.lefttop.x, y.lefttop.x + y.size.x); + const double r2MinY = dmin(y.lefttop.y, y.lefttop.y + y.size.y); + const double r2MaxY = dmax(y.lefttop.y, y.lefttop.y + y.size.y); + + // Compute the intersection boundaries + const double interLeft = dmax(r1MinX, r2MinX); + const double interTop = dmax(r1MinY, r2MinY); + const double interRight = dmin(r1MaxX, r2MaxX); + const double interBottom = dmin(r1MaxY, r2MaxY); + + // If the intersection is valid (positive non zero area), then there is an intersection + if ((interLeft < interRight) && (interTop < interBottom)) { + if (out_intersection) { + out_intersection->lefttop.x = interLeft; + out_intersection->lefttop.y = interTop; + out_intersection->size.x = interRight - interLeft; + out_intersection->size.y = interBottom - interTop; + } + return true; + } else { + return false; + } +} diff --git a/Types.h b/Types.h new file mode 100644 index 0000000..91a9890 --- /dev/null +++ b/Types.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +// A 2d vector of double. +typedef struct { + double x, y; +} Vec2; + + +Vec2 vec2_Add(Vec2 x, Vec2 y); +Vec2 vec2_Scale(Vec2 v, double scale); + + +// A 2d box of double. +typedef struct { + Vec2 lefttop; + Vec2 size; +} Box2; + + +// Intersection test. +bool box2_Intersects(const Box2 x, const Box2 y, Box2 *out_intersection); + + +#ifdef __cplusplus +} +#endif