]> git.localhorst.tv Git - orbi.git/blob - src/world/Tileset.cpp
initial collision tests
[orbi.git] / src / world / Tileset.cpp
1 #include "Tileset.h"
2
3 #include "Tile.h"
4 #include "World.h"
5 #include "../graphics/Canvas.h"
6
7
8 namespace orbi {
9
10 Tileset::Tileset(Texture &&tex, Vector<int> size)
11 : sprite(std::move(tex), size / 2)
12 , tileSize(size)
13 , subSize(size / 2)
14 , subCount(8, 4) {
15
16 }
17
18
19 void Tileset::DrawFG(Canvas &canv, Vector<int> to, const World &world, Vector<int> at) {
20         const Tile &mid = world.TileAt(at);
21         if (!mid.HasFG()) return;
22
23         const Vector<int> ul = at + Vector<int>(-1, -1);
24         const Vector<int> u = at + Vector<int>(0, -1);
25         const Vector<int> ur = at + Vector<int>(1, -1);
26         const Vector<int> l = at + Vector<int>(-1, 0);
27         const Vector<int> r = at + Vector<int>(1, 0);
28         const Vector<int> dl = at + Vector<int>(-1, 1);
29         const Vector<int> d = at + Vector<int>(0, 1);
30         const Vector<int> dr = at + Vector<int>(1, 1);
31
32         const bool mul = !world.InBounds(ul) || world.TileAt(ul).MatchesFG(mid);
33         const bool mu = !world.InBounds(u) || world.TileAt(u).MatchesFG(mid);
34         const bool mur = !world.InBounds(ur) || world.TileAt(ur).MatchesFG(mid);
35         const bool ml = !world.InBounds(l) || world.TileAt(l).MatchesFG(mid);
36         const bool mr = !world.InBounds(r) || world.TileAt(r).MatchesFG(mid);
37         const bool mdl = !world.InBounds(dl) || world.TileAt(dl).MatchesFG(mid);
38         const bool md = !world.InBounds(d) || world.TileAt(d).MatchesFG(mid);
39         const bool mdr = !world.InBounds(dr) || world.TileAt(dr).MatchesFG(mid);
40
41         const int tl = (ml << 2) | (mul << 1) | (mu);
42         const int tr = (mr << 2) | (mur << 1) | (mu);
43         const int bl = (ml << 2) | (mdl << 1) | (md);
44         const int br = (mr << 2) | (mdr << 1) | (md);
45
46         const Vector<int> off = Vector<int>(0, mid.GetFG() * subCount.y);
47         sprite.Draw(Vector<int>(off.x + tl, off.y), canv, to);
48         sprite.Draw(Vector<int>(off.x + tr, off.y + 1), canv, Vector<int>(to.x + subSize.x, to.y));
49         sprite.Draw(Vector<int>(off.x + bl, off.y + 2), canv, Vector<int>(to.x, to.y + subSize.y));
50         sprite.Draw(Vector<int>(off.x + br, off.y + 3), canv, Vector<int>(to.x + subSize.x, to.y + subSize.y));
51 }
52
53 }