Skip to content

Commit

Permalink
temp commit to work on laptop
Browse files Browse the repository at this point in the history
  • Loading branch information
tzann committed Jun 26, 2024
1 parent f521e8a commit 63aec4c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
44 changes: 40 additions & 4 deletions desktop_version/src/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


namespace Terrain {
bool precomputed_rooms[20][20] = { { false } };
RoomInfo precomputed_rooms[20][20] = { { RoomInfo() } };

CollisionKind collision_kind = CollisionKind::Walls;
std::vector<NavCorner> nav_corners;
Expand Down Expand Up @@ -41,6 +41,14 @@ namespace Terrain {
}
}

precomputed_rooms[rx][ry].min_x_pos = min_x_pos;
precomputed_rooms[rx][ry].max_x_pos = max_x_pos;
precomputed_rooms[rx][ry].min_y_pos = min_y_pos;
precomputed_rooms[rx][ry].max_y_pos = max_y_pos;
precomputed_rooms[rx][ry].warpx = map.warpx;
precomputed_rooms[rx][ry].warpy = map.warpy;
precomputed_rooms[rx][ry].towermode = map.towermode;

// Create all corners
for (int x = min_x_pos; x <= max_x_pos; x++) {
for (int y = min_y_pos; y <= max_y_pos; y++) {
Expand Down Expand Up @@ -261,6 +269,23 @@ namespace Terrain {
continue;
}

// Check no wall intersections
bool any_intersections = false;
for (int k = 0; k < num_walls; k++) {
Wall& w = nav_walls.at(k);
if (w.room_x != c.room_x || w.room_y != c.room_y) {
continue;
}
if (w.RayIntersect(c.x, c.y, dx, dy)) {
any_intersections = true;
break;
}
}
if (any_intersections) {
// Intersected a wall, continue
continue;
}

// Add edges to the corners
NavEdge e1, e2;
e1.target = i; e1.dx = dx; e1.dy = dy;
Expand All @@ -270,6 +295,18 @@ namespace Terrain {
new_corner.edges.push_back(e2);
}
}



precomputed_rooms[rx][ry].precomputed = true;
}

bool TryConnectCorners(NavCorner& source, NavCorner& target) {
if (IsConcave(source) || IsConcave(target)) {
return false;
}


}

bool CheckPlayerCollisionCurrentRoom(int x, int y) {
Expand Down Expand Up @@ -297,9 +334,8 @@ namespace Terrain {
int rx = game.roomx;
int ry = game.roomy;

if (!precomputed_rooms[rx][ry]) {
if (!precomputed_rooms[rx][ry].precomputed) {
PrecomputeCurrentRoomTerrain();
precomputed_rooms[rx][ry] = true;
}
}

Expand All @@ -310,7 +346,7 @@ namespace Terrain {
void AfterTileRenderHook(void) {
int rx = game.roomx;
int ry = game.roomy;
if (precomputed_rooms[rx][ry]) {
if (precomputed_rooms[rx][ry].precomputed) {
int px = obj.entities[0].xp;
int py = obj.entities[0].yp;
int num_walls = nav_walls.size();
Expand Down
46 changes: 39 additions & 7 deletions desktop_version/src/Terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ namespace Terrain {
QuadBLTR,
};

bool IsConvex(NavCornerType type) {
switch (type) {
case NavCornerType::TopLeft:
case NavCornerType::TopRight:
case NavCornerType::BottomLeft:
case NavCornerType::BottomRight:
return true;
default:
return false;
}
}

bool IsConcave(NavCornerType type) {
return !IsConvex(type);
}

struct NavEdge {
int target; // Index of destination corner
int dx, dy; // Horizontal and vertical distance
Expand Down Expand Up @@ -189,11 +205,10 @@ namespace Terrain {
// we can assume 0 < t < 1 because of min/max checks
float i_x = ((float)ox) + ((float)dx) * t;

float x_dist = orientation == Up ? i_x - x : x - i_x;
float x_dist = orientation == Down ? x - i_x : i_x - x;
// Intersection is within wall's range
return x_dist < width;
}
else {
return 0 < x_dist && x_dist < width;
} else {
// assume(orientation == Right || Orientation == Left)
int min_x, max_x;
if (dx == 0) {
Expand All @@ -212,17 +227,17 @@ namespace Terrain {
// Ray doesn't cross extended wall
return false;
}
else if (dy == 0 && (oy == y || oy == y + (orientation == Left ? -width : width))) {
else if (dy == 0 && (oy == y || oy == y + (orientation == Left ? width : -width))) {
// Ray exactly touches the end of the wall, let it through
return false;
}

float t = ((float)(x - ox)) / ((float)dx);
float i_y = ((float)oy) + ((float)dy) * t;

float y_dist = orientation == Right ? i_y - y : y - i_y;
float y_dist = orientation == Left ? y - i_y : i_y - y;
// Intersection is within wall's range
return y_dist < width;
return 0 < y_dist && y_dist < width;
}
}
};
Expand Down Expand Up @@ -267,6 +282,23 @@ namespace Terrain {
}
};

struct RoomInfo {
int min_x_pos, max_x_pos, min_y_pos, max_y_pos;
bool precomputed, warpx, warpy, towermode;

RoomInfo() {
min_x_pos = min_y_pos = INT_MAX;
max_x_pos = max_y_pos = INT_MIN;
precomputed = warpx = warpy = towermode = false;
}

RoomInfo(int min_x, int max_x, int min_y, int max_y) : min_x_pos(min_x), max_x_pos(max_x), min_y_pos(min_y), max_y_pos(max_y) {
precomputed = true;
warpx = warpy = towermode = false;
}

RoomInfo(int min_x, int max_x, int min_y, int max_y, bool pre, bool warp_x, bool warp_y, bool tower) : min_x_pos(min_x), max_x_pos(max_x), min_y_pos(min_y), max_y_pos(max_y), precomputed(pre), warpx(warpx), warpy(warpy), towermode(tower) { }
};

void PrecomputeCurrentRoomTerrain(void);
bool CheckPlayerCollisionCurrentRoom(int x, int y);
Expand Down

0 comments on commit 63aec4c

Please sign in to comment.