improve fixed point notation

This commit is contained in:
Lephenixnoir 2021-07-16 18:49:58 +02:00
parent 72e154a618
commit 90f43a2b7e
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
10 changed files with 58 additions and 68 deletions

View File

@ -4,55 +4,55 @@
static enemy_t const slime = {
.name = "Slime",
.hitbox = (frect_t){ -FIX(3)/16, FIX(4)/16, -FIX(2)/16, FIX(3)/16 },
.sprite = (frect_t){ -FIX(5)/16, FIX(5)/16, -FIX(4)/16, FIX(3)/16 },
.hitbox = (frect_t){ -fix(3)/16, fix(4)/16, -fix(2)/16, fix(3)/16 },
.sprite = (frect_t){ -fix(5)/16, fix(5)/16, -fix(4)/16, fix(3)/16 },
.anim_idle = { anim_slime_idle_left, anim_slime_idle_right },
.anim_death = { anim_slime_death_left, anim_slime_death_right },
.movement_params = {
.max_speed = FIX(1),
.propulsion = FIX(12),
.max_speed = fix(1),
.propulsion = fix(12),
},
.HP = {
.base = FIX(10),
.growth = FIX(5),
.affinity = FIX(3),
.base = fix(10),
.growth = fix(5),
.affinity = fix(3),
},
.ATK = {
.base = FIX(5),
.growth = FIX(2),
.affinity = FIX(1),
.base = fix(5),
.growth = fix(2),
.affinity = fix(1),
},
.DEF = {
.base = FIX(2),
.growth = FIX(1),
.affinity = FIX(1)/2,
.base = fix(2),
.growth = fix(1),
.affinity = fix(0.5),
},
};
static enemy_t const bat = {
.name = "Bat",
.hitbox = (frect_t){ -FIX(3)/16, FIX(4)/16, -FIX(2)/16, FIX(3)/16 },
.sprite = (frect_t){ -FIX(5)/16, FIX(5)/16, -FIX(4)/16, FIX(3)/16 },
.hitbox = (frect_t){ -fix(3)/16, fix(4)/16, -fix(2)/16, fix(3)/16 },
.sprite = (frect_t){ -fix(5)/16, fix(5)/16, -fix(4)/16, fix(3)/16 },
.anim_idle = { anim_bat_idle_left, anim_bat_idle_right },
.anim_death = { anim_bat_death_left, anim_bat_death_right },
.movement_params = {
.max_speed = FIX(2),
.propulsion = FIX(8),
.max_speed = fix(1.8),
.propulsion = fix(8),
},
.HP = {
.base = FIX(8),
.growth = FIX(4),
.affinity = FIX(2),
.base = fix(8),
.growth = fix(4),
.affinity = fix(2),
},
.ATK = {
.base = FIX(8),
.growth = FIX(2),
.affinity = FIX(2),
.base = fix(8),
.growth = fix(2),
.affinity = fix(2),
},
.DEF = {
.base = FIX(2),
.growth = FIX(2),
.affinity = FIX(1),
.base = fix(2),
.growth = fix(2),
.affinity = fix(1),
},
};

View File

@ -201,7 +201,7 @@ static bool slash_hit_apply(game_t *game, effect_area_t *ea, entity_t *e)
int damage = entity_damage(e, ea->data.slash.strength);
/* Knockback */
fixed_t r = (rand() & (fix(1)/4-1)) + fix(7)/8;
fixed_t r = (rand() & (fix(0.25)-1)) + fix(0.875);
/* Half knockback against players */
if(e->identity == 0) r /= 2;
@ -213,7 +213,7 @@ static bool slash_hit_apply(game_t *game, effect_area_t *ea, entity_t *e)
particle_damage_t *p = malloc(sizeof *p);
p->particle.type = PARTICLE_DAMAGE;
p->particle.age = 0;
p->particle.pos = (fpoint_t){ e->movement.x, e->movement.y - fix(1)/2 };
p->particle.pos = (fpoint_t){ e->movement.x, e->movement.y - fix(0.5) };
p->damage = damage;
p->color = (e->identity == 0) ? C_RED : C_WHITE;
game_add_particle(game, &p->particle);

View File

@ -28,13 +28,7 @@ static inline fixed_t fdiv(fixed_t left, fixed_t right)
return d / right;
}
static inline fixed_t fix(int constant)
{
return constant << 16;
}
/* Compile-time version */
#define FIX(c) ((c) * 65536)
#define fix(x) ((int)((x) * 65536))
static inline fixed_t fixdouble(double constant)
{
@ -83,7 +77,7 @@ static inline fixed_t feasein(fixed_t x)
static inline fixed_t fease(fixed_t x)
{
if(x <= fix(1) / 2) {
if(x <= fix(0.5)) {
return 2 * fmul(x, x);
}
else {

View File

@ -263,7 +263,7 @@ void game_spawn_enemies(game_t *g)
if(s->time > g->time_wave) break;
entity_t *e = enemy_spawn(s->identity, s->level);
fpoint_t pos = { fix(s->x) + fix(1)/2, fix(s->y) + fix(1)/2 };
fpoint_t pos = { fix(s->x) + fix(0.5), fix(s->y) + fix(0.5) };
game_spawn_entity(g, e, pos);
g->wave_spawned++;
}

View File

@ -27,7 +27,7 @@ irect_t rect_f2i(frect_t r)
fpoint_t point_i2f_center(ipoint_t p)
{
return (fpoint_t){ fix(p.x) + fix(1)/2, fix(p.y) + fix(1)/2 };
return (fpoint_t){ fix(p.x) + fix(0.5), fix(p.y) + fix(0.5) };
}
//---

View File

@ -46,7 +46,7 @@ typedef struct irect irect_t;
fpoint_t point_i2f(ipoint_t);
frect_t rect_i2f(irect_t);
/* Same as point_i2f() but adds +fix(1)/2 at each coordinate. */
/* Same as point_i2f() but adds +0.5 at each coordinate. */
fpoint_t point_i2f_center(ipoint_t);
/* Functions converting fixed-point shapes to integer. */

View File

@ -16,20 +16,20 @@ level_t lv_1 = {
/* Wave 1: Just some slimes */
{ .enemy_count = 6,
.enemies = (level_wave_spawn_t []){
{ ENEMY_SLIME, 1, 2,1, FIX(0)/2 },
{ ENEMY_SLIME, 1, 19,9, FIX(1)/2 },
{ ENEMY_SLIME, 1, 2,1, FIX(2)/2 },
{ ENEMY_SLIME, 1, 19,9, FIX(3)/2 },
{ ENEMY_SLIME, 2, 2,1, FIX(4)/2 },
{ ENEMY_SLIME, 2, 19,9, FIX(5)/2 },
{ ENEMY_SLIME, 1, 2,1, fix(0.0) },
{ ENEMY_SLIME, 1, 19,9, fix(0.5) },
{ ENEMY_SLIME, 1, 2,1, fix(1.0) },
{ ENEMY_SLIME, 1, 19,9, fix(1.5) },
{ ENEMY_SLIME, 2, 2,1, fix(2.0) },
{ ENEMY_SLIME, 2, 19,9, fix(2.5) },
}},
/* Wave 2: Stronger slimes and a bat */
{ .enemy_count = 4,
.enemies = (level_wave_spawn_t []){
{ ENEMY_SLIME, 2, 2,1, FIX(0)/2 },
{ ENEMY_SLIME, 2, 19,9, FIX(0)/2 },
{ ENEMY_BAT, 1, 2,1, FIX(2)/2 },
{ ENEMY_BAT, 1, 19,9, FIX(2)/2 },
{ ENEMY_SLIME, 2, 2,1, fix(0.0) },
{ ENEMY_SLIME, 2, 19,9, fix(0.0) },
{ ENEMY_BAT, 1, 2,1, fix(1.0) },
{ ENEMY_BAT, 1, 19,9, fix(1.0) },
}},
},
};

View File

@ -83,8 +83,8 @@ int main(void)
struct tile *t = map_tile(m, x, y);
if(t && !t->solid) break;
}
player->movement.x = fix(x) + fix(1) / 2;
player->movement.y = fix(y) + fix(1) / 2;
player->movement.x = fix(x) + fix(0.5);
player->movement.y = fix(y) + fix(0.5);
game_add_entity(&game, player);
game.player = player;
@ -284,9 +284,9 @@ int main(void)
}
if(ev.key == KEY_LN)
emp_player.dash_speed += fix(1) / 2;
emp_player.dash_speed += fix(0.5);
if(ev.key == KEY_LEFTP) {
emp_player.dash_speed -= fix(1) / 2;
emp_player.dash_speed -= fix(0.5);
if(emp_player.dash_speed <= 0)
emp_player.dash_speed = 0;
}
@ -400,8 +400,8 @@ int main(void)
fpoint_t dir = fdir(facing);
fpoint_t anchor = rect_center(entity_sprite(e));
anchor.x += fmul(fix(3)/4, dir.x);
anchor.y += fmul(fix(3)/4, dir.y);
anchor.x += fmul(fix(0.75), dir.x);
anchor.y += fmul(fix(0.75), dir.y);
effect_area_t *area = effect_area_new(EFFECT_HIT);
area->sprite = hitbox;
@ -423,14 +423,14 @@ int main(void)
/* Player attack */
if(player->HP > 0 && attack && !player->current_attack) {
frect_t hitbox = {
-fix(5)/8, fix(5)/8, -fix(1)/4, fix(1)/4,
-fix(10)/16, fix(10)/16, -fix(4)/16, fix(4)/16,
};
hitbox = rect_rotate(hitbox, UP, player->movement.facing);
fpoint_t dir = fdir(player->movement.facing);
fpoint_t anchor = rect_center(entity_sprite(player));
anchor.x += fmul(fix(3)/4, dir.x);
anchor.y += fmul(fix(3)/4, dir.y);
anchor.x += fmul(fix(0.75), dir.x);
anchor.y += fmul(fix(0.75), dir.y);
effect_area_t *area = effect_area_new(EFFECT_SLASH);
area->sprite = hitbox;

View File

@ -6,10 +6,10 @@ frect_t tile_shape(struct tile const *tile)
if(!tile->solid) return (frect_t){ 0 };
return (frect_t){
.l = -fix(1)/2,
.r = fix(1)/2,
.t = -fix(1)/2,
.b = fix(1)/2,
.l = -fix(0.5),
.r = fix(0.5),
.t = -fix(0.5),
.b = fix(0.5),
};
}
@ -34,7 +34,7 @@ bool map_collides(map_t const *m, frect_t hitbox)
struct tile *t = map_tile(m, x, y);
if(!t || !t->solid) continue;
fpoint_t center = { fix(x) + fix(1)/2, fix(y) + fix(1)/2 };
fpoint_t center = { fix(x) + fix(0.5), fix(y) + fix(0.5) };
frect_t tile_hitbox = rect_translate(tile_shape(t), center);
if(rect_collide(hitbox, tile_hitbox))

View File

@ -25,9 +25,5 @@
#define CAMERA_SPEED_Y fix(3)
/* Ideal frame rate (FPS) */
#define FRAME_RATE 30
/* Player reach for attacks, in tiles */
#define ATTACK_REACH fix(1)
/* Width of the cone where attacks reach, in cosine */
#define ATTACK_ANGLE_COS fix(1)/2 /* 60 degrees */
/* Knockback speed applied to hit enemies */
/* Knockback speed when entities are hit */
#define KNOCKBACK_SPEED fix(15)