summary refs log tree commit diff stats
path: root/src/systems/playing.cpp
blob: 66520993ce3a4c10908d33302c7a2cd905f909b8 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "playing.h"
#include "game.h"
#include "components/transformable.h"
#include "components/animatable.h"
#include "components/playable.h"
#include "components/controllable.h"
#include "components/orientable.h"
#include "systems/mapping.h"
#include "systems/pondering.h"
#include "systems/orienting.h"
#include "systems/scheduling.h"
#include "systems/controlling.h"
#include "systems/animating.h"
#include "systems/realizing.h"
#include "animation.h"
#include "muxer.h"

void PlayingSystem::initPlayer()
{
  id_type player = game_.getEntityManager().emplaceEntity();

  AnimationSet playerGraphics {"res/Starla.png", 10, 12, 6};
  playerGraphics.emplaceAnimation("stillLeft", 3, 1, 1);
  playerGraphics.emplaceAnimation("stillRight", 0, 1, 1);
  playerGraphics.emplaceAnimation("walkingLeft", 4, 2, 10);
  playerGraphics.emplaceAnimation("walkingRight", 1, 2, 10);

  game_.getEntityManager().emplaceComponent<AnimatableComponent>(
    player,
    std::move(playerGraphics));

  game_.getSystemManager().getSystem<AnimatingSystem>().startAnimation(
    player,
    "stillLeft");

  auto& realizing = game_.getSystemManager().getSystem<RealizingSystem>();

  auto& transformable = game_.getEntityManager().
    emplaceComponent<TransformableComponent>(player);

  transformable.pos = realizing.getStartingPos();
  transformable.size.w() = 10;
  transformable.size.h() = 12;

  game_.getSystemManager().getSystem<PonderingSystem>().initializeBody(
    player,
    PonderableComponent::Type::freefalling);

  game_.getEntityManager().emplaceComponent<ControllableComponent>(player);
  game_.getEntityManager().emplaceComponent<OrientableComponent>(player);

  auto& playable = game_.getEntityManager().
    emplaceComponent<PlayableComponent>(player);

  playable.mapId = realizing.getActiveMap();
  playable.checkpointMapId = realizing.getStartingMapId();
  playable.checkpointPos = realizing.getStartingPos();

  realizing.enterActiveMap(player);

  realizing.setActivePlayer(player);
}

void PlayingSystem::changeMap(
  id_type player,
  size_t mapId,
  vec2d warpPos)
{
  auto& playable = game_.getEntityManager().
    getComponent<PlayableComponent>(player);

  auto& transformable = game_.getEntityManager().
    getComponent<TransformableComponent>(player);

  auto& pondering = game_.getSystemManager().getSystem<PonderingSystem>();
  auto& realizing = game_.getSystemManager().getSystem<RealizingSystem>();

  id_type newMapEntity = realizing.getEntityByMapId(mapId);

  if (playable.mapId != newMapEntity)
  {
    if (playable.mapId == realizing.getActiveMap())
    {
      realizing.leaveActiveMap(player);
    } else if (newMapEntity == realizing.getActiveMap())
    {
      realizing.enterActiveMap(player);
    }

    playable.mapId = newMapEntity;
  }

  pondering.unferry(player);

  transformable.pos = warpPos;

  if (realizing.getActivePlayer() == player)
  {
    realizing.loadMap(newMapEntity);
  }
}

void PlayingSystem::die(id_type player)
{
  playSound("res/Hit_Hurt5.wav", 0.25);

  auto& animatable = game_.getEntityManager().
    getComponent<AnimatableComponent>(player);

  auto& ponderable = game_.getEntityManager().
    getComponent<PonderableComponent>(player);

  auto& controlling = game_.getSystemManager().getSystem<ControllingSystem>();
  controlling.freeze(player);

  animatable.frozen = true;
  animatable.flickering = true;
  ponderable.frozen = true;
  ponderable.collidable = false;

  auto& scheduling = game_.getSystemManager().getSystem<SchedulingSystem>();

  scheduling.schedule(player, 0.75, [&] (id_type player) {
    auto& playable = game_.getEntityManager().
      getComponent<PlayableComponent>(player);

    changeMap(
      player,
      playable.checkpointMapId,
      playable.checkpointPos);

    animatable.frozen = false;
    animatable.flickering = false;
    ponderable.frozen = false;
    ponderable.collidable = true;

    // Reset the walk state, and then potentially let the
    // ControllingSystem set it again.
    auto& orienting = game_.getSystemManager().getSystem<OrientingSystem>();
    orienting.stopWalking(player);

    controlling.unfreeze(player);
  });
}