aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: c50669c961205afdc4181957a6d3995a31cabcea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// src/main.c
#include "raylib.h"
#define RAYTMX_IMPLEMENTATION
#include "game.h"
#include "player.h"
#include "map_manager.h"
#include "entity.h"
#include "globals.h"

bool debugMode = false;

int main(void) {
  InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Dungeon Crawler");
  SetTargetFPS(280);

  GameScreen currentScreen = SCREEN_TITLE;

	MapManager mapMgr = LoadGameMap("assets/maps/debug.tmx");
  
  EntityManager entityMgr = InitEntityManager(100);
  if (mapMgr.map) {
    SpawnEntitiesFromMap(&entityMgr, mapMgr.map);
  }
  
  RaytmxExternalTileset elfTS = LoadTSX("assets/tilesets/elf.tsx");
	Player player = { 
    .position = { 200, 200}, 
    .speed = 120.0f, 
    .bounds = { 204, 204, 12, 12 },
    .tileset = elfTS.tileset,
    .currentFrame = 0,
    .frameTime = 0.0f,
    .state = 0, // idle
    .facingRight = true
  };

  Camera2D camera = { 0 };
  camera.offset.x = (float)SCREEN_WIDTH / 2.0f;
  camera.offset.y = (float)SCREEN_HEIGHT / 2.0f;
  camera.target = player.position;
  camera.zoom = 4.0f;

  while (!WindowShouldClose()) {
    if (IsKeyPressed(KEY_B)) debugMode = !debugMode;
    if (IsKeyPressed(KEY_R)) {
      // Reload map and entities
      UnloadGameMap(&mapMgr);
      UnloadEntityManager(&entityMgr);
      
      mapMgr = LoadGameMap("assets/maps/debug.tmx");
      entityMgr = InitEntityManager(100);
      if (mapMgr.map) {
        SpawnEntitiesFromMap(&entityMgr, mapMgr.map);
      }
      
      // Reset player position
      player.position = (Vector2){ 200, 200 };
      player.bounds = (Rectangle){ 204, 204, 12, 12 };
    }

    switch (currentScreen) {
    case SCREEN_TITLE:
      if (IsKeyPressed(KEY_ENTER))
        currentScreen = SCREEN_PLAYING;
      break;
    case SCREEN_PLAYING:
      if (IsKeyDown(KEY_ESCAPE))
        currentScreen = SCREEN_PAUSED;
			UpdatePlayer(&player, &mapMgr);
      UpdateEntities(&entityMgr, &player, &mapMgr);
			camera.target = player.position;
      break;
    case SCREEN_PAUSED:
      if (IsKeyDown(KEY_ESCAPE))
        currentScreen = SCREEN_PLAYING;
      break;
    case SCREEN_GAME_OVER:
      if (IsKeyDown(KEY_ENTER))
        currentScreen = SCREEN_PLAYING;
      break;
    };

    // --- DRAW ---
    BeginDrawing();
    
    switch (currentScreen) {
    case SCREEN_TITLE:
      ClearBackground(BLACK);
      DrawText("DUNGEON CRAWLER", 20, 20, 40, DARKGRAY);
      break;
    case SCREEN_PAUSED:
      ClearBackground(BLACK);
      DrawText("PAUSED", 20, 20, 40, DARKGRAY);
      break;
    case SCREEN_PLAYING:
      ClearBackground(BACKGROUND_COLOR);
      BeginMode2D(camera);
      {
        DrawMap(&mapMgr);
        DrawEntities(&entityMgr, mapMgr.map);
				DrawPlayer(&player);
      }
      EndMode2D();
      if (debugMode) DrawFPS(10, 10);
      break;
    case SCREEN_GAME_OVER:
      ClearBackground(BLACK);
      DrawText("GAME OVER", 20, 20, 40, RED);
      break;
    }

    EndDrawing();
  }

	UnloadGameMap(&mapMgr);
  UnloadEntityManager(&entityMgr);
  UnloadTexture(player.tileset.image.texture);
  CloseWindow();
  return 0;
}