(minor) Regulated tabulations

master
KikooDX 3 years ago
parent ac7a134063
commit 6ff82feef3
  1. 12
      levels.lua
  2. 26
      src/collide.c
  3. 206
      src/draw.c
  4. 430
      src/main.c
  5. 4
      src/player.c

@ -18,22 +18,22 @@ for i = 0, 9999, 1 do
end
end
file:close()
to_write = to_write.." case "..i..":\n memcpy(level, \""..
to_write = to_write.." case "..i..":\n memcpy(level, \""..
formated_file.."\", "..#formated_file..");\n"
if file_content and string.find(file_content, 'c') then
to_write = to_write.." *coin_id = "..current..";\n"
to_write = to_write.." *coin_id = "..current..";\n"
current = current + 1
end
to_write = to_write.." break;\n"
to_write = to_write.." break;\n"
end
end
to_write = to_write.." }\n}\n"
to_write = to_write.." }\n}\n"
--finish and write
to_write = [[#include "levels.h"
#include <gint/std/string.h>
void set_level(int level_id, char level[], unsigned int *coin_id) {
switch (level_id)
{
switch (level_id)
{
]]..to_write
io.write(to_write)

@ -5,21 +5,21 @@
char collide(int x, int y, char level[], char tile)
{
if ((level[(int)(x/16) + (int)(y/16) * LEVEL_WIDTH] == tile) ||
(level[(int)(x/16) + (int)((y+PLAYER_SIDES)/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES)/16) + (int)((y+PLAYER_SIDES)/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES)/16) + (int)(y/16) * LEVEL_WIDTH] == tile) /*||
(level[(int)(x/16) + (int)((y+PLAYER_SIDES/2)/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES/2)/16) + (int)(y/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES/2)/16) + (int)((y+PLAYER_SIDES)/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES)/16) + (int)((y+PLAYER_SIDES/2)/16) * LEVEL_WIDTH] == tile)*/) return 1;
return 0;
if ((level[(int)(x/16) + (int)(y/16) * LEVEL_WIDTH] == tile) ||
(level[(int)(x/16) + (int)((y+PLAYER_SIDES)/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES)/16) + (int)((y+PLAYER_SIDES)/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES)/16) + (int)(y/16) * LEVEL_WIDTH] == tile) /*||
(level[(int)(x/16) + (int)((y+PLAYER_SIDES/2)/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES/2)/16) + (int)(y/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES/2)/16) + (int)((y+PLAYER_SIDES)/16) * LEVEL_WIDTH] == tile) ||
(level[(int)((x+PLAYER_SIDES)/16) + (int)((y+PLAYER_SIDES/2)/16) * LEVEL_WIDTH] == tile)*/) return 1;
return 0;
}
char collide_solid(int x, int y, char level[])
{
return (collide(x, y, level, '0') ||
collide(x, y, level, '^') ||
collide(x, y, level, '~') ||
collide(x, y, level, '#'));
return (collide(x, y, level, '0') ||
collide(x, y, level, '^') ||
collide(x, y, level, '~') ||
collide(x, y, level, '#'));
}

@ -20,130 +20,130 @@ extern image_t img_elevator5, img_elevator6, img_elevator7, img_elevator8;
extern image_t img_elevator9, img_elevator10, img_elevator11, img_elevator12;
extern image_t img_elevator13, img_elevator14, img_elevator15, img_elevator16;
const image_t * ani_elevator[16] = {&img_elevator1, &img_elevator2,
&img_elevator3, &img_elevator4, &img_elevator5, &img_elevator6,
&img_elevator7, &img_elevator8, &img_elevator9, &img_elevator10,
&img_elevator11, &img_elevator12, &img_elevator13, &img_elevator14,
&img_elevator15, &img_elevator16};
&img_elevator3, &img_elevator4, &img_elevator5, &img_elevator6,
&img_elevator7, &img_elevator8, &img_elevator9, &img_elevator10,
&img_elevator11, &img_elevator12, &img_elevator13, &img_elevator14,
&img_elevator15, &img_elevator16};
void draw_player(int old_x, int old_y, int x, int y)
{
if (old_x != x || old_y != y)
{
drect(old_x + DRAW_OFFSET_X, old_y + DRAW_OFFSET_Y,
(old_x + PLAYER_SIDES) + DRAW_OFFSET_X,
(old_y + PLAYER_SIDES) + DRAW_OFFSET_Y, BG_COLOR);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_player);
}
if (old_x != x || old_y != y)
{
drect(old_x + DRAW_OFFSET_X, old_y + DRAW_OFFSET_Y,
(old_x + PLAYER_SIDES) + DRAW_OFFSET_X,
(old_y + PLAYER_SIDES) + DRAW_OFFSET_Y, BG_COLOR);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_player);
}
}
void draw_level(char level[], char upgrades[], char erase_coin)
{
dclear(BG_COLOR);
unsigned int i = 0;
unsigned int x = 0;
unsigned int y = 0;
while (i != LEVEL_WIDTH*16)
{
switch (level[i])
{
case '0':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_ground);
break;
case 'v':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_spike);
break;
case '~':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_ground);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_ice);
break;
case '#':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_ground);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_glue);
break;
case 'j':
if (upgrades[0]) erase_tile(x, y, level);
break;
case 'c':
if (erase_coin) erase_tile(x, y, level);
break;
}
x += 16;
if (x == 16*LEVEL_WIDTH)
{
x = 0;
y += 16;
}
i++;
}
dclear(BG_COLOR);
unsigned int i = 0;
unsigned int x = 0;
unsigned int y = 0;
while (i != LEVEL_WIDTH*16)
{
switch (level[i])
{
case '0':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_ground);
break;
case 'v':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_spike);
break;
case '~':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_ground);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_ice);
break;
case '#':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_ground);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, &img_glue);
break;
case 'j':
if (upgrades[0]) erase_tile(x, y, level);
break;
case 'c':
if (erase_coin) erase_tile(x, y, level);
break;
}
x += 16;
if (x == 16*LEVEL_WIDTH)
{
x = 0;
y += 16;
}
i++;
}
}
void draw_level_anims(char level[], char upgrades[], unsigned int step)
{
unsigned int i = 0;
unsigned int x = 0;
unsigned int y = 0;
while (i != LEVEL_WIDTH*16)
{
switch (level[i])
{
case '^':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, ani_elevator[(step/2)%16]);
break;
case 'j':
{
unsigned int buffer = (step/20)%4 - 1;
if (buffer == 2) buffer = 0;
drect(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, x + 15 + DRAW_OFFSET_X,
y + 15 + DRAW_OFFSET_Y, BG_COLOR);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y + buffer, &img_jitem);
}
break;
case 'c':
{
unsigned int buffer = (step/20)%4 - 1;
if (buffer == 2) buffer = 0;
drect(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, x + 15 + DRAW_OFFSET_X,
y + 15 + DRAW_OFFSET_Y, BG_COLOR);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y + buffer, &img_coin);
}
break;
}
x += 16;
if (x == 16*LEVEL_WIDTH)
{
x = 0;
y += 16;
}
i++;
}
unsigned int i = 0;
unsigned int x = 0;
unsigned int y = 0;
while (i != LEVEL_WIDTH*16)
{
switch (level[i])
{
case '^':
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, ani_elevator[(step/2)%16]);
break;
case 'j':
{
unsigned int buffer = (step/20)%4 - 1;
if (buffer == 2) buffer = 0;
drect(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, x + 15 + DRAW_OFFSET_X,
y + 15 + DRAW_OFFSET_Y, BG_COLOR);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y + buffer, &img_jitem);
}
break;
case 'c':
{
unsigned int buffer = (step/20)%4 - 1;
if (buffer == 2) buffer = 0;
drect(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y, x + 15 + DRAW_OFFSET_X,
y + 15 + DRAW_OFFSET_Y, BG_COLOR);
dimage(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y + buffer, &img_coin);
}
break;
}
x += 16;
if (x == 16*LEVEL_WIDTH)
{
x = 0;
y += 16;
}
i++;
}
}
void erase_tile(int x, int y, char level[])
{
x = (int)(x/16);
y = (int)(y/16);
level[x + y * 28] = '.';
x *= 16;
y *= 16;
drect(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y,
x + 15 + DRAW_OFFSET_X,
y + 15 + DRAW_OFFSET_Y, BG_COLOR);
x = (int)(x/16);
y = (int)(y/16);
level[x + y * 28] = '.';
x *= 16;
y *= 16;
drect(x + DRAW_OFFSET_X, y + DRAW_OFFSET_Y,
x + 15 + DRAW_OFFSET_X,
y + 15 + DRAW_OFFSET_Y, BG_COLOR);
}
void draw_upgrade_message(char item)
{
switch (item)
{
case 'j':
{
extern image_t img_jitem_popup;
dimage(112 + DRAW_OFFSET_X, 88 + DRAW_OFFSET_Y, &img_jitem_popup);
break;
}
}
switch (item)
{
case 'j':
{
extern image_t img_jitem_popup;
dimage(112 + DRAW_OFFSET_X, 88 + DRAW_OFFSET_Y, &img_jitem_popup);
break;
}
}
}
void draw_timer(unsigned int step)
{
dprint(0, 0, C_WHITE, C_BLACK, "%u.%02u", step/60, step%60);
dprint(0, 0, C_WHITE, C_BLACK, "%u.%02u", step/60, step%60);
}

@ -25,227 +25,227 @@ int sgn(int number); //return the sign of input double
int callback(volatile void *arg)
{
volatile int *has_ticked = arg;
*has_ticked = 1;
return 0;
volatile int *has_ticked = arg;
*has_ticked = 1;
return 0;
}
int main(void)
{
volatile int has_ticked = 1; //fps cap
char level[477];
int level_id = 5050;
//START UPGRADES
char upgrades[] = {
0 //jump
};
//END UPGRADES
unsigned int coin_id = 0;
unsigned int coin_count = 0;
char jump_pressed = 1; //avoid holding jump
char jump_buffer = 0; //jump buffer, last 3 frames
unsigned int jump_hold = 0; //number of consecutive frames jump has been held
double vspd = 0.0; //player vertical speed
double hspd = 0; //player horizontal speed
int move = 0;
int direction = 0;
int on_ground = 6; //remember if player is on solid
int player_x = 0;
int player_y = 0;
int start_x = 18*16 + 2;
int start_y = 9*16 + 4;
int old_x, old_y;
char spawn_buffer = 0;
unsigned int step = 0;
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
draw_level_anims(level, upgrades, step);
player_x = start_x;
player_y = start_y;
old_x = player_x + 1; //offset to draw it on first cycle
old_y = player_y;
//fps cap timer
timer_setup(0, timer_delay(0, 16667), 0, callback, &has_ticked);
timer_start(0);
while (1) {
while(!has_ticked) sleep();
has_ticked = 0;
//START DRAW
step++;
draw_level_anims(level, upgrades, step);
draw_player(old_x, old_y, player_x, player_y);
draw_timer(step);
dupdate();
old_y = player_y;
old_x = player_x;
//END DRAW
clearevents();
//horizontal movement & collision
{
move = 0;
double friction = 1;
int trunc_hspd;
//ice
if (collide(player_x, player_y + 1, level, '~')) friction = 0.2;
move = keydown(KEY_RIGHT) + 0 - keydown(KEY_LEFT);
if (move) {
if (hspd < MAX_HSPD)
{
hspd += ACCELERATION * friction;
if (hspd > MAX_HSPD) hspd = MAX_HSPD;
}
direction = move;
}
else if (hspd > 0)
{
if (friction != 0.2) hspd -= DECELERATION * friction;
else hspd -= DECELERATION / 4 * friction;
if (hspd < 0) hspd = 0;
}
trunc_hspd = hspd * direction;
if (!collide(player_x, player_y + 1, level, '#'))
{
if (!collide_solid(player_x + trunc_hspd, player_y, level))
{
player_x += trunc_hspd;
}
else
{
int sign_hspd = sgn(trunc_hspd);
while (!collide_solid(player_x + sign_hspd, player_y, level))
{
player_x += sign_hspd;
}
hspd = 0;
}
}
}
//ground detection
if (vspd >= 0 && collide_solid(player_x, player_y + 1, level))
{
on_ground = 6;
vspd = 0;
if (spawn_buffer)
{
spawn_buffer = 0;
set_start_pos(&start_x, &start_y, player_x, player_y);
}
}
else
{
if (on_ground != 6 && vspd < MAX_VSPD) vspd += GRAV;
if (on_ground) on_ground--;
}
//vertical movement
if (UG_CAN_JUMP) jump_test(&jump_pressed, &jump_buffer, &jump_hold);
//jump
if (jump_buffer && on_ground) {
vspd = JUMP_SPD;
on_ground = 0;
jump_hold = JUMP_SCALE;
}
else if (jump_hold && keydown(KEY_SHIFT))
{
jump_hold--;
vspd += JUMP_REDUCTION;
}
else jump_hold = 0;
//elevator collision and raise
if ((collide(player_x - 1, player_y, level, '^') ||
collide(player_x + 1, player_y, level, '^')) && vspd > MIN_VSPD)
{
if (vspd > -2)
{
vspd -= GRAV;
}
vspd -= GRAV;
}
//vertical collision
{
int trunc_vspd = vspd; //truncate vspd
if (trunc_vspd)
{
if (!collide_solid(player_x, player_y + trunc_vspd, level))
{
player_y += trunc_vspd;
}
else
{
int sign_vspd = sgn(trunc_vspd);
while (!collide_solid(player_x, player_y + sign_vspd, level))
{
player_y += sign_vspd;
}
vspd = 0;
jump_hold = 0;
}
}
}
//spike collision and death
if (collide(player_x, player_y, level, 'v'))
{
player_x = start_x;
player_y = start_y;
vspd = 0;
}
//borders collision
if (player_x <= 22)
{
level_id -= 1;
player_x = 416 + (move * (int) hspd);
set_start_pos(&start_x, &start_y, player_x, player_y);
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
reset_old_pos(&old_x, &old_y);
}
else if (player_x >= 416)
{
level_id += 1;
player_x = 22 + (move * (int) hspd);
set_start_pos(&start_x, &start_y, player_x, player_y);
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
reset_old_pos(&old_x, &old_y);
}
else if (player_y <= 8)
{
level_id -= 100;
player_y = 246;
spawn_buffer = 1;
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
reset_old_pos(&old_x, &old_y);
}
else if (player_y >= 248)
{
level_id += 100;
player_y = 10;
set_start_pos(&start_x, &start_y, player_x, player_y);
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
reset_old_pos(&old_x, &old_y);
}
//item get
if (collide(player_x, player_y, level, 'j'))
{
UG_CAN_JUMP = 1;
erase_tile(player_x, player_y, level);
draw_upgrade_message('j');
}
//coin get
if (collide(player_x, player_y, level, 'c'))
{
coins[coin_id] = 1;
coin_count++;
erase_tile(player_x, player_y, level);
}
//exit
if (keydown(KEY_EXIT)) return 0;
}
volatile int has_ticked = 1; //fps cap
char level[477];
int level_id = 5050;
//START UPGRADES
char upgrades[] = {
0 //jump
};
//END UPGRADES
unsigned int coin_id = 0;
unsigned int coin_count = 0;
char jump_pressed = 1; //avoid holding jump
char jump_buffer = 0; //jump buffer, last 3 frames
unsigned int jump_hold = 0; //number of consecutive frames jump has been held
double vspd = 0.0; //player vertical speed
double hspd = 0; //player horizontal speed
int move = 0;
int direction = 0;
int on_ground = 6; //remember if player is on solid
int player_x = 0;
int player_y = 0;
int start_x = 18*16 + 2;
int start_y = 9*16 + 4;
int old_x, old_y;
char spawn_buffer = 0;
unsigned int step = 0;
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
draw_level_anims(level, upgrades, step);
player_x = start_x;
player_y = start_y;
old_x = player_x + 1; //offset to draw it on first cycle
old_y = player_y;
//fps cap timer
timer_setup(0, timer_delay(0, 16667), 0, callback, &has_ticked);
timer_start(0);
while (1) {
while(!has_ticked) sleep();
has_ticked = 0;
//START DRAW
step++;
draw_level_anims(level, upgrades, step);
draw_player(old_x, old_y, player_x, player_y);
draw_timer(step);
dupdate();
old_y = player_y;
old_x = player_x;
//END DRAW
clearevents();
//horizontal movement & collision
{
move = 0;
double friction = 1;
int trunc_hspd;
//ice
if (collide(player_x, player_y + 1, level, '~')) friction = 0.2;
move = keydown(KEY_RIGHT) + 0 - keydown(KEY_LEFT);
if (move) {
if (hspd < MAX_HSPD)
{
hspd += ACCELERATION * friction;
if (hspd > MAX_HSPD) hspd = MAX_HSPD;
}
direction = move;
}
else if (hspd > 0)
{
if (friction != 0.2) hspd -= DECELERATION * friction;
else hspd -= DECELERATION / 4 * friction;
if (hspd < 0) hspd = 0;
}
trunc_hspd = hspd * direction;
if (!collide(player_x, player_y + 1, level, '#'))
{
if (!collide_solid(player_x + trunc_hspd, player_y, level))
{
player_x += trunc_hspd;
}
else
{
int sign_hspd = sgn(trunc_hspd);
while (!collide_solid(player_x + sign_hspd, player_y, level))
{
player_x += sign_hspd;
}
hspd = 0;
}
}
}
//ground detection
if (vspd >= 0 && collide_solid(player_x, player_y + 1, level))
{
on_ground = 6;
vspd = 0;
if (spawn_buffer)
{
spawn_buffer = 0;
set_start_pos(&start_x, &start_y, player_x, player_y);
}
}
else
{
if (on_ground != 6 && vspd < MAX_VSPD) vspd += GRAV;
if (on_ground) on_ground--;
}
//vertical movement
if (UG_CAN_JUMP) jump_test(&jump_pressed, &jump_buffer, &jump_hold);
//jump
if (jump_buffer && on_ground) {
vspd = JUMP_SPD;
on_ground = 0;
jump_hold = JUMP_SCALE;
}
else if (jump_hold && keydown(KEY_SHIFT))
{
jump_hold--;
vspd += JUMP_REDUCTION;
}
else jump_hold = 0;
//elevator collision and raise
if ((collide(player_x - 1, player_y, level, '^') ||
collide(player_x + 1, player_y, level, '^')) && vspd > MIN_VSPD)
{
if (vspd > -2)
{
vspd -= GRAV;
}
vspd -= GRAV;
}
//vertical collision
{
int trunc_vspd = vspd; //truncate vspd
if (trunc_vspd)
{
if (!collide_solid(player_x, player_y + trunc_vspd, level))
{
player_y += trunc_vspd;
}
else
{
int sign_vspd = sgn(trunc_vspd);
while (!collide_solid(player_x, player_y + sign_vspd, level))
{
player_y += sign_vspd;
}
vspd = 0;
jump_hold = 0;
}
}
}
//spike collision and death
if (collide(player_x, player_y, level, 'v'))
{
player_x = start_x;
player_y = start_y;
vspd = 0;
}
//borders collision
if (player_x <= 22)
{
level_id -= 1;
player_x = 416 + (move * (int) hspd);
set_start_pos(&start_x, &start_y, player_x, player_y);
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
reset_old_pos(&old_x, &old_y);
}
else if (player_x >= 416)
{
level_id += 1;
player_x = 22 + (move * (int) hspd);
set_start_pos(&start_x, &start_y, player_x, player_y);
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
reset_old_pos(&old_x, &old_y);
}
else if (player_y <= 8)
{
level_id -= 100;
player_y = 246;
spawn_buffer = 1;
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
reset_old_pos(&old_x, &old_y);
}
else if (player_y >= 248)
{
level_id += 100;
player_y = 10;
set_start_pos(&start_x, &start_y, player_x, player_y);
set_level(level_id, level, &coin_id);
DRAW_LEVEL();
reset_old_pos(&old_x, &old_y);
}
//item get
if (collide(player_x, player_y, level, 'j'))
{
UG_CAN_JUMP = 1;
erase_tile(player_x, player_y, level);
draw_upgrade_message('j');
}
//coin get
if (collide(player_x, player_y, level, 'c'))
{
coins[coin_id] = 1;
coin_count++;
erase_tile(player_x, player_y, level);
}
//exit
if (keydown(KEY_EXIT)) return 0;
}
}
int sgn(int number)
{
if (number < 0) return -1;
else return 1;
if (number < 0) return -1;
else return 1;
}

@ -20,8 +20,8 @@ void jump_test(char *jump_pressed, char *jump_buffer, unsigned int *jump_hold)
void set_start_pos(int *start_x, int *start_y, int x, int y)
{
*start_x = x;
*start_y = y;
*start_x = x;
*start_y = y;
}
void reset_old_pos(int *old_x, int *old_y)

Loading…
Cancel
Save