Skip to content

Commit 7e248d0

Browse files
committed
Add shrinking gaps
1 parent 21ffda3 commit 7e248d0

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

game.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@
7171

7272
// The width of the area to leave empty for the player to pass through.
7373
// Given in meters.
74-
#define GAP_WIDTH 0.75f
74+
// Gaps start out big and gradually shrink over time.
75+
#define GAP_WIDTH_MIN 0.75f
76+
#define GAP_WIDTH_MAX 1.5f
77+
#define GAP_WIDTH_SHRINK_SPEED -0.01f
7578

7679
// The height of gap surfaces.
7780
// Given in meters.

gap.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,19 @@ POSSIBILITY OF SUCH DAMAGE.
4141
SDL_Surface *GapSurfaces[6];
4242

4343
static int compareFloat(const void *a, const void *b);
44-
void GapInit(struct Gap* gap, float y)
44+
void GapInit(struct Gap* gap, const float w, const float y)
4545
{
4646
// Randomly generate some gaps, and place blocks around them
4747
float gapXs[MAX_GAPS];
4848
for (int i = 0; i < MAX_GAPS; i++)
4949
{
50-
gapXs[i] =
51-
GAP_WIDTH / 2 +
52-
(float)rand() / (float)RAND_MAX * (FIELD_WIDTH - GAP_WIDTH);
50+
gapXs[i] = w / 2 + (float)rand() / (float)RAND_MAX * (FIELD_WIDTH - w);
5351
}
5452
qsort(gapXs, MAX_GAPS, sizeof gapXs[0], compareFloat);
5553
// Merge gaps if they are too close
5654
for (int i = 1; i < MAX_GAPS; i++)
5755
{
58-
if (gapXs[i] - gapXs[i - 1] < GAP_WIDTH + MIN_BLOCK_WIDTH)
56+
if (gapXs[i] - gapXs[i - 1] < w + MIN_BLOCK_WIDTH)
5957
{
6058
gapXs[i] = (gapXs[i] + gapXs[i - 1]) / 2;
6159
gapXs[i - 1] = 0;
@@ -68,9 +66,9 @@ void GapInit(struct Gap* gap, float y)
6866
for (int i = 0; i < MAX_GAPS; i++)
6967
{
7068
if (gapXs[i] == 0) continue;
71-
BlockInit(&b, left, y, gapXs[i] - GAP_WIDTH / 2 - left);
69+
BlockInit(&b, left, y, gapXs[i] - w / 2 - left);
7270
CArrayPushBack(&gap->blocks, &b);
73-
left = gapXs[i] + GAP_WIDTH / 2;
71+
left = gapXs[i] + w / 2;
7472
}
7573
// Add last block
7674
BlockInit(&b, left, y, FIELD_WIDTH - left);

gap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct Gap
4848

4949
extern SDL_Surface *GapSurfaces[6];
5050

51-
void GapInit(struct Gap* gap, float y);
51+
void GapInit(struct Gap* gap, const float w, const float y);
5252
void GapRemove(struct Gap* gap);
5353
void GapDraw(const struct Gap* gap, const float y);
5454

space.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void SpaceReset(Space *s)
6969
}
7070
CArrayClear(&s->Gaps);
7171
s->gapGenDistance = GAP_GEN_START;
72+
s->gapWidth = GAP_WIDTH_MAX;
7273

7374
PickupsReset();
7475
}
@@ -130,18 +131,19 @@ void SpaceUpdate(
130131
lastGap = CArrayGet(&s->Gaps, s->Gaps.size - 1);
131132
}
132133
if (s->Gaps.size == 0 ||
133-
GapBottom(lastGap) - (cameraY - FIELD_HEIGHT) >= s->gapGenDistance)
134+
GapBottom(lastGap) - (cameraY - FIELD_HEIGHT * 2) >= s->gapGenDistance)
134135
{
135136
float top = 0;
136137
if (s->Gaps.size != 0)
137138
{
138139
top = GapBottom(lastGap) - s->gapGenDistance;
139-
s->gapGenDistance += GAP_GEN_SPEED;
140-
s->gapGenDistance = MAX(GAP_GEN_MIN, s->gapGenDistance);
140+
s->gapGenDistance =
141+
MAX(GAP_GEN_MIN, s->gapGenDistance + GAP_GEN_SPEED);
141142
}
142143
struct Gap g;
143-
GapInit(&g, top);
144+
GapInit(&g, s->gapWidth, top);
144145
CArrayPushBack(&s->Gaps, &g);
146+
s->gapWidth = MAX(GAP_WIDTH_MIN, s->gapWidth + GAP_WIDTH_SHRINK_SPEED);
145147
}
146148

147149
if (y < s->edgeBodiesBottom)

space.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct
4040

4141
CArray Gaps; // of Gap
4242
float gapGenDistance;
43+
float gapWidth;
4344
} Space;
4445

4546
extern Space space;

0 commit comments

Comments
 (0)