Skip to content

Commit

Permalink
refactor(?)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamader committed Dec 25, 2023
1 parent 1161e28 commit 803ae97
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/engine/Component.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Component: Loggable {
// utils

protected void line(Vec2 a, Vec2 b) {
color(0, 255, 0);
SDL_RenderDrawLine(go.ctx.r, cast(int)a.x, cast(int)a.y, cast(int)b.x, cast(int)b.y);
}

Expand Down
2 changes: 1 addition & 1 deletion src/engine/Context.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Context {
this.w = SDL_CreateWindow(title.toStringz,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
w, h,
SDL_WINDOW_VULKAN);
SDL_WINDOW_OPENGL);

SDL_Renderer* createRdr() =>
this.r = SDL_CreateRenderer(this.w,
Expand Down
22 changes: 15 additions & 7 deletions src/engine/GameObject.d
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class GameObject: Loggable {
// utils

protected void line(Vec2 a, Vec2 b) {
color(255, 0, 0);
SDL_RenderDrawLine(ctx.r, cast(int)a.x, cast(int)a.y, cast(int)b.x, cast(int)b.y);
}

Expand Down Expand Up @@ -121,20 +122,27 @@ class GameObject: Loggable {

C register(C: Component)(C c) {
c.go = this;
auto old = findComponent!C;
if(old.e) {
warn("registering ", C.stringof, " to ", this, " is duplicate; dropping old");
components[old.i] = c;
} else {
components ~= c;
if(has!C) {
warn("registering ", C.stringof, " to ", this, " is duplicate; dropping this");
warn("hint: you can update component with upsert(component)");
return component!C;
}
components ~= c;
if(alreadySetup) c.setup;
return c;
}

C upsert(C: Component)(C c) {
c.go = this;
auto old = findComponent!C;
if(old.e) components[old.i] = c;
else components ~= c;
if(alreadySetup) c.setup;
return c;
}

void destroy() {
foreach(i, e; parent.children) if(e == this) {
import std.algorithm.mutation;
parent.children = parent.children.remove(i);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/engine/System.d
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ Pair!bool objectsConflict(GameObject obj1, GameObject obj2) {
(abs(pos1.x - pos2.x) < size1.x + size2.x) &&
(abs(pos1.y - pos2.y) < size1.y + size2.y);

auto res1 = touched(pos1 + v1 * Vec2(0, 1), pos2 + v2 * Vec2(0, 1));
auto res2 = touched(pos1 + v1 * Vec2(1, 0), pos2 + v2 * Vec2(1, 0));
auto res1 = touched(pos1 + v1.v, pos2 + v2.v);
auto res2 = touched(pos1 + v1.h, pos2 + v2.h);

auto res = Pair!bool(res1, res2);
if(!res1 && !res2) {
Expand Down
11 changes: 3 additions & 8 deletions src/engine/components/Window.d
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
module engine.components.Window;

import sdl;
import engine;
import sdl.vulkan;


class Window: Component {
auto get() => go.ctx.w;

auto size(){
if(go.ctx is null){
dbg("null");
return Vec2(0,0);
}
auto size() {
int h, w;
SDL_Vulkan_GetDrawableSize(go.ctx.w, &w, &h);
SDL_GL_GetDrawableSize(go.ctx.w, &w, &h);
return Vec2(w, h);
}
}
3 changes: 3 additions & 0 deletions src/engine/core/Vec2.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ struct Vec2 {
real size() const => sqrt(_x ^^ 2 + _y ^^ 2);
Vec2 unit() const => this / size;

auto v() const => Vec2(0, _y);
auto h() const => Vec2(_x, 0);

// op

auto opUnary(string op)() const =>
Expand Down
1 change: 1 addition & 0 deletions src/engine/core/package.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module engine.core;

public import engine.core.media;
public import engine.core.physics;

public import engine.core.Nullpo;
public import engine.core.Vec2;
2 changes: 2 additions & 0 deletions src/engine/core/physics/package.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module engine.core.physics;

14 changes: 7 additions & 7 deletions src/game/entities/Missile.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ class Missile: GameObject {
Normal,
}

int id;
Type type;
Vec2 d;
Vec2 dir;

this(Type type, Vec2 dir, Vec2 pos) {
this.type = type;
this.dir = dir;

this(Type t, Vec2 direction, Vec2 pos) {
register(new Transform(Transform.Org.World)).pos = pos;
register(new RigidBody(1)).a = Vec2(0, 0);
auto missile = new ImageAsset("assets/hero0.png");
register(new SpriteRenderer(missile));
this.type = t;
this.d = direction;
/*
auto missile = new ImageAsset("");
register(new SpriteRenderer(missile));
Expand All @@ -29,7 +29,7 @@ class Missile: GameObject {
Vec2 f;
final switch(type) {
case Type.Normal:
if(this.d.x >= 0) f = Vec2(3, 0);
if(dir.x >= 0) f = Vec2(3, 0);
else f = Vec2(-3, 0);
break;
}
Expand All @@ -45,7 +45,7 @@ class Missile: GameObject {
auto tform = component!Transform;
tform.rot++;
if(tform.pos.x > 1000 || tform.pos.y > 1000) destroy;
final switch(this.type){
final switch(type) {
case Type.Normal: break;
}
}
Expand Down

0 comments on commit 803ae97

Please sign in to comment.