Sun, 13 Jul 2025 17:17:15 +0200
hack a quick example for both rotation directions
test/snake/snake.c | file | annotate | diff | comparison | revisions |
--- a/test/snake/snake.c Sun Jul 13 15:09:04 2025 +0200 +++ b/test/snake/snake.c Sun Jul 13 17:17:15 2025 +0200 @@ -115,7 +115,41 @@ asc_ui_add_node(node); } -static AscSceneNode *create_spaceship(void) { +enum MoveDirection { + MOVE_UP, + MOVE_LEFT, + MOVE_DOWN, + MOVE_RIGHT +}; + +static asc_transform rotations[4]; +static asc_vec2f directions[4]; + +typedef struct { + AscSceneNode *sprite; + enum MoveDirection direction; +} Spaceship; + +static void turn_left(Spaceship *spaceship) { + spaceship->direction = (spaceship->direction + 1) % 4; + asc_node_set_rotation(spaceship->sprite, rotations[spaceship->direction]); +} + +static void turn_right(Spaceship *spaceship) { + spaceship->direction = (spaceship->direction + 3) % 4; + asc_node_set_rotation(spaceship->sprite, rotations[spaceship->direction]); +} + +static Spaceship *create_spaceship(void) { + // TODO: this all belongs somewhere else, this is just quickly hacked into here for testing + asc_transform_identity(rotations[MOVE_UP]); + asc_transform_roll(rotations[MOVE_LEFT], asc_rad(-90)); + asc_transform_roll(rotations[MOVE_RIGHT], asc_rad(90)); + asc_transform_roll(rotations[MOVE_DOWN], asc_rad(180)); + directions[MOVE_UP] = ASC_VEC2F(0, -1); + directions[MOVE_LEFT] = ASC_VEC2F(-1, 0); + directions[MOVE_DOWN] = ASC_VEC2F(0, 1); + directions[MOVE_RIGHT] = ASC_VEC2F(1, 0); AscSceneNode *sprite = asc_sprite( .name = "Player", .texture = TEXTURE_SHIP, @@ -127,7 +161,10 @@ .origin_y = 32, ); asc_scene_add_node(MAIN_SCENE, sprite); - return sprite; + // TODO: this is never freed at the moment + Spaceship *ship = cxZallocDefault(sizeof(Spaceship)); + ship->sprite = sprite; + return ship; } static asc_rect update_viewport_for_window_resize(asc_vec2u window_size) { @@ -192,7 +229,7 @@ create_score_counter(); // create spaceship - AscSceneNode *spaceship = create_spaceship(); + Spaceship *spaceship = create_spaceship(); // Main Loop do { @@ -207,8 +244,10 @@ // player rotation if (asc_key_pressed(ASC_KEY(LEFT))) { - asc_node_roll_deg(spaceship, -90); - asc_node_update_transform(spaceship); + turn_left(spaceship); + } + if (asc_key_pressed(ASC_KEY(RIGHT))) { + turn_right(spaceship); } // debug-key for clearing the shader registry