summary refs log tree commit diff stats
path: root/src/game.cpp
blob: d1e5b8e847b4039fef488239ff49d88b8db48b8b (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
#include "game.h"
#include "transform_system.h"
#include "animation_system.h"
#include "character_system.h"
#include "camera_system.h"

int Game::emplaceSprite(std::string alias) {
  int id = sprites_.size();
  sprites_.emplace_back();
  spriteIds_.push_back(id);
  spritesByAlias_[alias] = id;
  return id;
}

void Game::clearSprites() {
  for (std::unique_ptr<System>& system : systems_) {
    system->clearSpriteCache();
  }

  sprites_.clear();
  spriteIds_.clear();
  spritesByAlias_.clear();
}

void Game::loadMap(std::string filename, std::string warpPoint, Direction dir) {
  clearSprites();

  map_ = std::make_unique<Map>(filename);

  vec2i warpLoc = map_->getWarpPoint(warpPoint);

  int lucasSprite = emplaceSprite("lucas");
  getSystem<TransformSystem>().initSprite(lucasSprite, warpLoc);
  getSystem<TransformSystem>().setUpCollision(lucasSprite, {-8, -8}, {12, 8}, true);
  getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt");
  getSystem<AnimationSystem>().setSpriteDirection(lucasSprite, dir);
  getSprite(lucasSprite).hasShadow = true;
  getSprite(lucasSprite).player = true;
  getSystem<CharacterSystem>().initSprite(lucasSprite);

  int kumaSprite = emplaceSprite("kuma");
  getSystem<TransformSystem>().initSprite(kumaSprite, warpLoc);
  getSystem<AnimationSystem>().initSprite(kumaSprite, "../res/sprites/kuma_anim.txt");
  getSystem<AnimationSystem>().setSpriteDirection(kumaSprite, dir);
  getSprite(kumaSprite).hasShadow = true;
  getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, kumaSprite);

  int dusterSprite = emplaceSprite("duster");
  getSystem<TransformSystem>().initSprite(dusterSprite, warpLoc);
  getSystem<AnimationSystem>().initSprite(dusterSprite, "../res/sprites/duster_anim.txt");
  getSystem<AnimationSystem>().setSpriteDirection(dusterSprite, dir);
  getSprite(dusterSprite).hasShadow = true;
  getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, dusterSprite);

  int boneySprite = emplaceSprite("boney");
  getSystem<TransformSystem>().initSprite(boneySprite, warpLoc);
  getSystem<AnimationSystem>().initSprite(boneySprite, "../res/sprites/boney_anim.txt");
  getSystem<AnimationSystem>().setSpriteDirection(boneySprite, dir);
  getSprite(boneySprite).hasShadow = true;
  getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, boneySprite);

  for (const Prototype& p : map_->getPrototypes()) {
    int spriteId = emplaceSprite(p.name);
    getSystem<TransformSystem>().initSprite(spriteId, p.pos);
    getSystem<TransformSystem>().setUpCollision(spriteId, p.collisionOffset, p.collisionSize, true);
    if (!p.animationFilename.empty()) {
      getSystem<AnimationSystem>().initSprite(spriteId, p.animationFilename);
      getSprite(spriteId).hasShadow = p.shadow;
    }
    getSprite(spriteId).interactionScript = p.interactionScript;
  }

  for (const Trigger& t : map_->getTriggers()) {
    int spriteId = emplaceSprite(t.name);
    getSystem<TransformSystem>().initSprite(spriteId, t.pos);
    getSystem<TransformSystem>().setUpCollision(spriteId, {0, 0}, t.size, false);
    getSprite(spriteId).walkthroughScript = t.script;
  }

  getSystem<CameraSystem>().setFollowingSprite(lucasSprite);
  getSystem<CameraSystem>().unlockCamera();
}