// Copyright (C) 2021 - Sebastien Alaiwan
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.

#include "base/scene.h"
#include "base/util.h"

#include "gameplay/collision_groups.h"
#include "gameplay/entity.h"
#include "gameplay/entity_factory.h"
#include "gameplay/models.h"

namespace
{
template<int Direction>
struct Spikes : Entity
{
  Spikes(IEntityConfig* cfg)
  {
    size.x = cfg->getInt("width", 1);
    size.y = cfg->getInt("height", 1);
    size.y -= 0.05;
    solid = 1;
    collisionGroup = CG_WALLS;
    collidesWith = CG_SOLIDPLAYER;
    Body::onCollision = [this] (Body* other) { onCollide(other); };
  }

  void addActors(std::vector<SpriteActor> &) const override {};

  void addActors(std::vector<TileActor>& actors) const override
  {
    const Rect2f rect { pos, size };
    auto r = TileActor { rect, MDL_SPIKES };
    r.action = Direction;
    actors.push_back(r);
  }

  void onCollide(Body* other)
  {
    if(auto damageable = dynamic_cast<Damageable*>(other))
      damageable->onDamage(1000);
  }
};

DECLARE_ENTITY("spikes", Spikes<0>);
DECLARE_ENTITY("spikesdown", Spikes<1>);
}

