piece.cpp 4.88 KB
Newer Older
魔大农's avatar
魔大农 committed
1
/* Quatter
魔大农's avatar
魔大农 committed
2
// Copyright (C) 2017 LucKey Productions (luckeyproductions.nl)
魔大农's avatar
魔大农 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

19
#include "quattercam.h"
20
#include "effectmaster.h"
21
#include "board.h"
魔大农's avatar
魔大农 committed
22 23
#include "piece.h"

24 25 26 27 28 29 30
void Piece::RegisterObject(Context *context)
{
    context->RegisterFactory<Piece>();
}

Piece::Piece(Context* context): LogicComponent(context),
    attributes_{},
魔大农's avatar
魔大农 committed
31
    state_{PieceState::FREE}
魔大农's avatar
魔大农 committed
32
{
33
}
魔大农's avatar
魔大农 committed
34

35
void Piece::OnNodeSet(Node* node)
魔大农's avatar
魔大农 committed
36
{ if (!node) return;
37 38

    node_->SetRotation(Quaternion(Random(360.0f), Vector3::UP));
魔大农's avatar
魔大农 committed
39
    node_->AddTag("Piece");
魔大农's avatar
魔大农 committed
40

41 42
    pieceModel_ = node_->CreateComponent<StaticModel>();
    pieceModel_->SetCastShadows(true);
魔大农's avatar
魔大农 committed
43

44
    outlineModel_ = node_->CreateComponent<StaticModel>();
魔大农's avatar
魔大农 committed
45
    outlineModel_->SetCastShadows(false);
46

魔大农's avatar
魔大农 committed
47
    Node* lightNode{ node_->CreateChild("Light", LOCAL) };
48 49 50 51 52
    lightNode->SetPosition(Vector3::UP * 0.5f);
    light_ = lightNode->CreateComponent<Light>();
    light_->SetColor(Color(0.0f, 0.8f, 0.5f));
    light_->SetBrightness(0.0f);
    light_->SetRange(3.0f);
魔大农's avatar
魔大农 committed
53
}
54 55 56 57 58

void Piece::Init(PieceAttributes attributes)
{
    attributes_ = attributes;
    pieceModel_->SetModel(MC->GetModel("Piece_" + GetCodon(3)));
魔大农's avatar
魔大农 committed
59 60 61

    if (attributes[3]) {

魔大农's avatar
魔大农 committed
62
        pieceModel_->SetMaterial(MC->GetMaterial("WoodLight"));
魔大农's avatar
魔大农 committed
63 64 65

    } else {

魔大农's avatar
魔大农 committed
66
        pieceModel_->SetMaterial(MC->GetMaterial("WoodDark"));
67 68 69 70 71 72 73 74
    }

    outlineModel_->SetModel(MC->GetModel("Piece_"+GetCodon(2)+"_outline"));
    outlineModel_->SetMaterial(MC->GetMaterial("Glow")->Clone());
    outlineModel_->GetMaterial()->SetShaderParameter("MatDiffColor", Color(0.0f, 0.0f, 0.0f));
    outlineModel_->SetEnabled(false);
}

魔大农's avatar
魔大农 committed
75 76
void Piece::Reset()
{
77
    node_->SetParent(MC->world_.scene_);
魔大农's avatar
魔大农 committed
78

79
    if (MC->GetSelectedPiece() == this)
魔大农's avatar
魔大农 committed
80
        MC->DeselectPiece();
81 82
    if (MC->GetPickedPiece() == this)
        MC->SetPickedPiece(nullptr);
魔大农's avatar
魔大农 committed
83 84 85

    if (state_ != PieceState::FREE){
        state_ = PieceState::FREE;
86
        FX->ArchTo(node_,
魔大农's avatar
魔大农 committed
87 88 89
                   MC->AttributesToPosition(ToInt()),
                   Quaternion(Random(360.0f), Vector3::UP),
                   attributes_[0] ? 2.0f : 1.3f + attributes_[1] ? 0.5f : 1.0f + Random(0.23f),
90 91
                   RESET_DURATION,
                   Random(0.42f) + 0.23f * (ToInt()/NUM_PIECES));
魔大农's avatar
魔大农 committed
92 93
    }
}
魔大农's avatar
魔大农 committed
94

魔大农's avatar
魔大农 committed
95 96
String Piece::GetCodon(int length) const
{
97
    if (length > static_cast<int>(attributes_.size()) || length < 1)
魔大农's avatar
魔大农 committed
98 99 100 101
        length = static_cast<int>(attributes_.size());

    String codon{};

魔大农's avatar
魔大农 committed
102
        codon += attributes_[0] ? "T" : "S"; //Tall  : Short
魔大农's avatar
魔大农 committed
103
    if (length > 1)
魔大农's avatar
魔大农 committed
104
        codon += attributes_[1] ? "R" : "S"; //Round : Square
魔大农's avatar
魔大农 committed
105
    if (length > 2)
魔大农's avatar
魔大农 committed
106
        codon += attributes_[2] ? "H" : "S"; //Hole  : Solid
魔大农's avatar
魔大农 committed
107
    if (length == NUM_ATTRIBUTES)
魔大农's avatar
魔大农 committed
108
        codon += attributes_[3] ? "L" : "D"; //Light : Dark
魔大农's avatar
魔大农 committed
109 110 111 112

    return codon;
}

113 114 115 116 117
float Piece::GetAngle() const
{
    return MC->AttributesToAngle(ToInt());
}

魔大农's avatar
魔大农 committed
118 119
void Piece::Select()
{
魔大农's avatar
魔大农 committed
120 121
    if ((MC->GetGameState() == GameState::PLAYER1PICKS ||
         MC->GetGameState() == GameState::PLAYER2PICKS))
122 123
    {
        outlineModel_->SetEnabled(true);
魔大农's avatar
魔大农 committed
124 125 126

        if (state_ == PieceState::FREE) {

127
            state_ = PieceState::SELECTED;
魔大农's avatar
魔大农 committed
128
            FX->FadeTo(outlineModel_->GetMaterial(), COLOR_GLOW);
魔大农's avatar
魔大农 committed
129
            FX->FadeTo(light_, 0.666f);
130 131
        }
    }
魔大农's avatar
魔大农 committed
132 133 134
}
void Piece::Deselect()
{
魔大农's avatar
魔大农 committed
135
    if (state_ == PieceState::SELECTED) {
魔大农's avatar
魔大农 committed
136

137
        state_ = PieceState::FREE;
魔大农's avatar
魔大农 committed
138 139
        FX->FadeOut(outlineModel_->GetMaterial());
        FX->FadeOut(light_);
140 141
    }
}
魔大农's avatar
魔大农 committed
142

143 144
void Piece::Pick()
{
魔大农's avatar
魔大农 committed
145
    if (state_ != PieceState::PUT) {
魔大农's avatar
魔大农 committed
146

147
        state_ = PieceState::PICKED;
148
        MC->SetPickedPiece(this);
魔大农's avatar
魔大农 committed
149

魔大农's avatar
魔大农 committed
150
        if (MC->GetGameState() == GameState::PLAYER1PICKS)
151
            node_->SetParent(CAMERA->GetPocket(false));
魔大农's avatar
魔大农 committed
152
        if (MC->GetGameState() == GameState::PLAYER2PICKS)
153
            node_->SetParent(CAMERA->GetPocket(true));
154

155
        FX->ArchTo(node_, Vector3::DOWN, Quaternion(10.0f, Vector3(1.0f, 0.0f, 0.5f)), 1.0f, 0.8f);
156

魔大农's avatar
魔大农 committed
157 158
        FX->FadeOut(outlineModel_->GetMaterial());
        FX->FadeOut(light_);
魔大农's avatar
魔大农 committed
159 160

        MC->NextPhase();
161 162 163 164
    }
}
void Piece::Put(Vector3 position)
{
魔大农's avatar
魔大农 committed
165
    if (state_ == PieceState::PICKED) {
166

魔大农's avatar
魔大农 committed
167
        state_ = PieceState::PUT;
168
        MC->SetPickedPiece(nullptr);
169 170
        node_->SetParent(MC->world_.scene_);
        FX->ArchTo(node_, position, Quaternion(Random(-13.0f, 13.0f), Vector3::UP), 2.3f, 0.5f);
171
    }
魔大农's avatar
魔大农 committed
172
}