ninja dash

This commit is contained in:
nemo 2024-09-27 15:16:05 +02:00
parent 328903af6d
commit c43558413c
3 changed files with 45 additions and 21 deletions

28
main.c
View File

@ -78,20 +78,22 @@ int main(void)
camera.offset = SCREEN_MIDDLE; camera.offset = SCREEN_MIDDLE;
} }
float movement_speed = 10.0f * DT * 50; if (ninja_dash_time() <= EPSILON) {
if ((key_down(KC_UP) || key_down(KC_DOWN)) && (key_down(KC_LEFT) || key_down(KC_RIGHT))) float movement_speed = 10.0f * DT * 50;
movement_speed = sqrtf(movement_speed) * 2; if ((key_down(KC_UP) || key_down(KC_DOWN)) && (key_down(KC_LEFT) || key_down(KC_RIGHT)))
if (key_down(KC_CROUCH)) movement_speed = sqrtf(movement_speed) * 2;
movement_speed /= 2.0f; if (key_down(KC_CROUCH))
movement_speed /= 2.0f;
if (key_down(KC_LEFT)) if (key_down(KC_LEFT))
camera.target.x -= movement_speed ; camera.target.x -= movement_speed ;
if (key_down(KC_RIGHT)) if (key_down(KC_RIGHT))
camera.target.x += movement_speed ; camera.target.x += movement_speed ;
if (key_down(KC_DOWN)) if (key_down(KC_DOWN))
camera.target.y -= movement_speed ; camera.target.y -= movement_speed ;
if (key_down(KC_UP)) if (key_down(KC_UP))
camera.target.y += movement_speed ; camera.target.y += movement_speed ;
}
camera_bound(&camera, map.box, ninja_radius()); camera_bound(&camera, map.box, ninja_radius());

36
ninja.c
View File

@ -6,6 +6,8 @@
Texture ninja_texture; Texture ninja_texture;
float dash_time = 0;
void load_ninja() void load_ninja()
{ {
ninja_texture = LoadTexture("data/ninja.png"); ninja_texture = LoadTexture("data/ninja.png");
@ -16,21 +18,39 @@ float ninja_radius()
return fmax(ninja_texture.width, ninja_texture.height)/2; return fmax(ninja_texture.width, ninja_texture.height)/2;
} }
Vector2 ninja_dash(Vector2 pos) Vector2 ninja_dash(Vector2 pos, Vector2 to, float sec)
{ {
static Vector2 target = {0};
Vector2 dash = {0}; Vector2 dash = {0};
Vector2 mouse = GetMousePosition(); if (sec <= EPSILON)
Vector2 diff = Vector2Subtract(mouse, pos); return dash;
dash = Vector2Normalize(diff); if (key_pressed(KC_SHOOT)) {
dash.x *= 100; dash_time = sec;
dash.y *= 100; Vector2 diff = Vector2Subtract(to, pos);
Vector2 scale = Vector2Normalize(diff);
int dist = 200;
scale.x *= dist;
scale.y *= dist;
target = Vector2Add(pos, scale);
}
if (dash_time > 0) {
float dt = GetFrameTime();
dash_time -= dt;
dash.x = Lerp(pos.x, target.x, dt/sec) - pos.x;
dash.y = Lerp(pos.y, target.y, dt/sec) - pos.y;
}
return dash; return dash;
} }
float ninja_dash_time()
{
return dash_time;
}
void draw_ninja(Vector2 pos, Camera2D *cam) void draw_ninja(Vector2 pos, Camera2D *cam)
{ {
if (key_pressed(KC_SHOOT)) Vector2 dash = ninja_dash(GetScreenToWorld2D(pos, *cam), GetScreenToWorld2D(GetMousePosition(), *cam), 0.12);
cam->target = Vector2Add(cam->target, ninja_dash(pos)); cam->target = Vector2Add(cam->target, dash);
Texture n = ninja_texture; Texture n = ninja_texture;
Rectangle source = {0, 0, n.width, n.height}; Rectangle source = {0, 0, n.width, n.height};

View File

@ -7,4 +7,6 @@ float ninja_radius();
void draw_ninja(Vector2 pos, Camera2D *cam); void draw_ninja(Vector2 pos, Camera2D *cam);
float ninja_dash_time();
#endif // NINJA_H #endif // NINJA_H