Commit 98f1256f authored by 魔大农's avatar 魔大农 🍀

Added EffectMaster for even more convenient fading of lights and materials

parent ee7f565f
No preview for this file type
......@@ -23,7 +23,8 @@ SOURCES += \
quattercam.cpp \
board.cpp \
piece.cpp \
master.cpp
master.cpp \
effectmaster.cpp
HEADERS += \
luckey.h \
......@@ -32,4 +33,5 @@ HEADERS += \
quattercam.h \
board.h \
piece.h \
master.h
master.h \
effectmaster.h
This diff is collapsed.
......@@ -3,8 +3,8 @@
<technique name="Techniques/NoTexture.xml" quality="0" loddistance="0" />
<parameter name="UOffset" value="1 0 0 0" />
<parameter name="VOffset" value="0 1 0 0" />
<parameter name="MatDiffColor" value="0.75 0.62 0.42 1" />
<parameter name="MatDiffColor" value="0.75 0.62 0.5 1" />
<parameter name="MatEmissiveColor" value="0 0 0" />
<parameter name="MatEnvMapColor" value="0.4 0.5 0.4" />
<parameter name="MatSpecColor" value="0.5 0.5 0.5 5" />
<parameter name="MatSpecColor" value="0.3 0.3 0.3 5" />
</material>
This diff is collapsed.
......@@ -20,6 +20,7 @@ public:
Square() : Object(MC->GetContext()) {}
IntVector2 coords_;
SharedPtr<Node> node_;
SharedPtr<AnimatedModel> slot_;
SharedPtr<Light> light_;
Piece* piece_;
bool free_;
......@@ -31,19 +32,28 @@ class Board : public Object
URHO3D_OBJECT(Board, Object);
public:
Board();
Node* GetRootNode() const { return rootNode_; }
float GetThickness() const { return model_->GetBoundingBox().Size().y_; }
bool CheckQuatter();
void PutPiece(Piece* piece, IntVector2 coords);
void PutPiece(Piece* piece) { PutPiece(piece, IntVector2{Random(BOARD_HEIGHT), Random(BOARD_HEIGHT)}); }//GetSelectedSquare().coords_); }
Node* GetRootNode() const { return rootNode_; }
void PutPiece(Piece* piece, Square* square);
void PutPiece(Piece* piece) { PutPiece(piece, GetSelectedSquare()); }
Square*GetNearestFreeSquare(Vector3 pos);
Square* GetSelectedSquare() const { return selectedSquare_; }
void Select(Square* square);
void SelectNearestFreeSquare(Vector3 pos);
void DeselectAll();
private:
SharedPtr<Node> rootNode_;
StaticModel* model_;
HashMap<IntVector2, SharedPtr<Square>> squares_;
Square* selectedSquare_;
Vector3 SquarePosition(IntVector2 coords);
Square* GetSelectedSquare();
void Deselect(Square* square);
void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
};
#endif // BOARD_H
#include "effectmaster.h"
using namespace Urho3D;
EffectMaster::EffectMaster() : Master()
{
}
void EffectMaster::FadeTo(Material* material, Color color, float duration)
{
Color startColor{material->GetShaderParameter("MatDiffColor").GetColor()};
ValueAnimation* fade{new ValueAnimation(context_)};
fade->SetKeyFrame(0.0f, startColor);
fade->SetKeyFrame(duration, color);
material->SetShaderParameterAnimation("MatDiffColor", fade, WM_ONCE);
}
void EffectMaster::FadeTo(Light* light, float brightness, float duration)
{
ValueAnimation* fade{new ValueAnimation(context_)};
fade->SetKeyFrame(0.0f, light->GetBrightness());
fade->SetKeyFrame(duration, brightness);
light->SetAttributeAnimation("Brightness Multiplier", fade, WM_ONCE);
}
#ifndef EFFECTMASTER_H
#define EFFECTMASTER_H
#include <Urho3D/Urho3D.h>
#include "master.h"
class EffectMaster : public Master
{
URHO3D_OBJECT(EffectMaster, Master);
public:
EffectMaster();
void FadeOut(Material* material) {
FadeTo(material, material->GetShaderParameter("MatDiffColor").GetColor() * 0.0f);
}
void FadeOut(Light* light) { FadeTo(light, 0.0f); }
void FadeTo(Material* material, Color color, float duration = 0.23f);
void FadeTo(Light* light, float brightness, float duration = 0.23f);
};
#endif // EFFECTMASTER_H
......@@ -100,12 +100,17 @@ void InputMaster::HandleActionButtonPressed()
if (selectedPiece){
selectedPiece->Pick();
MC->NextPhase();
MC->world.board_->SelectNearestFreeSquare(CAMERA->GetPosition());
}
} else if (MC->GetGamePhase() == GamePhase::PLAYER1PUTS ||
MC->GetGamePhase() == GamePhase::PLAYER2PUTS )
{
MC->world.board_->PutPiece(MC->GetPickedPiece());
MC->NextPhase();
Square* selectedSquare{MC->world.board_->GetSelectedSquare()};
if (selectedSquare && selectedSquare->free_){
MC->world.board_->PutPiece(MC->GetPickedPiece());
if (MC->GetGamePhase() != GamePhase::QUATTER)
MC->NextPhase();
}
}
}
......@@ -139,9 +144,9 @@ void InputMaster::HandleCameraMovement(float t)
} break;
case KEY_S:{ camRot += keyRotMultiplier * Vector2::DOWN;
} break;
case KEY_Q:{ camZoom += keyZoomSpeed;
case KEY_Q:{ camZoom -= keyZoomSpeed;
} break;
case KEY_E:{ camZoom += -keyZoomSpeed;
case KEY_E:{ camZoom += keyZoomSpeed;
} break;
default: break;
}
......@@ -161,7 +166,7 @@ void InputMaster::HandleCameraMovement(float t)
idleTime_ = 0.0f;
camRot += rotation * t * joyRotMultiplier;
}
camZoom += t * joyZoomSpeed * (joy0->GetAxisPosition(12) - joy0->GetAxisPosition(13));
camZoom += t * joyZoomSpeed * (joy0->GetAxisPosition(13) - joy0->GetAxisPosition(12));
}
float idleThreshold{5.0f};
......@@ -202,8 +207,8 @@ void InputMaster::SmoothCameraMovement(float camZoom, Vector2 camRot)
if (pitchLeft < PITCH_EDGE)
pitchBrake = pitchLeft / PITCH_EDGE;
}
camRot.y_ *= pitchBrake;
smoothCamRotate_.y_ *= pitchBrake;
camRot.y_ *= pitchBrake * pitchBrake;
// smoothCamRotate_.y_ *= pitchBrake;
//Slow down zooming when nearing extremes
float zoomBrake{1.0f};
if (Sign(camZoom) < 0.0f){
......@@ -216,7 +221,7 @@ void InputMaster::SmoothCameraMovement(float camZoom, Vector2 camRot)
zoomBrake = zoomLeft / ZOOM_EDGE;
}
camZoom *= zoomBrake;
smoothCamZoom_ *= zoomBrake;
// smoothCamZoom_ *= zoomBrake;
smoothCamRotate_ = 0.0666f * (camRot + smoothCamRotate_ * 14.0f);
smoothCamZoom_ = 0.05f * (camZoom + smoothCamZoom_ * 19.0f);
......
......@@ -15,7 +15,6 @@ public:
bool IsIdle() const noexcept { return idle_; }
void HandleActionButtonPressed();
private:
Input* input_;
......@@ -26,7 +25,7 @@ private:
HashSet<int> pressedKeys_;
HashSet<int> pressedMouseButtons_;
HashMap< int, HashSet<int> > pressedJoystickButtons_;
HashMap<int, HashSet<int>> pressedJoystickButtons_;
void HandleKeyDown(StringHash eventType, VariantMap &eventData);
void HandleKeyUp(StringHash eventType, VariantMap &eventData);
......
#include "mastercontrol.h"
#include "inputmaster.h"
#include "effectmaster.h"
#include "quattercam.h"
#include "board.h"
#include <Urho3D/Graphics/Texture2D.h>
......@@ -30,13 +31,14 @@ void MasterControl::Setup()
engineParameters_["ResourcePaths"] = "Data;CoreData;Resources";
engineParameters_["WindowIcon"] = "icon.png";
// engineParameters_["FullScreen"] = false;
engineParameters_["FullScreen"] = false;
// engineParameters_["WindowWidth"] = 960;
// engineParameters_["WindowHeight"] = 540;
}
void MasterControl::Start()
{
inputMaster_ = new InputMaster();
effectMaster_ = new EffectMaster();
cache_ = GetSubsystem<ResourceCache>();
CreateScene();
......@@ -94,51 +96,56 @@ void MasterControl::CreateScene()
for (int p{0}; p < NUM_PIECES; ++p){
Piece* newPiece = new Piece(Piece::Attributes(p));
world.pieces_.Push(SharedPtr<Piece>(newPiece));
newPiece->SetPosition(AttributesToPosition(p) + Vector3(Random(0.23f), 0.0f, Random(0.23f)));
newPiece->SetPosition(AttributesToPosition(p) + Vector3(Random(0.05f), 0.0f, Random(0.05f)));
}
}
void MasterControl::CreateLights()
{
//Add main light source
leafyLightNode_ = world.scene->CreateChild("DirectionalLight");
leafyLightNode_->SetPosition(Vector3(6.0f, 96.0f, 9.0f));
leafyLightNode_->LookAt(Vector3(0.0f, 0.0f, 0.0f));
leafyLight_ = leafyLightNode_->CreateComponent<Light>();
leafyLight_->SetLightType(LIGHT_SPOT);
leafyLight_->SetRange(180.0f);
leafyLight_->SetFov(34.0f);
leafyLight_->SetCastShadows(false);
leafyLight_->SetShadowIntensity(0.23f);
leafyLight_->SetShapeTexture(static_cast<Texture*>(cache_->GetResource<Texture2D>("Textures/LeafyMask.png")));
leafyLight_->SetShadowBias(BiasParameters(0.000025f, 0.5f));
leafyLight_->SetShadowCascade(CascadeParameters(5.0f, 7.0f, 23.0f, 42.0f, 0.8f));
// leafyLight_->SetShadowCascade(CascadeParameters(64.0f, 86.0f, 128.0f, 192.0f, 0.8f));
// leafyLight_->SetShadowCascade(CascadeParameters(5.0f, 7.0f, 23.0f, 42.0f, 0.8f));
//Add a directional light to the world. Enable cascaded shadows on it
Node* downardsLightNode{world.scene->CreateChild("DirectionalLight")};
downardsLightNode->SetPosition(Vector3(2.0f, 23.0f, 3.0f));
downardsLightNode->LookAt(Vector3(0.0f, 0.0f, 0.0f));
Light* downwardsLight{downardsLightNode->CreateComponent<Light>()};
downwardsLight->SetLightType(LIGHT_DIRECTIONAL);
downwardsLight->SetBrightness(0.5f);
downwardsLight->SetBrightness(0.34f);
downwardsLight->SetColor(Color(0.8f, 0.9f, 0.95f));
downwardsLight->SetCastShadows(true);
downwardsLight->SetShadowIntensity(0.23f);
// downwardsLight->SetShadowIntensity(0.23f);
downwardsLight->SetShadowBias(BiasParameters(0.000025f, 0.5f));
downwardsLight->SetShadowCascade(CascadeParameters(5.0f, 7.0f, 23.0f, 42.0f, 0.8f));
//Add a directional light to the world. Enable cascaded shadows on it
leafyLightNode_ = world.scene->CreateChild("DirectionalLight");
leafyLightNode_->SetPosition(Vector3(6.0f, 96.0f, 9.0f));
leafyLightNode_->LookAt(Vector3(0.0f, 0.0f, 0.0f));
leafyLight_ = leafyLightNode_->CreateComponent<Light>();
leafyLight_->SetLightType(LIGHT_SPOT);
// leafyLight_->SetBrightness(1.0f);
leafyLight_->SetRange(180.0f);
leafyLight_->SetFov(34.0f);
leafyLight_->SetCastShadows(true);
leafyLight_->SetShapeTexture(static_cast<Texture*>(cache_->GetResource<Texture2D>("Textures/LeafyMask.png")));
leafyLight_->SetShadowBias(BiasParameters(0.0000025f, 0.5f));
leafyLight_->SetShadowCascade(CascadeParameters(5.0f, 7.0f, 23.0f, 42.0f, 0.8f));
leafyLight_->SetShadowCascade(CascadeParameters(64.0f, 86.0f, 128.0f, 192.0f, 0.8f));
//Create a point light.
Node* pointLightNode_{world.scene->CreateChild("PointLight")};
pointLightNode_->SetPosition(Vector3(-10.0f, 5.0f, -23.0f));
Light* pointLight{pointLightNode_->CreateComponent<Light>()};
pointLight->SetLightType(LIGHT_POINT);
pointLight->SetBrightness(0.42f);
pointLight->SetRange(42.0f);
pointLight->SetColor(Color(0.75f, 1.0f, 0.75f));
pointLight->SetCastShadows(true);
pointLight->SetShadowIntensity(0.8f);
//Create a point lights.
for (Vector3 pos : {Vector3(-10.0f, 8.0f, -23.0f), Vector3(-20.0f, -8.0f, 23.0f), Vector3(20.0f, -7.0f, 23.0f)}){
Node* pointLightNode_{world.scene->CreateChild("PointLight")};
pointLightNode_->SetPosition(pos);
Light* pointLight{pointLightNode_->CreateComponent<Light>()};
pointLight->SetLightType(LIGHT_POINT);
pointLight->SetBrightness(0.42f);
pointLight->SetRange(42.0f);
pointLight->SetColor(Color(0.75f, 1.0f, 0.75f));
pointLight->SetCastShadows(true);
pointLight->SetShadowResolution(0.25f);
pointLight->SetShadowIntensity(0.6f);
}
}
Piece* MasterControl::GetSelectedPiece() const
......@@ -186,7 +193,7 @@ void MasterControl::HandleUpdate(StringHash eventType, VariantMap& eventData)
//Wave leafy light
leafyLightNode_->SetRotation(Quaternion(Sine(Sine(0.1f, 0.05f, 0.23f), -0.23f, 0.23f) + 90.0f, Vector3::RIGHT) *
Quaternion(Sine(0.23f, 178.0f, 182.0f), Vector3::FORWARD));
leafyLight_->SetBrightness(Sine(0.011f, 0.05f, 0.5f) + Sine(0.02f, 0.13f, 0.34f));
leafyLight_->SetBrightness(0.34f + Sine(0.011f, 0.05f, 0.23f) + Sine(0.02f, 0.05f, 0.13f));
}
void MasterControl::NextPhase()
......@@ -194,14 +201,18 @@ void MasterControl::NextPhase()
switch (gamePhase_) {
case GamePhase::PLAYER1PICKS: gamePhase_ = GamePhase::PLAYER2PUTS;
break;
case GamePhase::PLAYER2PUTS: gamePhase_ = GamePhase::PLAYER2PICKS;
case GamePhase::PLAYER2PUTS: gamePhase_ = GamePhase::PLAYER2PICKS;
break;
case GamePhase::PLAYER2PICKS: gamePhase_ = GamePhase::PLAYER1PUTS;
break;
case GamePhase::PLAYER1PUTS: gamePhase_ = GamePhase::PLAYER1PICKS;
case GamePhase::PLAYER1PUTS: gamePhase_ = GamePhase::PLAYER1PICKS;
break;
}
}
void MasterControl::Quatter()
{
gamePhase_ = GamePhase::QUATTER;
}
void MasterControl::ToggleMusic()
{
......
......@@ -15,6 +15,7 @@ using namespace Urho3D;
class QuatterCam;
class InputMaster;
class EffectMaster;
class Board;
class Piece;
......@@ -45,6 +46,7 @@ public:
MasterControl(Context* context);
static MasterControl* GetInstance();
EffectMaster* effectMaster_;
GameWorld world;
SharedPtr<ResourceCache> cache_;
SharedPtr<Graphics> graphics_;
......@@ -76,6 +78,8 @@ public:
float Sine(const float freq, const float min, const float max, const float shift = 0.0f);
float Cosine(const float freq, const float min, const float max, const float shift = 0.0f);
void Quatter();
private:
static MasterControl* instance_;
InputMaster* inputMaster_;
......
#include "quattercam.h"
#include "effectmaster.h"
#include "board.h"
#include "piece.h"
Piece::Piece(Attributes attributes): Object(MC->GetContext()),
state_{PieceState::FREE },
attributes_{attributes},
sinceStateChange_{0.0f}
state_{PieceState::FREE }
{
rootNode_ = MC->world.scene->CreateChild("Piece"+GetCodon(4));
rootNode_->SetRotation(Quaternion(Random(360.0f), Vector3::UP));
......@@ -18,13 +18,19 @@ Piece::Piece(Attributes attributes): Object(MC->GetContext()),
}
else pieceModel->SetMaterial(MC->GetMaterial("Wood_dark"));
outlineModel_ = rootNode_->CreateComponent<StaticModel>();
outlineModel_->SetCastShadows(false);
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);
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);
}
String Piece::GetCodon(int length) const
......@@ -53,15 +59,9 @@ void Piece::Select()
outlineModel_->SetEnabled(true);
if (state_ == PieceState::FREE){
state_ = PieceState::SELECTED;
Material* glow{outlineModel_->GetMaterial()};
Material* originalGlow{MC->GetMaterial("Glow")};
Color glowColor{glow->GetShaderParameter("MatDiffColor").GetColor()};
Color originalGlowColor{originalGlow->GetShaderParameter("MatDiffColor").GetColor()};
ValueAnimation* fadeIn_{new ValueAnimation(context_)};
fadeIn_->SetKeyFrame(0.0f, glowColor);
fadeIn_->SetKeyFrame(0.23f, originalGlowColor);
glow->SetShaderParameterAnimation("MatDiffColor", fadeIn_, WM_ONCE);
outlineModel_->SetEnabled(true);
MC->effectMaster_->FadeTo(outlineModel_->GetMaterial(),
MC->GetMaterial("Glow")->GetShaderParameter("MatDiffColor").GetColor());
MC->effectMaster_->FadeTo(light_, 0.666f);
}
}
}
......@@ -69,17 +69,10 @@ void Piece::Deselect()
{
if (state_ == PieceState::SELECTED){
state_ = PieceState::FREE;
Material* glow{outlineModel_->GetMaterial()};
Color glowColor{glow->GetShaderParameter("MatDiffColor").GetColor()};
ValueAnimation* fadeOut_{new ValueAnimation(context_)};
fadeOut_->SetKeyFrame(0.0f, glowColor);
fadeOut_->SetKeyFrame(0.23f, glowColor * 0.0f);
glow->SetShaderParameterAnimation("MatDiffColor", fadeOut_, WM_ONCE);
MC->effectMaster_->FadeOut(outlineModel_->GetMaterial());
MC->effectMaster_->FadeOut(light_);
}
// outlineModel_->SetEnabled(false);
}
void Piece::Pick()
{
if (state_ == PieceState::SELECTED){
......@@ -99,16 +92,10 @@ void Piece::Pick()
intoPocketRot->SetKeyFrame(1.0f, Quaternion(10.0f, Vector3(1.0f, 0.0f, 0.5f)));
rootNode_->SetAttributeAnimation("Rotation", intoPocketRot, WM_ONCE);
//Fade out outline
Material* glow{outlineModel_->GetMaterial()};
Color glowColor{glow->GetShaderParameter("MatDiffColor").GetColor()};
ValueAnimation* fadeOut{new ValueAnimation(context_)};
fadeOut->SetKeyFrame(0.0f, glowColor);
fadeOut->SetKeyFrame(0.23f, glowColor * 0.0f);
glow->SetShaderParameterAnimation("MatDiffColor", fadeOut, WM_ONCE);
MC->effectMaster_->FadeOut(outlineModel_->GetMaterial());
MC->effectMaster_->FadeOut(light_);
}
}
void Piece::Put(Vector3 position)
{
if (state_ == PieceState::PICKED){
......
......@@ -15,7 +15,6 @@ enum class PieceState {FREE, SELECTED, PICKED, PUT};
#define NUM_ATTRIBUTES 4
class Piece : public Object
{
URHO3D_OBJECT(Piece, Object);
......@@ -24,8 +23,8 @@ public:
Piece(Attributes);
void SetPosition(Vector3 pos) { rootNode_->SetPosition(pos); }
inline bool GetAttribute(int index) const noexcept { return attributes_[index]; }
inline Attributes GetAttributes() const noexcept { return attributes_; }
bool GetAttribute(int index) const { return attributes_[index]; }
Attributes GetAttributes() const { return Attributes(attributes_.to_ulong()); }
String GetCodon(int length = NUM_ATTRIBUTES) const;
float GetAngle() const { return MC->AttributesToAngle(static_cast<int>(attributes_.to_ulong())); }
void Select();
......@@ -36,10 +35,14 @@ public:
private:
SharedPtr<Node> rootNode_;
SharedPtr<StaticModel> outlineModel_;
SharedPtr<Light> light_;
Attributes attributes_;
PieceState state_;
float sinceStateChange_;
// void FadeInOutline();
// void FadeOutOutline();
// void FadeInLight();
// void FadeOutLight();
};
#endif // PIECE_H
......@@ -18,10 +18,10 @@ using namespace Urho3D;
#define PITCH_MIN 13.0f
#define PITCH_MAX 80.0f
#define PITCH_EDGE 5.0f
#define PITCH_EDGE 10.0f
#define ZOOM_MIN 5.0f
#define ZOOM_MAX 23.0f
#define ZOOM_EDGE 2.3f
#define ZOOM_EDGE 7.0f
class QuatterCam : public Object
{
......@@ -38,6 +38,7 @@ public:
float GetPitch() const { return rootNode_->GetRotation().EulerAngles().x_; }
float GetYaw() const { return rootNode_->GetRotation().EulerAngles().y_ + 180.0f; }
float GetDistance() const { return distance_; }
Vector3 GetPosition() const { return rootNode_->GetPosition(); }
void Zoom(float distance);
Node* GetPocket(bool right) const { return right ? pockets_.second_ : pockets_.first_; }
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment