From b4481973b978f80950c0c463cdc0cad6d89e0614 Mon Sep 17 00:00:00 2001 From: Elronnd Date: Thu, 28 Dec 2017 14:54:54 -0800 Subject: [PATCH] Add non-working Vector struct --- src/map.d | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/map.d b/src/map.d index a61dd1f..02bc807 100644 --- a/src/map.d +++ b/src/map.d @@ -23,6 +23,61 @@ struct Tile { } } +// (n)ormalized +struct Vector2n(uint maxx = map_x, uint maxy = map_y) { + pragma(inline, true) private static uint normalized(int i, uint max) { + i %= max; + if (i < 0) { + i += max; + } + + return i; + // If this function is a perf problem, use the following to trigger a cmov (compiler should be smart enough to do it that way already though) + /* + * int oth = i + max; + * i = (i < 0) ? i : oth; + */ + } + + private struct Normalint(uint max) { + private int _i; + + // No reason you would want to negate it, deref it, or invert it; unary + literally does nothing, + uint opUnary(string op)() if (op == "++" || op = "--") { + normalized = true; + mixin(op ~ "_i"); + return _i = normalized(_i, max); + } + uint opBinary(string op)(uint other) { + return normalized(mixin("_i" ~ op ~ "other"), max); + } + + @property uint i() { + // we don't have to normalize i. Because assignment and inc/dec already normalize it + return _i; + } + @property uint i(int newi) { + return _i = normalized(newi, max); + } + alias i this; + } + uint _y, _x; + + this(uint y, uint x) { + y = _y; + x = _x; + } + + @property uint y() { return _y; } + @property uint y(uint newy) { return _y = newy; } + @property uint y(int newy) { return _y = normalized(newy, maxy); } + + @property uint x() { return_x; } + @property uint x(uint newx) { return_x = newx; } + @property uint x(int newx) { return_x = normalized(newx, maxx); } +} + + class Map { private Tile[][] tiles; -- 2.11.4.GIT