diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/assets.c | 5 | ||||
-rw-r--r-- | src/background.c | 11 | ||||
-rw-r--r-- | src/game.c | 32 | ||||
-rw-r--r-- | src/main.c | 4 | ||||
-rw-r--r-- | src/player.c | 37 |
5 files changed, 65 insertions, 24 deletions
diff --git a/src/assets.c b/src/assets.c index 498c801..b08201a 100644 --- a/src/assets.c +++ b/src/assets.c @@ -15,6 +15,11 @@ Asset assets[] = { .name = "rails", .filePath = "assets/art/backgrounds/rails.png" }, + { + .type = Texture_Asset, + .name = "human-shadow", + .filePath = "assets/art/characters/shadow.png" + } }; #define ASSET_COUNT (sizeof(assets) / sizeof(Asset)) diff --git a/src/background.c b/src/background.c index 76a3edf..d3588a7 100644 --- a/src/background.c +++ b/src/background.c @@ -9,7 +9,6 @@ void AddBackground(const char* backgroundTextureName) { e.type = Background_Entity; e.flags |= ENTITY_VISIBLE; - e.drawLayer = Background_Layer; e.position = (Vector2) {0.0f, 0.0f}; @@ -32,11 +31,11 @@ void AddBackground(const char* backgroundTextureName) { Vector2 backgroundBounds = Vector2Scale((Vector2) {backgroundTexture.width, backgroundTexture.height}, backgroundSizeScale); - e.destRect = (Rectangle) { - .width = backgroundBounds.x, - .height = backgroundBounds.y - }; - e.texture = backgroundTexture; + AddSpriteToEntity(&e, (Sprite){ + .texture = backgroundTexture, + .layer = Background_Layer, + .destRect = {NO_OFFSET, NO_OFFSET, backgroundBounds.x, backgroundBounds.y} + }); AddEntity(&e); } @@ -7,17 +7,17 @@ #include "assets.h" #include "player.h" #include "background.h" +#include <math.h> Game game; static inline bool ShouldDrawEntity(const Entity* e, DrawLayer layerDrawing) { return EntityAllocated(e) - && (e->flags & ENTITY_VISIBLE) - && (layerDrawing == e->drawLayer); + && (e->flags & ENTITY_VISIBLE); } -static inline Rectangle GetDrawDestinationRectGlobal(const Entity* e) { - Rectangle destRectGlobal = e->destRect; +static inline Rectangle GetSpriteDrawDestinationRectGlobal(const Entity* e, int spriteIndex) { + Rectangle destRectGlobal = e->sprites[spriteIndex].destRect; destRectGlobal.x += e->position.x; destRectGlobal.y += e->position.y; @@ -78,20 +78,30 @@ void UpdateGame(float deltaTime) { } } -void DefaultDrawEntity(const Entity* e) { - Rectangle srcRect = {0, 0, e->texture.width, e->texture.height}; +void DrawEntitySprite(const Entity* e, int spriteIndex) { + const Sprite* drawnSprite = &e->sprites[spriteIndex]; + + Rectangle srcRect = + {0, 0, drawnSprite->texture.width, drawnSprite->texture.height}; Vector2 origin = {0.0f, 0.0f}; float rotation = 0.0f; - Rectangle destRect = GetDrawDestinationRectGlobal(e); + Rectangle destRect = GetSpriteDrawDestinationRectGlobal(e, spriteIndex); + + DrawTexturePro(drawnSprite->texture, srcRect, destRect, origin, rotation, WHITE); +} - DrawTexturePro(e->texture, srcRect, destRect, origin, rotation, WHITE); +void DefaultDrawEntity(const Entity* e, DrawLayer toLayer) { + for (int i = 0; i < e->numSprites; i++) { + if (e->sprites[i].layer == toLayer) + DrawEntitySprite(e, i); + } } -void DrawEntity(const Entity* e) { +void DrawEntity(const Entity* e, DrawLayer toLayer) { switch (e->type) { default: - DefaultDrawEntity(e); + DefaultDrawEntity(e, toLayer); } } @@ -100,7 +110,7 @@ void DrawEntitiesInLayer(int layer) { Entity* currentEntity = &game.entities[i]; if (ShouldDrawEntity(currentEntity, layer)) - DrawEntity(currentEntity); + DrawEntity(currentEntity, layer); } } @@ -6,15 +6,13 @@ #include "player.h" int main() { - SetConfigFlags(FLAG_VSYNC_HINT); //Always better than fixed FPS imo InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "My BeatEmup"); - SetTargetFPS(0); + SetTargetFPS(60); LoadAssets(); InitGame(); AddBackground("bar-background"); - AddPlayer(0, 0); AddWall(100, 100, 1000, 100); diff --git a/src/player.c b/src/player.c index 6426db7..67f971f 100644 --- a/src/player.c +++ b/src/player.c @@ -1,13 +1,29 @@ +#include <stdlib.h> #include <math.h> #include "constants.h" #include "game.h" #include "player.h" #include "physics.h" +#include "assets.h" extern Game game; #define PLAYER_SPEED 300.0f +const Rectangle shadowDestRect = (Rectangle) { + .x = 0, + .y = 100, + .width = 100, + .height = 40 +}; + +const Rectangle physicsCollider = (Rectangle) { + .x = 0, + .y = 0, + .width = 100, + .height = 100 +}; + void UpdatePlayer(Entity* player, float deltaTime) { player->velocity = (Vector2) {0.0f, 0.0f}; @@ -31,7 +47,7 @@ void UpdatePlayer(Entity* player, float deltaTime) { MoveAndSlide(player, deltaTime); game.camera.target = - (Vector2) {fmax(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f}; + (Vector2) {fmaxf(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f}; } void AddPlayer(float xpos, float ypos) { @@ -39,13 +55,26 @@ void AddPlayer(float xpos, float ypos) { player.type = Player_Entity; player.position = (Vector2) {xpos, ypos}; - player.flags |= (ENTITY_PHYSICS_ACTIVE); + player.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE); - player.physicsCollider = (Rectangle) {0, 0, 100, 100}; + player.physicsCollider = physicsCollider; #ifdef BEATEMUP_DEBUG player.physicsColliderColor = RED; #endif - + + Asset* shadowTexture = GetMatchingAssetWithType("human-shadow", Texture_Asset); + + if (shadowTexture == NULL) { + TraceLog(LOG_ERROR, "Failed to find texture asset human-shadow, exitting!"); + exit(EXIT_FAILURE); + } + + AddSpriteToEntity(&player, (Sprite){ + .texture = shadowTexture->texture, + .layer = Foreground_Layer, + .destRect = shadowDestRect + }); + AddEntity(&player); } |