This is my code:
#include "Scripts/Utilities/Sample.as"
Node@ mushroomNode;
StaticModel@ mushroomObject;
Material@ My_Mat;
ValueAnimation@ My_Color;
bool My_Click = false;
bool My_Click2 = false;
int Clk = 1;
int C1 = 1;
void Start()
{
SampleStart();
CreateScene();
SetupViewport();
SubscribeToEvents();
}
void CreateScene()
{
scene_ = Scene();
scene_.CreateComponent("Octree");
Node@ lightNode = scene_.CreateChild("DirectionalLight");
lightNode.direction = Vector3(0.6f, -1.0f, 0.8f); // The direction vector does not need to be normalized
Light@ light = lightNode.CreateComponent("Light");
light.lightType = LIGHT_DIRECTIONAL;
light.brightness = 2.1f;
My_Mat = cache.GetResource("Material", "Materials/wire_255013019.xml");
My_Color = ValueAnimation();
mushroomNode = scene_.CreateChild("Mushroom");
mushroomNode.position = Vector3(0.0f, 0.0f, 300.0f);
mushroomNode.rotation = Quaternion(0.0f, 0.0f, 0.0f);
mushroomNode.SetScale(0.6f);
mushroomObject = mushroomNode.CreateComponent("StaticModel");
mushroomObject.model = cache.GetResource("Model", "3D_Models/Shoe_3.mdl");
mushroomObject.ApplyMaterialList();
ScriptInstance@ instance = mushroomNode.CreateComponent("ScriptInstance");
instance.CreateObject(scriptFile, "Rotator");
Rotator@ rotator = cast<Rotator>(instance.scriptObject);
rotator.rotationSpeed = Vector3(10.0f, 10.0f, 10.0f);
cameraNode = scene_.CreateChild("Camera");
cameraNode.CreateComponent("Camera");
cameraNode.position = Vector3(0.0f, 5.0f, 0.0f);
}
void CreateInstructions()
{
Text@ instructionText = ui.root.CreateChild("Text");
instructionText.text = "SAPNA-CO : Use WASD keys to move!";
instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
instructionText.horizontalAlignment = HA_CENTER;
instructionText.verticalAlignment = VA_CENTER;
instructionText.SetPosition(0, ui.root.height / 3);
}
void SetupViewport()
{
Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
renderer.viewports[0] = viewport;
}
void MoveCamera(float timeStep)
{
const float MOVE_SPEED = 10.0f;
const float MOUSE_SENSITIVITY = -0.05f;
IntVector2 mouseMove = input.mouseMove;
yaw += MOUSE_SENSITIVITY * mouseMove.x;
pitch += MOUSE_SENSITIVITY * mouseMove.y;
pitch = Clamp(pitch, -90.0f, 90.0f);
mushroomNode.Rotate(Quaternion(MOUSE_SENSITIVITY * mouseMove.y, MOUSE_SENSITIVITY * mouseMove.x, 0.0f));
if (input.keyDown['W'])
cameraNode.Translate(Vector3(0.0f, -5.0f, 0.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['S'])
cameraNode.Translate(Vector3(0.0f, 5.0f, 0.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['A'])
cameraNode.Translate(Vector3(5.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['D'])
cameraNode.Translate(Vector3(-5.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['Q'])
cameraNode.Translate(Vector3(0.0f, 0.0f, -5.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['E'])
cameraNode.Translate(Vector3(0.0f, 0.0f, 5.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['Z'])
mushroomNode.position = Vector3(0.0f, 0.0f, 400.0f);
if (input.keyDown['X'])
mushroomNode.position = Vector3(0.0f, 0.0f, 500.0f);
if (input.mouseButtonPress[MOUSEB_LEFT])
{
My_Click = !My_Click;
if (My_Click)
Clk = 0;
else
Clk = 1;
}
if (input.mouseButtonPress[MOUSEB_RIGHT])
{
switch (C1)
{
case 1:
My_Mat.shaderParameters["MatDiffColor"] = Variant(Color(0.0f, 0.0f, 1.0f, 1.0f));
C1 = 2;
break;
case 2:
My_Mat.shaderParameters["MatDiffColor"] = Variant(Color(0.0f, 1.0f, 0.0f, 1.0f));
C1 = 3;
break;
case 3:
My_Mat.shaderParameters["MatDiffColor"] = Variant(Color(1.0f, 0.0f, 1.0f, 1.0f));
C1 = 4;
break;
case 4:
My_Mat.shaderParameters["MatDiffColor"] = Variant(Color(1.0f, 1.0f, 0.0f, 1.0f));
C1 = 5;
break;
case 5:
My_Mat.shaderParameters["MatDiffColor"] = Variant(Color(0.0f, 1.0f, 1.0f, 1.0f));
C1 = 6;
break;
case 6:
My_Mat.shaderParameters["MatDiffColor"] = Variant(Color(1.0f, 1.0f, 1.0f, 1.0f));
C1 = 7;
break;
case 7:
My_Mat.shaderParameters["MatDiffColor"] = Variant(Color(1.0f, 0.0f, 0.0f, 1.0f));
C1 = 1;
break;
}
}
}
void SubscribeToEvents()
{
SubscribeToEvent("Update", "HandleUpdate");
}
void HandleUpdate(StringHash eventType, VariantMap& eventData)
{
float timeStep = eventData["TimeStep"].GetFloat();
MoveCamera(timeStep);
}
class Rotator : ScriptObject
{
Vector3 rotationSpeed;
void Update(float timeStep)
{
node.Rotate(Quaternion(rotationSpeed.x * timeStep * Clk, rotationSpeed.y * timeStep * Clk, rotationSpeed.z * timeStep * Clk));
}
}
String patchInstructions = "";