Skip to content

Commit 1dbf92f

Browse files
committed
RigidBody2DComponent in wren
1 parent 52292cb commit 1dbf92f

7 files changed

Lines changed: 62 additions & 13 deletions

File tree

OverEngine/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ endif()
4040
file(GLOB_RECURSE OE_WREN_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/src/Wren" "src/Wren/*.wren")
4141
set(OE_WREN_INC_FILES)
4242
foreach(WREN_FILE IN LISTS OE_WREN_FILES)
43+
set(WREN_FILE_ABS "${CMAKE_CURRENT_SOURCE_DIR}/src/Wren/${WREN_FILE}")
4344
add_custom_command(
44-
OUTPUT "${WREN_FILE}.inc"
45-
MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/src/Wren/${WREN_FILE}"
45+
OUTPUT "${WREN_FILE_ABS}.inc"
46+
MAIN_DEPENDENCY "${WREN_FILE_ABS}"
4647
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/Wren"
4748
COMMAND python "${PROJECT_SOURCE_DIR}/utils/wren_to_c_string.py" "${WREN_FILE}.inc" ${WREN_FILE}
4849
)
4950

50-
list(APPEND OE_WREN_INC_FILES "${WREN_FILE}.inc")
51+
list(APPEND OE_WREN_INC_FILES "${WREN_FILE_ABS}.inc")
5152
endforeach()
5253

5354
add_library(OverEngine STATIC ${OE_CROSS_PLATFORM_FILES} ${OE_PLATFORM_FILES}

OverEngine/src/OverEngine/Scripting/Wren.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace WrenSources
55
{
6+
#include "Wren/components.wren.inc"
67
#include "Wren/entity.wren.inc"
78
#include "Wren/input.wren.inc"
89
#include "Wren/keycodes.wren.inc"
@@ -40,6 +41,7 @@ namespace OverEngine
4041
wrenpp::VM::loadModuleFn = [](const char* mod) -> char* {
4142
#define WREN_MOD(m) if (strcmp(mod, #m) == 0) return const_cast<char*>(WrenSources::m##ModuleSource);
4243

44+
WREN_MOD(components)
4345
WREN_MOD(entity)
4446
WREN_MOD(input)
4547
WREN_MOD(keycodes)

OverEngine/src/OverEngine/Scripting/WrenBindings.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@ namespace OverEngine
3636
entity.GetComponent<TransformComponent>().SetPosition(position);
3737
}
3838

39-
// TODO: Move into component classes (not all entities have rigidbodies)
40-
// TODO: Use Vector2
41-
void EntityInternals_applyLinearImpulseToCenter(Entity& entity, const Vector3& impulse)
39+
#define COMPONENT_HAS(component) \
40+
bool component##_has(Entity& entity) \
41+
{ \
42+
return entity.HasComponent<component>(); \
43+
}
44+
45+
COMPONENT_HAS(TransformComponent)
46+
COMPONENT_HAS(RigidBody2DComponent)
47+
48+
void RigidBody2DComponent_applyLinearImpulseToCenter(Entity& entity, const Vector3& impulse)
4249
{
4350
entity.GetComponent<RigidBody2DComponent>().RigidBody->ApplyLinearImpulseToCenter(impulse);
4451
}
@@ -59,14 +66,26 @@ namespace OverEngine
5966
WRENPP_BIND_STATIC(EntityInternals_getName, "getName(_)")
6067
WRENPP_BIND_STATIC(EntityInternals_getPosition, "getPosition(_)")
6168
WRENPP_BIND_STATIC(EntityInternals_setPosition, "setPosition(_,_)")
62-
63-
WRENPP_BIND_STATIC(EntityInternals_applyLinearImpulseToCenter, "applyLinearImpulseToCenter(_,_)")
6469
.endClass()
6570

6671
.bindClass<Entity>("Entity")
6772
.endClass()
6873
.endModule();
6974

75+
m_VM.beginModule("components")
76+
.beginClass("ComponentInternals")
77+
WRENPP_BIND_STATIC(RigidBody2DComponent_applyLinearImpulseToCenter, "RigidBody2DComponent_applyLinearImpulseToCenter(_,_)")
78+
.endClass()
79+
80+
.beginClass("TransformComponent")
81+
WRENPP_BIND_STATIC(TransformComponent_has, "has(_)")
82+
.endClass()
83+
84+
.beginClass("RigidBody2DComponent")
85+
WRENPP_BIND_STATIC(RigidBody2DComponent_has, "has(_)")
86+
.endClass()
87+
.endModule();
88+
7089
m_VM.beginModule("math")
7190
.bindClass<Vector3, float, float, float>("Vector3")
7291
WRENPP_BIND_GETTER(Vector3::x, "x")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class ComponentInternals {
2+
foreign static RigidBody2DComponent_applyLinearImpulseToCenter(entity, impulse)
3+
}
4+
5+
class TransformComponent {
6+
foreign static has(entity)
7+
8+
construct new(entity) { _entity = entity }
9+
}
10+
11+
class RigidBody2DComponent {
12+
foreign static has(entity)
13+
14+
construct new(entity) { _entity = entity }
15+
16+
applyLinearImpulseToCenter(impulse) { ComponentInternals.RigidBody2DComponent_applyLinearImpulseToCenter(_entity, impulse) }
17+
}

OverEngine/src/Wren/entity.wren

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ class EntityInternals {
33
foreign static getName(entity)
44
foreign static getPosition(entity)
55
foreign static setPosition(entity, position)
6-
foreign static applyLinearImpulseToCenter(entity, impulse)
76
}
87

98
/// A single entity in the scene.
@@ -16,6 +15,12 @@ foreign class Entity {
1615
position { EntityInternals.getPosition(this) }
1716
position=(rhs) { EntityInternals.setPosition(this, rhs) }
1817

19-
/// Will be removed :)
20-
applyLinearImpulseToCenter(impulse) { EntityInternals.applyLinearImpulseToCenter(this, impulse) }
18+
hasComponent(type) { type.has(this) }
19+
getComponent(type) {
20+
if (hasComponent(type)) {
21+
return type.new(this)
22+
}
23+
24+
Fiber.abort("Entity '%(name)' doesn't have %(type)")
25+
}
2126
}

OverEngine/src/Wren/math.wren

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ foreign class Vector3 {
1212
toString { "Vector3(%(x), %(y), %(z))" }
1313

1414
*(rhs) { Vector3.new(x * rhs, y * rhs, z * rhs) }
15+
+(rhs) { Vector3.new(x + rhs.x, y + rhs.y, z + rhs.z) }
16+
17+
lerp(b, t) { this * (1 - t) + b * t }
1518
}

Sandbox/src/wren/scripts.wren

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "script" for Script
22
import "keycodes" for KeyCode
33
import "input" for Input
44
import "math" for Vector3
5+
import "components" for RigidBody2DComponent
56

67
class Player is Script {
78
construct new(e) {
@@ -10,6 +11,7 @@ class Player is Script {
1011

1112
onCreate() {
1213
System.print("My name is '%(entity.name)'")
14+
_rb = entity.getComponent(RigidBody2DComponent)
1315
}
1416

1517
onUpdate(delta) {
@@ -20,7 +22,7 @@ class Player is Script {
2022
if (Input.isKeyPressed(KeyCode.w)) { impulse.y = impulse.y + 1 }
2123
if (Input.isKeyPressed(KeyCode.s)) { impulse.y = impulse.y - 1 }
2224

23-
entity.applyLinearImpulseToCenter(impulse * 20 * delta)
25+
_rb.applyLinearImpulseToCenter(impulse * 20 * delta)
2426
}
2527
}
2628

@@ -32,6 +34,6 @@ class CameraController is Script {
3234
player=(rhs) { _player = rhs }
3335

3436
onLateUpdate(delta) {
35-
entity.position = _player.position
37+
entity.position = _player.position.lerp(entity.position, 0.5)
3638
}
3739
}

0 commit comments

Comments
 (0)