scene->create_and_add(this, PlayerComponent) {
comp.holding = Entity_Nothing;
- comp.controls = controls;
comp.color = .{1,1,1};
}
+ scene->create_and_add(this, MovementComponent) {
+ comp.controls = controls;
+ }
+
scene->create_and_add(this, RenderComponent) {
comp.func = PlayerComponent.render;
}
return this;
}
-PlayerComponent :: struct {
- use base: Component;
-
- color := Color.{1,1,1};
- holding : Entity_ID;
+MovementComponent :: struct {
+ use component: Component;
controls : Player_Controls;
facing := Facing.Up;
- update :: (player: ^PlayerComponent, use this: ^Entity, dt: f32) {
+ update :: (movement: ^MovementComponent, use this: ^Entity, dt: f32) {
speed :: 128.0f;
delta: Vector2;
- if is_key_down(player.controls.left) { delta.x -= speed * dt; player.facing = .Left; }
- if is_key_down(player.controls.right) { delta.x += speed * dt; player.facing = .Right; }
- if is_key_down(player.controls.up) { delta.y -= speed * dt; player.facing = .Up; }
- if is_key_down(player.controls.down) { delta.y += speed * dt; player.facing = .Down; }
+ if is_key_down(movement.controls.left) { delta.x -= speed * dt; movement.facing = .Left; }
+ if is_key_down(movement.controls.right) { delta.x += speed * dt; movement.facing = .Right; }
+ if is_key_down(movement.controls.up) { delta.y -= speed * dt; movement.facing = .Up; }
+ if is_key_down(movement.controls.down) { delta.y += speed * dt; movement.facing = .Down; }
dist := math.max(size.x, size.y) * 2;
walls := scene->query_by_flags(.{pos.x - dist, pos.y - dist, dist * 2, dist * 2}, .Solid);
try_move(this, .{delta.x, 0}, walls);
try_move(this, .{0, delta.y}, walls);
+ }
+
+ try_move :: (use this: ^Entity, delta: Vector2, obsticles: [] ^Entity) {
+ pos += delta;
+ ent_rect := Entity.get_rect(this);
+
+ holding: Entity_ID;
+ if player := this->get(PlayerComponent); player != null {
+ holding = player.holding;
+ }
+
+ for obsticles {
+ // Don't check collision on the object that you are carrying or yourself because it probably won't make sense.
+ if it == this do continue;
+ if it.id == holding do continue;
+
+ if Rect.intersects(ent_rect, Entity.get_rect(it)) {
+ pos -= delta;
+ break;
+ }
+ }
+ }
+}
+
+PlayerComponent :: struct {
+ use base: Component;
+
+ color := Color.{1,1,1};
+ holding : Entity_ID;
+ update :: (player: ^PlayerComponent, use this: ^Entity, dt: f32) {
// Highlight the object that you are going to interact with
+ movement := this->get(MovementComponent);
+
//
// Try to interact with nearby objects
//
- if is_key_just_up(player.controls.interact) {
+ if is_key_just_up(movement.controls.interact) {
area := Rect.{pos.x - size.x, pos.y - size.y, size.x * 2, size.y * 2};
objects := scene->query_by_component(area, InteractableComponent);
defer memory.free_slice(^objects);
//
// Try to pick up nearby objects
//
- if is_key_just_up(player.controls.pick_up) {
+ if is_key_just_up(movement.controls.pick_up) {
if player.holding != Entity_Nothing {
holding_object := scene->get(player.holding);
holding_object.flags |= .Carryable;
r := Entity.get_rect(holding_object);
d := Vector2.{(size.x + r.w) / 2 + 4, (size.y + r.h) / 2 + 4};
- holding_object.pos = pos + facing_to_direction_vector(player.facing) * d;
+ holding_object.pos = pos + facing_to_direction_vector(movement.facing) * d;
player.holding = Entity_Nothing;
} else {
}
}
- try_move :: (use this: ^Entity, delta: Vector2, obsticles: [] ^Entity) {
- pos += delta;
- ent_rect := Entity.get_rect(this);
-
- holding: Entity_ID;
- if player := this->get(PlayerComponent); player != null {
- holding = player.holding;
- }
-
- for obsticles {
- // Don't check collision on the object that you are carrying or yourself because it probably won't make sense.
- if it == this do continue;
- if it.id == holding do continue;
-
- if Rect.intersects(ent_rect, Entity.get_rect(it)) {
- pos -= delta;
- break;
- }
- }
- }
-
render :: (use this: ^Entity) {
player := this->get(PlayerComponent);
immediate_set_color(player.color);