summary refs log tree commit diff stats
path: root/src/main.cpp
blob: d0220fc7c45d9c7a06339318b9ff89cf5ad375ac (plain) (blame)
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
#include <iostream>
#include <memory>
#include <random>
#include "renderer.h"
#include "game.h"
#include "timer.h"
#include "map.h"
#include "transform_system.h"
#include "camera_system.h"
#include "animation_system.h"
#include "character_system.h"
#include "input_system.h"
#include "message_system.h"
#include "script_system.h"
#include "effect_system.h"
#include "behaviour_system.h"

void loop(Renderer& renderer, std::mt19937& rng) {
  Game game(renderer, rng);
  game.emplaceSystem<ScriptSystem>();
  game.emplaceSystem<TransformSystem>();
  game.emplaceSystem<InputSystem>();
  game.emplaceSystem<BehaviourSystem>();
  game.emplaceSystem<CharacterSystem>();
  game.emplaceSystem<AnimationSystem>();
  game.emplaceSystem<CameraSystem>();
  game.emplaceSystem<MessageSystem>();
  game.emplaceSystem<EffectSystem>();

  game.loadMap("pink_shell");

  vec2i warpLoc = game.getMap().getWarpPoint("fromOutside");

  int lucasSprite = game.emplaceSprite("lucas");
  game.getSystem<TransformSystem>().initSprite(lucasSprite, warpLoc);
  game.getSystem<TransformSystem>().setUpCollision(lucasSprite, {-8, -8}, {12, 8}, true);
  game.getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt");
  game.getSystem<AnimationSystem>().setSpriteDirection(lucasSprite, Direction::down);
  game.getSprite(lucasSprite).normallyHasShadow = true;
  game.getSprite(lucasSprite).player = true;
  game.getSprite(lucasSprite).controllable = true;
  game.getSprite(lucasSprite).persistent = true;
  game.getSystem<CharacterSystem>().initSprite(lucasSprite, LUCAS_MOVEMENT_SPEED);
  game.getSystem<CameraSystem>().setFollowingSprite(lucasSprite);
  game.getSystem<CameraSystem>().unlockCamera();

  int kumaSprite = game.emplaceSprite("kuma");
  game.getSystem<TransformSystem>().initSprite(kumaSprite, warpLoc);
  game.getSystem<AnimationSystem>().initSprite(kumaSprite, "../res/sprites/kuma_anim.txt");
  game.getSystem<AnimationSystem>().setSpriteDirection(kumaSprite, Direction::down);
  game.getSprite(kumaSprite).normallyHasShadow = true;
  game.getSprite(kumaSprite).persistent = true;
  game.getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, kumaSprite);

  int dusterSprite = game.emplaceSprite("duster");
  game.getSystem<TransformSystem>().initSprite(dusterSprite, warpLoc);
  game.getSystem<AnimationSystem>().initSprite(dusterSprite, "../res/sprites/duster_anim.txt");
  game.getSystem<AnimationSystem>().setSpriteDirection(dusterSprite, Direction::down);
  game.getSprite(dusterSprite).normallyHasShadow = true;
  game.getSprite(dusterSprite).persistent = true;
  game.getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, dusterSprite);

  int boneySprite = game.emplaceSprite("boney");
  game.getSystem<TransformSystem>().initSprite(boneySprite, warpLoc);
  game.getSystem<AnimationSystem>().initSprite(boneySprite, "../res/sprites/boney_anim.txt");
  game.getSystem<AnimationSystem>().setSpriteDirection(boneySprite, Direction::down);
  game.getSprite(boneySprite).normallyHasShadow = true;
  game.getSprite(boneySprite).persistent = true;
  game.getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, boneySprite);

  renderer.render(game);

  if (game.getMap().hasMusic()) {
    game.getMixer().playMusic(game.getMap().getMusic());
  }

  size_t lastTime = SDL_GetTicks();

  while (!game.shouldQuit()) {
    size_t currentTime = SDL_GetTicks();
    size_t frameTime = currentTime - lastTime;
    lastTime = currentTime;

    for (System& system : game.systems()) {
      system.tick(frameTime);
    }

    renderer.render(game);
  }
}

int main(int, char**) {
  std::random_device randomEngine;
  std::mt19937 rng(randomEngine());

  try
  {
    Renderer renderer;

    loop(renderer, rng);
  } catch (const sdl_error& ex)
  {
    std::cout << "SDL error (" << ex.what() << ")" << std::endl;
  }

  return 0;
}