2025-03-01 08:53:38 -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/>.
# include <stdlib.h>
# include <raylib.h>
# include "enemy.h"
# include "entity.h"
2025-03-04 21:02:55 -05:00
Texture2D * enemy_sprite = NULL ;
2025-03-01 08:53:38 -05:00
Entity * spawn_enemy ( void ) {
2025-03-04 21:02:55 -05:00
if ( enemy_sprite = = NULL ) {
enemy_sprite = malloc ( sizeof ( Texture2D ) ) ;
Image sprite_img = LoadImage ( " img/enemy.png " ) ;
* enemy_sprite = LoadTextureFromImage ( sprite_img ) ;
UnloadImage ( sprite_img ) ;
}
2025-03-01 08:53:38 -05:00
Entity * e = malloc ( sizeof ( Entity ) ) ;
e - > name = " Enemy " ;
e - > properties = malloc ( sizeof ( Entity ) ) ;
2025-03-01 11:34:51 -05:00
e - > timer = 1.0 ;
2025-03-01 08:53:38 -05:00
EnemyProperties * props = ( EnemyProperties * ) e - > properties ;
props - > position . x = 400 ;
props - > position . y = 50 ;
props - > position . width = 25 ;
props - > position . height = 25 ;
props - > velocity . x = 3 ;
props - > velocity . y = 0 ;
2025-03-02 18:58:52 -05:00
props - > bullet_timer = rand ( ) % 4 ;
2025-03-01 08:53:38 -05:00
e - > draw = & draw_enemy ;
e - > tick = & tick_enemy ;
e - > free = & free_enemy ;
return e ;
}
void draw_enemy ( Entity * e ) {
EnemyProperties * props = e - > properties ;
2025-03-04 21:02:55 -05:00
// DrawRectangleRec(props->position, RED);
DrawTextureRec ( * enemy_sprite , ( Rectangle ) { props - > velocity . x > 0 ? 32 : 0 , 0 , 32 , 32 } , ( Vector2 ) { props - > position . x , props - > position . y } , WHITE ) ;
2025-03-01 08:53:38 -05:00
}
2025-03-02 18:58:52 -05:00
void update_position_enemy ( Entity * e , float dt ) {
2025-03-01 08:53:38 -05:00
EnemyProperties * props = e - > properties ;
if ( props - > position . x < 200 ) props - > velocity . x = 3 ;
if ( props - > position . x > 600 ) props - > velocity . x = - 3 ;
props - > position . x + = props - > velocity . x ;
}
2025-03-02 18:58:52 -05:00
void tick_enemy ( Game * g , Entity * e , float dt ) {
EnemyProperties * props = e - > properties ;
update_position_enemy ( e , dt ) ;
props - > bullet_timer - = dt ;
if ( props - > bullet_timer < 0. ) {
props - > bullet_timer = ( float ) ( rand ( ) % 4 ) ;
2025-03-08 10:34:37 -05:00
add_entity ( g - > bullets , spawn_bullet ( ( Vector2 ) { props - > position . x , props - > position . y } , props - > velocity , true ) ) ;
2025-03-02 18:58:52 -05:00
}
}
2025-03-01 08:53:38 -05:00
void free_enemy ( Entity * e ) {
free ( e - > properties ) ;
free ( e ) ;
}