Component intro
This commit is contained in:
parent
a6642911d7
commit
8e6b6cec9b
36
Component_Physics.h
Normal file
36
Component_Physics.h
Normal file
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include "Types.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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
|
23
Entity.h
Normal file
23
Entity.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "util/vector.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// Entity.
|
||||
typedef struct {
|
||||
uintptr_t type;
|
||||
const char *name;
|
||||
|
||||
} Entity;
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -127,6 +127,9 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Component_Physics.h" />
|
||||
<ClInclude Include="Entity.h" />
|
||||
<ClInclude Include="Types.h" />
|
||||
<ClInclude Include="util\minmax.h" />
|
||||
<ClInclude Include="util\queue.h" />
|
||||
<ClInclude Include="util\tree.h" />
|
||||
@ -134,6 +137,7 @@
|
||||
<ClInclude Include="util\vector.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Types.c" />
|
||||
<ClCompile Include="util\queue.c" />
|
||||
<ClCompile Include="util\tree.c" />
|
||||
<ClCompile Include="util\vector.c" />
|
||||
|
@ -33,6 +33,15 @@
|
||||
<ClInclude Include="util\vector.h">
|
||||
<Filter>Util</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Entity.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Component_Physics.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Types.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="util\queue.c">
|
||||
@ -44,5 +53,8 @@
|
||||
<ClCompile Include="util\vector.c">
|
||||
<Filter>Util</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Types.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
57
Types.c
Normal file
57
Types.c
Normal file
@ -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;
|
||||
}
|
||||
}
|
34
Types.h
Normal file
34
Types.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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
|
Loading…
Reference in New Issue
Block a user