summary refs log tree commit diff stats
path: root/src/main.cpp
blob: 2d3cb5dfc6d15e8befb543b49081cf08efc4756b (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
108
109
110
111
112
113
114
115
#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"
#include "mirror_system.h"
#include "menu_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<MirrorSystem>();
  game.emplaceSystem<AnimationSystem>();
  game.emplaceSystem<CameraSystem>();
  game.emplaceSystem<MessageSystem>();
  game.emplaceSystem<EffectSystem>();
  game.emplaceSystem<MenuSystem>();

  game.loadMap("hallucination_cliff");

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

  int lucasSprite = game.emplaceSprite("lucas");
  game.getSystem<TransformSystem>().initSprite(lucasSprite, warpLoc);
  game.getSystem<TransformSystem>().setUpCollision(lucasSprite, {-7, -8}, {14, 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.getSprite(lucasSprite).sliding = 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<TransformSystem>().setUpCollision(kumaSprite, {-7, -8}, {14, 8}, false);
  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<TransformSystem>().setUpCollision(dusterSprite, {-7, -8}, {14, 8}, false);
  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<TransformSystem>().setUpCollision(boneySprite, {-7, -8}, {14, 8}, false);
  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;
}