--- a/test/snake/snake.c Sun Jul 13 17:17:15 2025 +0200 +++ b/test/snake/snake.c Mon Jul 14 21:56:53 2025 +0200 @@ -44,6 +44,31 @@ #define BACKDROP_SCENE asc_window_scene(0) #define MAIN_SCENE asc_window_scene(1) +enum MoveDirection { + MOVE_UP, + MOVE_LEFT, + MOVE_DOWN, + MOVE_RIGHT +}; + +static asc_transform rotations[4]; +static asc_vec2f directions[4]; + +typedef struct { + enum MoveDirection direction; +} Spaceship; + +static void init_globals(void) { + 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); +} + static void destroy_textures(void) { asc_texture_destroy(tex2d, TEX2D_COUNT); } @@ -70,7 +95,7 @@ if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) || asc_active_window->resized) { asc_vec2u bottom_right = asc_active_window->dimensions; asc_vec2u text_size = ((AscText*)node)->dimension; - asc_node_set_position2f(node, ASC_VEC2F( + asc_scene_node_set_position2f(node, ASC_VEC2F( (int) bottom_right.x - (int) text_size.width - 10, (int) bottom_right.y - (int) text_size.height - 10 )); @@ -115,56 +140,30 @@ asc_ui_add_node(node); } -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 void move_spaceship(AscBehavior *behavior) { + AscSceneNode *node = behavior->node; + Spaceship *spaceship = node->user_data; + float speed = 32.f * asc_context.frame_factor; + asc_vec2f movement = asc_vec2f_scale(directions[spaceship->direction], speed); + asc_scene_node_move2f(node, movement); + asc_scene_node_set_rotation(node, 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, .x = 250, .y = 300, - .width = 64, - .height = 64, - .origin_x = 32, - .origin_y = 32, + .width = 32, + .height = 32, + .origin_x = 16, + .origin_y = 16, ); asc_scene_add_node(MAIN_SCENE, sprite); - // TODO: this is never freed at the moment - Spaceship *ship = cxZallocDefault(sizeof(Spaceship)); - ship->sprite = sprite; - return ship; + asc_scene_node_allocate_data(sprite, sizeof(Spaceship)); + asc_behavior_add(sprite, move_spaceship); + return sprite->user_data; } static asc_rect update_viewport_for_window_resize(asc_vec2u window_size) { @@ -197,6 +196,9 @@ asc_set_texture_path("../../test/snake/textures"); #endif + // initialize globals + init_globals(); + // create window AscWindowSettings settings; asc_window_settings_init_defaults(&settings); @@ -208,7 +210,7 @@ init_textures(); // initialize the scenes - const int game_field_size = 500; + const int game_field_size = 512; asc_scene_init(BACKDROP_SCENE, .type = ASC_CAMERA_ORTHO, .projection_update_func = asc_camera_ortho_update_size @@ -244,10 +246,24 @@ // player rotation if (asc_key_pressed(ASC_KEY(LEFT))) { - turn_left(spaceship); + if (spaceship->direction != MOVE_RIGHT) { + spaceship->direction = MOVE_LEFT; + } } if (asc_key_pressed(ASC_KEY(RIGHT))) { - turn_right(spaceship); + if (spaceship->direction != MOVE_LEFT) { + spaceship->direction = MOVE_RIGHT; + } + } + if (asc_key_pressed(ASC_KEY(UP))) { + if (spaceship->direction != MOVE_DOWN) { + spaceship->direction = MOVE_UP; + } + } + if (asc_key_pressed(ASC_KEY(DOWN))) { + if (spaceship->direction != MOVE_UP) { + spaceship->direction = MOVE_DOWN; + } } // debug-key for clearing the shader registry