piece.cpp 4.53 KB
Newer Older
魔大农's avatar
魔大农 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Quatter
// Copyright (C) 2016 LucKey Productions (luckeyproductions.nl)
//
// 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"

魔大农's avatar
魔大农 committed
24
Piece::Piece(Attributes attributes): Object(MC->GetContext()),
25
    attributes_{attributes},
26
    state_{PieceState::FREE }
魔大农's avatar
魔大农 committed
27
{
魔大农's avatar
魔大农 committed
28
    rootNode_ = MC->world_.scene_->CreateChild("Piece"+GetCodon(4));
魔大农's avatar
魔大农 committed
29 30
    rootNode_->SetRotation(Quaternion(Random(360.0f), Vector3::UP));

魔大农's avatar
魔大农 committed
31
    StaticModel* pieceModel{rootNode_->CreateComponent<StaticModel>()};
32
    pieceModel->SetCastShadows(true);
魔大农's avatar
魔大农 committed
33
    pieceModel->SetModel(MC->GetModel("Piece_"+GetCodon(3)));
魔大农's avatar
魔大农 committed
34
    if (attributes[3]){
35
        pieceModel->SetMaterial(MC->GetMaterial("Wood_light"));
魔大农's avatar
魔大农 committed
36
    }
37
    else pieceModel->SetMaterial(MC->GetMaterial("Wood_dark"));
魔大农's avatar
魔大农 committed
38 39 40 41

    outlineModel_ = rootNode_->CreateComponent<StaticModel>();
    outlineModel_->SetCastShadows(false);
    outlineModel_->SetModel(MC->GetModel("Piece_"+GetCodon(2)+"_outline"));
42 43 44
    outlineModel_->SetMaterial(MC->GetMaterial("Glow")->Clone());
    outlineModel_->GetMaterial()->SetShaderParameter("MatDiffColor", Color(0.0f, 0.0f, 0.0f));
    outlineModel_->SetEnabled(false);
45 46 47 48 49 50 51

    Node* lightNode{rootNode_->CreateChild("Light")};
    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
52
}
魔大农's avatar
魔大农 committed
53 54
void Piece::Reset()
{
魔大农's avatar
魔大农 committed
55 56
    rootNode_->SetParent(MC->world_.scene_);

魔大农's avatar
魔大农 committed
57 58
    if (MC->GetSelectedPiece() == this){
        MC->DeselectPiece();
魔大农's avatar
魔大农 committed
59
    } else Deselect();
魔大农's avatar
魔大农 committed
60 61 62

    if (state_ != PieceState::FREE){
        state_ = PieceState::FREE;
魔大农's avatar
魔大农 committed
63 64 65 66
        FX->ArchTo(rootNode_,
                   MC->AttributesToPosition(ToInt()),
                   Quaternion(Random(360.0f), Vector3::UP),
                   attributes_[0] ? 2.0f : 1.3f + attributes_[1] ? 0.5f : 1.0f + Random(0.23f),
魔大农's avatar
魔大农 committed
67
                   RESET_DURATION, Random(0.42f) + 0.23f * (ToInt()/NUM_PIECES));
魔大农's avatar
魔大农 committed
68 69
    }
}
魔大农's avatar
魔大农 committed
70

魔大农's avatar
魔大农 committed
71 72
String Piece::GetCodon(int length) const
{
73
    if (length > static_cast<int>(attributes_.size()) || length < 1)
魔大农's avatar
魔大农 committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        length = static_cast<int>(attributes_.size());

    String codon{};

        codon += attributes_[0] ? "T" : "S"; //Tall  : Short
    if (length > 1)
        codon += attributes_[1] ? "R" : "S"; //Round : Square
    if (length > 2)
        codon += attributes_[2] ? "H" : "S"; //Hole  : Solid
    if (length == NUM_ATTRIBUTES)
        codon += attributes_[3] ? "L" : "D"; //Light : Dark

    return codon;
}

void Piece::Select()
{
魔大农's avatar
魔大农 committed
91 92
    if ((MC->GetGameState() == GameState::PLAYER1PICKS ||
         MC->GetGameState() == GameState::PLAYER2PICKS))
93 94 95 96
    {
        outlineModel_->SetEnabled(true);
        if (state_ == PieceState::FREE){
            state_ = PieceState::SELECTED;
魔大农's avatar
魔大农 committed
97 98 99
            FX->FadeTo(outlineModel_->GetMaterial(),
                                      COLOR_GLOW);
            FX->FadeTo(light_, 0.666f);
100 101
        }
    }
魔大农's avatar
魔大农 committed
102 103 104
}
void Piece::Deselect()
{
105 106
    if (state_ == PieceState::SELECTED){
        state_ = PieceState::FREE;
魔大农's avatar
魔大农 committed
107 108
        FX->FadeOut(outlineModel_->GetMaterial());
        FX->FadeOut(light_);
109 110 111 112 113 114
    }
}
void Piece::Pick()
{
    if (state_ == PieceState::SELECTED){
        state_ = PieceState::PICKED;
魔大农's avatar
魔大农 committed
115
        if (MC->GetGameState() == GameState::PLAYER1PICKS)
116
            rootNode_->SetParent(CAMERA->GetPocket(false));
魔大农's avatar
魔大农 committed
117
        if (MC->GetGameState() == GameState::PLAYER2PICKS)
118 119
            rootNode_->SetParent(CAMERA->GetPocket(true));

魔大农's avatar
魔大农 committed
120
        FX->ArchTo(rootNode_, Vector3::DOWN, Quaternion(10.0f, Vector3(1.0f, 0.0f, 0.5f)), 1.0f, 0.8f);
121

魔大农's avatar
魔大农 committed
122 123
        FX->FadeOut(outlineModel_->GetMaterial());
        FX->FadeOut(light_);
124 125 126 127 128 129 130
    }
}
void Piece::Put(Vector3 position)
{
    if (state_ == PieceState::PICKED){
        state_ = PieceState::PUT;

魔大农's avatar
魔大农 committed
131
        rootNode_->SetParent(MC->world_.scene_);
132

魔大农's avatar
魔大农 committed
133
        FX->ArchTo(rootNode_, position, Quaternion(Random(-13.0f, 13.0f), Vector3::UP), 2.3f, 0.42f);
134
    }
魔大农's avatar
魔大农 committed
135
}