Skip to content

Commit

Permalink
(agrf) Reverse class order & add unit test to ensure it works
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyangyi committed Nov 15, 2024
1 parent 0e1d502 commit fe1d9af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
27 changes: 27 additions & 0 deletions agrf/lib/building/layout_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from agrf.lib.building.layout import ADefaultGroundSprite, ALayout
from agrf.lib.building.symmetry import BuildingSymmetrical


def test_default_groundsprite():
Expand All @@ -12,6 +13,14 @@ def test_default_groundsprite():
assert m_graphics.shape == (127, 256, 4)
assert m_graphics[64, 128, 1] == 72

r_graphics = gs1012.R.graphics(4, 32).to_image()
assert r_graphics.shape == (127, 256, 4)
assert r_graphics[64, 128, 1] == 67

t_graphics = gs1012.T.graphics(4, 32).to_image()
assert t_graphics.shape == (127, 256, 4)
assert t_graphics[64, 128, 1] == 67


def test_layout():
gs1012 = ADefaultGroundSprite(1012)
Expand All @@ -24,3 +33,21 @@ def test_layout():
m_graphics = l.M.graphics(4, 32).to_image()
assert m_graphics.shape == (127, 256, 4)
assert m_graphics[64, 128, 1] == 72

r_graphics = l.R.graphics(4, 32).to_image()
assert r_graphics.shape == (127, 256, 4)
assert r_graphics[64, 128, 1] == 67

t_graphics = l.T.graphics(4, 32).to_image()
assert t_graphics.shape == (127, 256, 4)
assert t_graphics[64, 128, 1] == 67


def test_symmetry():
gs1012 = ADefaultGroundSprite(1012)
gs1011 = ADefaultGroundSprite(1011)

g = BuildingSymmetrical.create_variants([gs1012, gs1011])
assert g is g.R
assert g is g.T
assert g is g.M.M
54 changes: 18 additions & 36 deletions agrf/lib/building/symmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ class BuildingSymmetryMixin:
def create_variants(classobj, variants):
for i, v in enumerate(variants):
cls = v.__class__
v.__class__ = type(cls.__name__, (cls, classobj), {})
v.__class__ = type(cls.__name__, (classobj, cls), {})

idx = classobj.render_indices()[i]
v.M = variants[classobj._symmetry_descriptor[idx ^ 1]]
v._M = variants[classobj._symmetry_descriptor[idx ^ 1]]
if idx % 2 == 0:
v.R = variants[classobj._symmetry_descriptor[idx ^ 2]]
v.T = variants[classobj._symmetry_descriptor[idx ^ 4]]
v._R = variants[classobj._symmetry_descriptor[idx ^ 2]]
v._T = variants[classobj._symmetry_descriptor[idx ^ 4]]
else:
v.R = variants[classobj._symmetry_descriptor[idx ^ 4]]
v.T = variants[classobj._symmetry_descriptor[idx ^ 2]]
v._R = variants[classobj._symmetry_descriptor[idx ^ 4]]
v._T = variants[classobj._symmetry_descriptor[idx ^ 2]]
v.symmetry = classobj
return variants[0]

@property
def M(self):
return self._M

@property
def R(self):
return self._R

@property
def T(self):
return self._T

@classmethod
def get_all_variants(cls, thing):
return [cls.symmetry_index(thing, i) for i in cls.render_indices()]
Expand Down Expand Up @@ -189,72 +201,42 @@ def compose_symmetry_indices(a, b):


class BuildingFull(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 1, 2, 3, 4, 5, 6, 7)


class BuildingSymmetricalX(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 1, 0, 1, 2, 3, 2, 3)


class BuildingSymmetricalY(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 1, 2, 3, 0, 1, 2, 3)


class BuildingSymmetrical(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 1, 0, 1, 0, 1, 0, 1)


class BuildingRotational(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 1, 2, 3, 2, 3, 0, 1)


class BuildingRotational4(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 1, 1, 0, 1, 0, 0, 1)


class BuildingDiagonal(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 0, 1, 2, 2, 1, 3, 3)


class BuildingDiagonalAlt(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 1, 2, 2, 3, 3, 1, 0)


class BuildingDiamond(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 0, 1, 1, 1, 1, 0, 0)


class BuildingCylindrical(BuildingSymmetryMixin):
def __init__(self, obj):
super().__init__(obj)

_symmetry_descriptor = (0, 0, 0, 0, 0, 0, 0, 0)


Expand Down

0 comments on commit fe1d9af

Please sign in to comment.