From 34feb226e470491ebce9630d656e82e01b5b0d3d Mon Sep 17 00:00:00 2001 From: John McCardle Date: Fri, 10 Mar 2023 11:35:46 -0500 Subject: [PATCH] collision --- src/scripts/Grid.py | 24 ++++++++++++++++++++++-- src/scripts/TestScene.py | 8 ++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/scripts/Grid.py b/src/scripts/Grid.py index aefea8e..3dae6aa 100644 --- a/src/scripts/Grid.py +++ b/src/scripts/Grid.py @@ -14,7 +14,7 @@ class GridPoint: return f"" class Grid: - def __init__(self, title, gx, gy, gs, x, y, w, h): + def __init__(self, title, gx, gy, gs, x, y, w, h, visible=False): self.title = title self.grid_x = gx self.grid_y = gy @@ -23,8 +23,28 @@ class Grid: self.y = y self.w = w self.h = h + self.visible = visible self.points = [] + self.entities = [] + + def at(self, x, y): + if not (x > 0 and y > 0 and x < self.grid_x and y < self.grid_y): return None + return self.points[y * self.grid_y + x] def __repr__(self): - return f"" + return f"" + + +# CGrid(Grid* _g, int _ti, int _si, int _x, int _y, bool _v) +class Entity: + def __init__(self, parent, tex_index, sprite_index, x, y, visible=True): + self.parent = parent + self.tex_index = tex_index + self.sprite_index = sprite_index + self.x = x + self.y = y + self.visible = visible + + def __repr__(self): + return f"" diff --git a/src/scripts/TestScene.py b/src/scripts/TestScene.py index 1b20b50..db9ff92 100644 --- a/src/scripts/TestScene.py +++ b/src/scripts/TestScene.py @@ -30,6 +30,12 @@ class TestEntity: def move(self, dx, dy): # select animation direction # prefer left or right for diagonals. + grids = mcrfpy.listGrids() + for g in grids: + if g.title == self.grid: + if not g.at(self.x + dx, self.y + dy).walkable: + print("Blocked at target location.") + return if (dx == 0 and dy == 0): direction = self.facing_direction # TODO, jump straight to computer turn elif (dx): @@ -248,6 +254,8 @@ class TestScene: for p in self.grids[0].points: p.color = (randint(0, 255), randint(64, 192), 0) #print("[Python] Modifying:") + self.grids[0].at(10, 10).color = (255, 255, 255) + self.grids[0].at(10, 10).walkable = False self.grids[0].visible = True mcrfpy.modGrid(self.grids[0]) print(f"Sent grid: {repr(self.grids[0])}")