2025-03-01 05:58:12 -05:00
// Copyright 2025 Bill Rossi
//
// This file is part of Starship Futuretime
//
// Starship Futuretime is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// Starship Futuretime is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Starship Futuretime. If not, see <https://www.gnu.org/licenses/>.
2025-03-01 08:12:40 -05:00
# include <stdio.h>
2025-03-01 05:58:12 -05:00
# include <raylib.h>
# include "player.h"
2025-03-08 10:25:24 -05:00
void initialize_player ( Game * g , Player * p ) {
2025-03-01 05:58:12 -05:00
p - > position = ( Vector2 ) { 400 , 300 } ;
2025-03-01 08:12:40 -05:00
p - > velocity = ( Vector2 ) { 0 , 0 } ;
p - > acceleration = ( Vector2 ) { 0 , 0 } ;
2025-03-01 05:58:12 -05:00
p - > pitch = PITCH_NEUTRAL ;
p - > roll = ROLL_NEUTRAL ;
2025-03-04 20:23:41 -05:00
Image sprite_img = LoadImage ( " img/player.png " ) ;
p - > spritesheet = LoadTextureFromImage ( sprite_img ) ;
UnloadImage ( sprite_img ) ;
2025-03-08 10:25:24 -05:00
p - > game = g ;
2025-03-01 05:58:12 -05:00
}
void handle_player_input ( Player * p ) {
if ( IsKeyDown ( KEY_DOWN ) ) p - > pitch = PITCH_DESCENDING ;
else if ( IsKeyDown ( KEY_UP ) ) p - > pitch = PITCH_ASCENDING ;
else p - > pitch = PITCH_NEUTRAL ;
if ( IsKeyDown ( KEY_LEFT ) ) p - > roll = ROLL_LEFT ;
else if ( IsKeyDown ( KEY_RIGHT ) ) p - > roll = ROLL_RIGHT ;
else p - > roll = ROLL_NEUTRAL ;
}
void draw_player ( Player * p ) {
2025-03-04 20:49:52 -05:00
DrawTextureRec ( p - > spritesheet , ( Rectangle ) { 128 , 128 , 128 , 128 } , ( Vector2 ) { p - > position . x - 64 , p - > position . y - 29 } , WHITE ) ;
2025-03-01 05:58:12 -05:00
}
2025-03-01 08:12:40 -05:00
void move_player ( Player * p ) {
switch ( p - > roll ) {
case ROLL_LEFT : p - > acceleration . x = - ACCEL_X ; break ;
case ROLL_NEUTRAL : p - > acceleration . x = 0 ; break ;
case ROLL_RIGHT : p - > acceleration . x = ACCEL_X ; break ;
}
switch ( p - > pitch ) {
2025-03-08 10:34:37 -05:00
case PITCH_DESCENDING : p - > acceleration . y = .4 ; break ;
2025-03-01 08:12:40 -05:00
case PITCH_NEUTRAL : p - > acceleration . y = 0 ; break ;
2025-03-08 10:34:37 -05:00
case PITCH_ASCENDING : p - > acceleration . y = - .4 ; break ;
2025-03-01 08:12:40 -05:00
}
p - > velocity . x + = p - > acceleration . x ;
p - > velocity . x = CLAMP ( p - > velocity . x , MAX_VEL_X ) ;
p - > velocity . x * = DAMPING_X ;
p - > velocity . y + = p - > acceleration . y ;
2025-03-08 10:34:37 -05:00
p - > velocity . y = MAX ( MIN ( p - > velocity . y , - MIN_VEL_Y ) , - MAX_VEL_Y ) ;
2025-03-01 08:12:40 -05:00
p - > position . x + = p - > velocity . x ;
p - > position . y + = p - > velocity . y ;
2025-03-08 10:25:24 -05:00
p - > game - > camera - > target = p - > position ;
2025-03-09 09:48:45 -04:00
p - > game - > camera - > offset . y = 400 ;
2025-03-08 10:48:55 -05:00
int level_width_in_pixels = p - > game - > level - > width * 32 ;
bool too_far_left = p - > position . x < 400 ;
bool too_far_right = level_width_in_pixels - p - > position . x < 400 ;
if ( too_far_left ) {
p - > game - > camera - > offset . x = p - > position . x ;
} else if ( too_far_right ) {
2025-03-08 19:01:23 -05:00
p - > game - > camera - > offset . x = p - > position . x - level_width_in_pixels + 800 ;
2025-03-08 10:48:55 -05:00
} else {
p - > game - > camera - > offset . x = 400 ;
}
2025-03-01 08:12:40 -05:00
}