mirror of
https://bitbucket.org/King_DuckZ/jumping-in-d.git
synced 2025-02-01 09:03:55 +00:00
Start adding some basic collision primitives.
This commit is contained in:
parent
c51fdd7483
commit
71c4f0f089
6 changed files with 216 additions and 3 deletions
|
@ -18,6 +18,9 @@ add_executable(${PROJECT_NAME}
|
||||||
src/texture.d
|
src/texture.d
|
||||||
src/gameobjjumping.d
|
src/gameobjjumping.d
|
||||||
src/vector.d
|
src/vector.d
|
||||||
|
src/collisionline.d
|
||||||
|
src/rect.d
|
||||||
|
src/collidingentity.d
|
||||||
|
|
||||||
lib/DerelictSDL2/source/derelict/sdl2/functions.d
|
lib/DerelictSDL2/source/derelict/sdl2/functions.d
|
||||||
lib/DerelictSDL2/source/derelict/sdl2/image.d
|
lib/DerelictSDL2/source/derelict/sdl2/image.d
|
||||||
|
|
41
src/collidingentity.d
Normal file
41
src/collidingentity.d
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/* Copyright 2015, Michele Santullo
|
||||||
|
* This file is part of "Jumping in D".
|
||||||
|
*
|
||||||
|
* "Jumping in D" is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* "Jumping in D" is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with "Jumping in D". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module collidingentity;
|
||||||
|
import collisionline;
|
||||||
|
|
||||||
|
struct CollidingEntity {
|
||||||
|
@property ref const(CollisionQuad) current() const { return m_current; }
|
||||||
|
|
||||||
|
ref CollidingEntity opOpAssign(string op)(ref const(vec2) parOffset) {
|
||||||
|
static if ("+" == op || "-" == op) {
|
||||||
|
m_offs = parOffset;
|
||||||
|
mixin("m_current.left "~op~"= parOffset;");
|
||||||
|
mixin("m_current.top "~op~"= parOffset;");
|
||||||
|
mixin("m_current.right "~op~"= parOffset;");
|
||||||
|
mixin("m_current.bottom "~op~"= parOffset;");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
static assert(false, "No CollidingEntity::operator"~op~" available");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
CollisionQuad m_current;
|
||||||
|
vec2 m_offs;
|
||||||
|
}
|
65
src/collisionline.d
Normal file
65
src/collisionline.d
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/* Copyright 2015, Michele Santullo
|
||||||
|
* This file is part of "Jumping in D".
|
||||||
|
*
|
||||||
|
* "Jumping in D" is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* "Jumping in D" is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with "Jumping in D". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module collisionline;
|
||||||
|
public import vector : Vector;
|
||||||
|
import rect : Rect;
|
||||||
|
|
||||||
|
alias vec2 = Vector!(float, 2);
|
||||||
|
|
||||||
|
struct CollisionLine {
|
||||||
|
this (in vec2 parA, in vec2 parB) {
|
||||||
|
m_from = parA;
|
||||||
|
m_to = parB;
|
||||||
|
assert(m_from != m_to);
|
||||||
|
}
|
||||||
|
|
||||||
|
@property ref const(vec2) from() const { return m_from; }
|
||||||
|
@property ref const(vec2) to() const { return m_to; }
|
||||||
|
|
||||||
|
//This is how you provide your own operator+= and operator-= in D
|
||||||
|
ref CollisionLine opOpAssign(string op)(ref const(vec2) parOffset) {
|
||||||
|
static if ("+" == op || "-" == op) {
|
||||||
|
mixin("m_from "~op~"= parOffset;");
|
||||||
|
mixin("m_to "~op~"= parOffset;");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
static assert(false, "No CollisionLine::operator"~op~" available");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
vec2 m_from;
|
||||||
|
vec2 m_to;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct CollisionQuad {
|
||||||
|
Rect!CollisionLine m_lines;
|
||||||
|
|
||||||
|
//Structs don't have inheritance, but you can achieve the same with:
|
||||||
|
alias m_lines this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool collides (CollisionLine parLine, CollisionLine parLine1, vec2 parOffs, out vec2 parPoint) {
|
||||||
|
parPoint = vec2(0.0f);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool collides (CollisionQuad parQuad, CollisionQuad parQuad1, vec2 parOffs, out vec2 parPoint) {
|
||||||
|
return false;
|
||||||
|
}
|
|
@ -20,9 +20,8 @@ public import gameobj;
|
||||||
import derelict.sdl2.sdl;
|
import derelict.sdl2.sdl;
|
||||||
import texture;
|
import texture;
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import vector : Vector;
|
import collisionline;
|
||||||
|
import collidingentity : CollidingEntity;
|
||||||
alias vec2 = Vector!(float, 2);
|
|
||||||
|
|
||||||
//This is one possible game scene. It will draw a texture on the screen
|
//This is one possible game scene. It will draw a texture on the screen
|
||||||
//and it will report user wants to quit if you press the close button in the
|
//and it will report user wants to quit if you press the close button in the
|
||||||
|
@ -74,6 +73,17 @@ class GameObjJumping : GameObj {
|
||||||
//This is the function that is called at every frame. It represents the
|
//This is the function that is called at every frame. It represents the
|
||||||
//main loop's body.
|
//main loop's body.
|
||||||
override void exec() {
|
override void exec() {
|
||||||
|
//Move the sprite around (note that we store the original position)
|
||||||
|
vec2 offs = vec2(0.0f);
|
||||||
|
m_coll_sprite += offs;
|
||||||
|
|
||||||
|
//Check for collision between our sprite and the screen edges
|
||||||
|
//vec2 collision_point;
|
||||||
|
//if (m_coll_sprite.collides(m_coll_screen, collision_point)) {
|
||||||
|
//assert(false); //not implemented
|
||||||
|
//}
|
||||||
|
|
||||||
|
//Draw the sprite
|
||||||
SDL_RenderClear(renderer());
|
SDL_RenderClear(renderer());
|
||||||
m_texture.draw();
|
m_texture.draw();
|
||||||
SDL_RenderPresent(renderer());
|
SDL_RenderPresent(renderer());
|
||||||
|
@ -86,6 +96,8 @@ class GameObjJumping : GameObj {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Texture m_texture;
|
Texture m_texture;
|
||||||
|
CollisionQuad m_coll_screen;
|
||||||
|
CollidingEntity m_coll_sprite;
|
||||||
bool m_wants_to_quit;
|
bool m_wants_to_quit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
53
src/rect.d
Normal file
53
src/rect.d
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/* Copyright 2015, Michele Santullo
|
||||||
|
* This file is part of "Jumping in D".
|
||||||
|
*
|
||||||
|
* "Jumping in D" is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* "Jumping in D" is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with "Jumping in D". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module rect;
|
||||||
|
import std.traits : isNumeric, isAssignable;
|
||||||
|
|
||||||
|
struct Rect(T) {
|
||||||
|
this (in T parLeft, in T parTop, in T parRight, in T parBottom) {
|
||||||
|
m_left = parLeft;
|
||||||
|
m_top = parTop;
|
||||||
|
m_right = parRight;
|
||||||
|
m_bottom = parBottom;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property ref inout(T) left() inout { return m_left; }
|
||||||
|
@property ref inout(T) top() inout { return m_top; }
|
||||||
|
@property ref inout(T) right() inout { return m_right; }
|
||||||
|
@property ref inout(T) bottom() inout { return m_bottom; }
|
||||||
|
static if (isAssignable!T) {
|
||||||
|
@property ref T left(in T parLeft) { return m_left = parLeft; }
|
||||||
|
@property ref T top(in T parTop) { return m_top = parTop; }
|
||||||
|
@property ref T right(in T parRight) { return m_right = parRight; }
|
||||||
|
@property ref T bottom(in T parBottom) { return m_bottom = parBottom; }
|
||||||
|
}
|
||||||
|
static if (isNumeric!T) {
|
||||||
|
@property diff_t width() const { return m_right - m_left; }
|
||||||
|
@property diff_t height() const { return m_bottom - m_top; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static if (isNumeric!T) {
|
||||||
|
alias diff_t = typeof(m_right - m_left);
|
||||||
|
}
|
||||||
|
|
||||||
|
T m_left;
|
||||||
|
T m_top;
|
||||||
|
T m_right;
|
||||||
|
T m_bottom;
|
||||||
|
}
|
39
src/vector.d
39
src/vector.d
|
@ -34,6 +34,39 @@ struct Vector(T, int N) {
|
||||||
fill(m_data[], parInit[0]);
|
fill(m_data[], parInit[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Define mathematical operators
|
||||||
|
//Note the use of auto ref. This is only allowed in function templates, and
|
||||||
|
//instructs the compiler to generate a byref and byvalue version of the same
|
||||||
|
//function. It is necessary to have a byvalue function; if you don't provide
|
||||||
|
//one, the following code: v += Vector!(int, 2)(1, 2);
|
||||||
|
//won't compile as the temporary created on the rhs can't be bound to a
|
||||||
|
//const ref in D.
|
||||||
|
ref Vector!(T,N) opOpAssign(string op)(auto ref const(Vector!(T,N)) parOther) {
|
||||||
|
for (int z = 0; z < N; ++z) {
|
||||||
|
mixin("m_data[z] "~op~"= parOther.m_data[z];");
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
ref Vector!(T,N) opOpAssign(string op)(T parOther) {
|
||||||
|
for (int z = 0; z < N; ++z) {
|
||||||
|
mixin("m_data[z] "~op~"= parOther;");
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
Vector!(T,N) opBinary(string op)(Vector!(T,N) parOther) {
|
||||||
|
for (int z = 0; z < N; ++z) {
|
||||||
|
mixin("parOther.m_data[z] = m_data[z] "~op~" parOther.m_data[z];");
|
||||||
|
}
|
||||||
|
return parOther;
|
||||||
|
}
|
||||||
|
Vector!(T,N) opBinary(string op)(T parOther) {
|
||||||
|
auto retval = Vector!(T,N)(this);
|
||||||
|
for (int z = 0; z < N; ++z) {
|
||||||
|
mixin("retval "~op~"= parOther;");
|
||||||
|
}
|
||||||
|
return parOther;
|
||||||
|
}
|
||||||
|
|
||||||
//Define x, y, z, w accessors. Vectors larger than 4 elements won't have any
|
//Define x, y, z, w accessors. Vectors larger than 4 elements won't have any
|
||||||
//of those.
|
//of those.
|
||||||
static if (N <= 4) {
|
static if (N <= 4) {
|
||||||
|
@ -111,4 +144,10 @@ unittest {
|
||||||
assert(Vector!(int, 2)(3, 0) == v2.x0);
|
assert(Vector!(int, 2)(3, 0) == v2.x0);
|
||||||
assert(Vector!(int, 2)(4, 0) == v2.y0);
|
assert(Vector!(int, 2)(4, 0) == v2.y0);
|
||||||
assert(Vector!(int, 3)(5, 4, 3) == v2.zyx);
|
assert(Vector!(int, 3)(5, 4, 3) == v2.zyx);
|
||||||
|
|
||||||
|
v2 += Vector!(int, 4)(1, 2, 3, 4);
|
||||||
|
assert(4 == v2.x);
|
||||||
|
assert(6 == v2.y);
|
||||||
|
assert(8 == v2.z);
|
||||||
|
assert(10 == v2.w);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue