|
@@ -55,7 +55,7 @@ DEFINE_APPLICATION_MAIN(CharacterDemo)
|
|
|
|
|
|
|
|
CharacterDemo::CharacterDemo(Context* context) :
|
|
CharacterDemo::CharacterDemo(Context* context) :
|
|
|
Sample(context),
|
|
Sample(context),
|
|
|
- touch_(new Touch(context))
|
|
|
|
|
|
|
+ firstPerson_(false)
|
|
|
{
|
|
{
|
|
|
// Register factory and attributes for the Character component so it can be created via CreateComponent, and loaded / saved
|
|
// Register factory and attributes for the Character component so it can be created via CreateComponent, and loaded / saved
|
|
|
Character::RegisterObject(context);
|
|
Character::RegisterObject(context);
|
|
@@ -69,44 +69,39 @@ void CharacterDemo::Start()
|
|
|
{
|
|
{
|
|
|
// Execute base class startup
|
|
// Execute base class startup
|
|
|
Sample::Start();
|
|
Sample::Start();
|
|
|
-
|
|
|
|
|
|
|
+ if (touchEnabled_)
|
|
|
|
|
+ touch_ = new Touch(context_);
|
|
|
|
|
+
|
|
|
// Create static scene content
|
|
// Create static scene content
|
|
|
CreateScene();
|
|
CreateScene();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create the controllable character
|
|
// Create the controllable character
|
|
|
CreateCharacter();
|
|
CreateCharacter();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create the UI content
|
|
// Create the UI content
|
|
|
CreateInstructions();
|
|
CreateInstructions();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Subscribe to necessary events
|
|
// Subscribe to necessary events
|
|
|
SubscribeToEvents();
|
|
SubscribeToEvents();
|
|
|
-
|
|
|
|
|
- // Initialize touch input on Android & iOS
|
|
|
|
|
- if (GetPlatform() == "Android" || GetPlatform() == "iOS")
|
|
|
|
|
- {
|
|
|
|
|
- SetLogoVisible(false);
|
|
|
|
|
- touch_->InitTouchInput();
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void CharacterDemo::CreateScene()
|
|
void CharacterDemo::CreateScene()
|
|
|
{
|
|
{
|
|
|
ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
scene_ = new Scene(context_);
|
|
scene_ = new Scene(context_);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create scene subsystem components
|
|
// Create scene subsystem components
|
|
|
scene_->CreateComponent<Octree>();
|
|
scene_->CreateComponent<Octree>();
|
|
|
scene_->CreateComponent<PhysicsWorld>();
|
|
scene_->CreateComponent<PhysicsWorld>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create camera and define viewport. We will be doing load / save, so it's convenient to create the camera outside the scene,
|
|
// Create camera and define viewport. We will be doing load / save, so it's convenient to create the camera outside the scene,
|
|
|
// so that it won't be destroyed and recreated, and we don't have to redefine the viewport on load
|
|
// so that it won't be destroyed and recreated, and we don't have to redefine the viewport on load
|
|
|
cameraNode_ = new Node(context_);
|
|
cameraNode_ = new Node(context_);
|
|
|
Camera* camera = cameraNode_->CreateComponent<Camera>();
|
|
Camera* camera = cameraNode_->CreateComponent<Camera>();
|
|
|
camera->SetFarClip(300.0f);
|
|
camera->SetFarClip(300.0f);
|
|
|
GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));
|
|
GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create static scene content. First create a zone for ambient lighting and fog control
|
|
// Create static scene content. First create a zone for ambient lighting and fog control
|
|
|
Node* zoneNode = scene_->CreateChild("Zone");
|
|
Node* zoneNode = scene_->CreateChild("Zone");
|
|
|
Zone* zone = zoneNode->CreateComponent<Zone>();
|
|
Zone* zone = zoneNode->CreateComponent<Zone>();
|
|
@@ -115,7 +110,7 @@ void CharacterDemo::CreateScene()
|
|
|
zone->SetFogStart(100.0f);
|
|
zone->SetFogStart(100.0f);
|
|
|
zone->SetFogEnd(300.0f);
|
|
zone->SetFogEnd(300.0f);
|
|
|
zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
|
|
zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create a directional light with cascaded shadow mapping
|
|
// Create a directional light with cascaded shadow mapping
|
|
|
Node* lightNode = scene_->CreateChild("DirectionalLight");
|
|
Node* lightNode = scene_->CreateChild("DirectionalLight");
|
|
|
lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f));
|
|
lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f));
|
|
@@ -133,14 +128,14 @@ void CharacterDemo::CreateScene()
|
|
|
StaticModel* object = floorNode->CreateComponent<StaticModel>();
|
|
StaticModel* object = floorNode->CreateComponent<StaticModel>();
|
|
|
object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
|
|
object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
|
|
|
object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
|
|
object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
RigidBody* body = floorNode->CreateComponent<RigidBody>();
|
|
RigidBody* body = floorNode->CreateComponent<RigidBody>();
|
|
|
// Use collision layer bit 2 to mark world scenery. This is what we will raycast against to prevent camera from going
|
|
// Use collision layer bit 2 to mark world scenery. This is what we will raycast against to prevent camera from going
|
|
|
// inside geometry
|
|
// inside geometry
|
|
|
body->SetCollisionLayer(2);
|
|
body->SetCollisionLayer(2);
|
|
|
CollisionShape* shape = floorNode->CreateComponent<CollisionShape>();
|
|
CollisionShape* shape = floorNode->CreateComponent<CollisionShape>();
|
|
|
shape->SetBox(Vector3::ONE);
|
|
shape->SetBox(Vector3::ONE);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create mushrooms of varying sizes
|
|
// Create mushrooms of varying sizes
|
|
|
const unsigned NUM_MUSHROOMS = 60;
|
|
const unsigned NUM_MUSHROOMS = 60;
|
|
|
for (unsigned i = 0; i < NUM_MUSHROOMS; ++i)
|
|
for (unsigned i = 0; i < NUM_MUSHROOMS; ++i)
|
|
@@ -153,19 +148,19 @@ void CharacterDemo::CreateScene()
|
|
|
object->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
|
|
object->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
|
|
|
object->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
|
|
object->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
|
|
|
object->SetCastShadows(true);
|
|
object->SetCastShadows(true);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
RigidBody* body = objectNode->CreateComponent<RigidBody>();
|
|
RigidBody* body = objectNode->CreateComponent<RigidBody>();
|
|
|
body->SetCollisionLayer(2);
|
|
body->SetCollisionLayer(2);
|
|
|
CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
|
|
CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
|
|
|
shape->SetTriangleMesh(object->GetModel(), 0);
|
|
shape->SetTriangleMesh(object->GetModel(), 0);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create movable boxes. Let them fall from the sky at first
|
|
// Create movable boxes. Let them fall from the sky at first
|
|
|
const unsigned NUM_BOXES = 100;
|
|
const unsigned NUM_BOXES = 100;
|
|
|
for (unsigned i = 0; i < NUM_BOXES; ++i)
|
|
for (unsigned i = 0; i < NUM_BOXES; ++i)
|
|
|
{
|
|
{
|
|
|
float scale = Random(2.0f) + 0.5f;
|
|
float scale = Random(2.0f) + 0.5f;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
Node* objectNode = scene_->CreateChild("Box");
|
|
Node* objectNode = scene_->CreateChild("Box");
|
|
|
objectNode->SetPosition(Vector3(Random(180.0f) - 90.0f, Random(10.0f) + 10.0f, Random(180.0f) - 90.0f));
|
|
objectNode->SetPosition(Vector3(Random(180.0f) - 90.0f, Random(10.0f) + 10.0f, Random(180.0f) - 90.0f));
|
|
|
objectNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
|
|
objectNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
|
|
@@ -174,7 +169,7 @@ void CharacterDemo::CreateScene()
|
|
|
object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
|
|
object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
|
|
|
object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
|
|
object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
|
|
|
object->SetCastShadows(true);
|
|
object->SetCastShadows(true);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
RigidBody* body = objectNode->CreateComponent<RigidBody>();
|
|
RigidBody* body = objectNode->CreateComponent<RigidBody>();
|
|
|
body->SetCollisionLayer(2);
|
|
body->SetCollisionLayer(2);
|
|
|
// Bigger boxes will be heavier and harder to move
|
|
// Bigger boxes will be heavier and harder to move
|
|
@@ -182,26 +177,22 @@ void CharacterDemo::CreateScene()
|
|
|
CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
|
|
CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
|
|
|
shape->SetBox(Vector3::ONE);
|
|
shape->SetBox(Vector3::ONE);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // Pass knowledge of the scene & camera node to the Touch helper object
|
|
|
|
|
- touch_->scene_ = scene_;
|
|
|
|
|
- touch_->cameraNode_ = cameraNode_;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void CharacterDemo::CreateCharacter()
|
|
void CharacterDemo::CreateCharacter()
|
|
|
{
|
|
{
|
|
|
ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
Node* objectNode = scene_->CreateChild("Jack");
|
|
Node* objectNode = scene_->CreateChild("Jack");
|
|
|
objectNode->SetPosition(Vector3(0.0f, 1.0f, 0.0f));
|
|
objectNode->SetPosition(Vector3(0.0f, 1.0f, 0.0f));
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create the rendering component + animation controller
|
|
// Create the rendering component + animation controller
|
|
|
AnimatedModel* object = objectNode->CreateComponent<AnimatedModel>();
|
|
AnimatedModel* object = objectNode->CreateComponent<AnimatedModel>();
|
|
|
object->SetModel(cache->GetResource<Model>("Models/Jack.mdl"));
|
|
object->SetModel(cache->GetResource<Model>("Models/Jack.mdl"));
|
|
|
object->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml"));
|
|
object->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml"));
|
|
|
object->SetCastShadows(true);
|
|
object->SetCastShadows(true);
|
|
|
objectNode->CreateComponent<AnimationController>();
|
|
objectNode->CreateComponent<AnimationController>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Set the head bone for manual control
|
|
// Set the head bone for manual control
|
|
|
object->GetSkeleton().GetBone("Bip01_Head")->animated_ = false;
|
|
object->GetSkeleton().GetBone("Bip01_Head")->animated_ = false;
|
|
|
|
|
|
|
@@ -209,18 +200,18 @@ void CharacterDemo::CreateCharacter()
|
|
|
RigidBody* body = objectNode->CreateComponent<RigidBody>();
|
|
RigidBody* body = objectNode->CreateComponent<RigidBody>();
|
|
|
body->SetCollisionLayer(1);
|
|
body->SetCollisionLayer(1);
|
|
|
body->SetMass(1.0f);
|
|
body->SetMass(1.0f);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Set zero angular factor so that physics doesn't turn the character on its own.
|
|
// Set zero angular factor so that physics doesn't turn the character on its own.
|
|
|
// Instead we will control the character yaw manually
|
|
// Instead we will control the character yaw manually
|
|
|
body->SetAngularFactor(Vector3::ZERO);
|
|
body->SetAngularFactor(Vector3::ZERO);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Set the rigidbody to signal collision also when in rest, so that we get ground collisions properly
|
|
// Set the rigidbody to signal collision also when in rest, so that we get ground collisions properly
|
|
|
body->SetCollisionEventMode(COLLISION_ALWAYS);
|
|
body->SetCollisionEventMode(COLLISION_ALWAYS);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Set a capsule shape for collision
|
|
// Set a capsule shape for collision
|
|
|
CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
|
|
CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
|
|
|
shape->SetCapsule(0.7f, 1.8f, Vector3(0.0f, 0.9f, 0.0f));
|
|
shape->SetCapsule(0.7f, 1.8f, Vector3(0.0f, 0.9f, 0.0f));
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Create the character logic component, which takes care of steering the rigidbody
|
|
// Create the character logic component, which takes care of steering the rigidbody
|
|
|
// Remember it so that we can set the controls. Use a WeakPtr because the scene hierarchy already owns it
|
|
// Remember it so that we can set the controls. Use a WeakPtr because the scene hierarchy already owns it
|
|
|
// and keeps it alive as long as it's not removed from the hierarchy
|
|
// and keeps it alive as long as it's not removed from the hierarchy
|
|
@@ -231,7 +222,7 @@ void CharacterDemo::CreateInstructions()
|
|
|
{
|
|
{
|
|
|
ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
|
UI* ui = GetSubsystem<UI>();
|
|
UI* ui = GetSubsystem<UI>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Construct new Text object, set string to display and font to use
|
|
// Construct new Text object, set string to display and font to use
|
|
|
Text* instructionText = ui->GetRoot()->CreateChild<Text>();
|
|
Text* instructionText = ui->GetRoot()->CreateChild<Text>();
|
|
|
instructionText->SetText(
|
|
instructionText->SetText(
|
|
@@ -242,7 +233,7 @@ void CharacterDemo::CreateInstructions()
|
|
|
instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
|
|
instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
|
|
|
// The text has multiple rows. Center them in relation to each other
|
|
// The text has multiple rows. Center them in relation to each other
|
|
|
instructionText->SetTextAlignment(HA_CENTER);
|
|
instructionText->SetTextAlignment(HA_CENTER);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Position the text relative to the screen center
|
|
// Position the text relative to the screen center
|
|
|
instructionText->SetHorizontalAlignment(HA_CENTER);
|
|
instructionText->SetHorizontalAlignment(HA_CENTER);
|
|
|
instructionText->SetVerticalAlignment(VA_CENTER);
|
|
instructionText->SetVerticalAlignment(VA_CENTER);
|
|
@@ -253,15 +244,18 @@ void CharacterDemo::SubscribeToEvents()
|
|
|
{
|
|
{
|
|
|
// Subscribe to Update event for setting the character controls before physics simulation
|
|
// Subscribe to Update event for setting the character controls before physics simulation
|
|
|
SubscribeToEvent(E_UPDATE, HANDLER(CharacterDemo, HandleUpdate));
|
|
SubscribeToEvent(E_UPDATE, HANDLER(CharacterDemo, HandleUpdate));
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Subscribe to PostUpdate event for updating the camera position after physics simulation
|
|
// Subscribe to PostUpdate event for updating the camera position after physics simulation
|
|
|
SubscribeToEvent(E_POSTUPDATE, HANDLER(CharacterDemo, HandlePostUpdate));
|
|
SubscribeToEvent(E_POSTUPDATE, HANDLER(CharacterDemo, HandlePostUpdate));
|
|
|
|
|
+
|
|
|
|
|
+ // Unsubscribe the SceneUpdate event from base class as the camera node is being controlled in HandlePostUpdate() in this sample
|
|
|
|
|
+ UnsubscribeFromEvent(E_SCENEUPDATE);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void CharacterDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
|
|
void CharacterDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
{
|
|
|
using namespace Update;
|
|
using namespace Update;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
Input* input = GetSubsystem<Input>();
|
|
Input* input = GetSubsystem<Input>();
|
|
|
|
|
|
|
@@ -270,52 +264,73 @@ void CharacterDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
// Clear previous controls
|
|
// Clear previous controls
|
|
|
character_->controls_.Set(CTRL_FORWARD | CTRL_BACK | CTRL_LEFT | CTRL_RIGHT | CTRL_JUMP, false);
|
|
character_->controls_.Set(CTRL_FORWARD | CTRL_BACK | CTRL_LEFT | CTRL_RIGHT | CTRL_JUMP, false);
|
|
|
|
|
|
|
|
- if (touch_->touchEnabled_)
|
|
|
|
|
- {
|
|
|
|
|
- // Update controls using touch (mobile)
|
|
|
|
|
|
|
+ // Update controls using touch utility class
|
|
|
|
|
+ if (touch_)
|
|
|
touch_->UpdateTouches(character_->controls_);
|
|
touch_->UpdateTouches(character_->controls_);
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Update controls using keys
|
|
|
|
|
+ UI* ui = GetSubsystem<UI>();
|
|
|
|
|
+ if (!ui->GetFocusElement())
|
|
|
{
|
|
{
|
|
|
- // Update controls using keys (desktop)
|
|
|
|
|
- UI* ui = GetSubsystem<UI>();
|
|
|
|
|
-
|
|
|
|
|
- if (!ui->GetFocusElement())
|
|
|
|
|
|
|
+ if (!touch_ || !touch_->useGyroscope_)
|
|
|
{
|
|
{
|
|
|
character_->controls_.Set(CTRL_FORWARD, input->GetKeyDown('W'));
|
|
character_->controls_.Set(CTRL_FORWARD, input->GetKeyDown('W'));
|
|
|
character_->controls_.Set(CTRL_BACK, input->GetKeyDown('S'));
|
|
character_->controls_.Set(CTRL_BACK, input->GetKeyDown('S'));
|
|
|
character_->controls_.Set(CTRL_LEFT, input->GetKeyDown('A'));
|
|
character_->controls_.Set(CTRL_LEFT, input->GetKeyDown('A'));
|
|
|
character_->controls_.Set(CTRL_RIGHT, input->GetKeyDown('D'));
|
|
character_->controls_.Set(CTRL_RIGHT, input->GetKeyDown('D'));
|
|
|
- character_->controls_.Set(CTRL_JUMP, input->GetKeyDown(KEY_SPACE));
|
|
|
|
|
-
|
|
|
|
|
- // Add character yaw & pitch from the mouse motion
|
|
|
|
|
- character_->controls_.yaw_ += (float)input->GetMouseMoveX() * YAW_SENSITIVITY;
|
|
|
|
|
- character_->controls_.pitch_ += (float)input->GetMouseMoveY() * YAW_SENSITIVITY;
|
|
|
|
|
- // Limit pitch
|
|
|
|
|
- character_->controls_.pitch_ = Clamp(character_->controls_.pitch_, -80.0f, 80.0f);
|
|
|
|
|
-
|
|
|
|
|
- // Switch between 1st and 3rd person
|
|
|
|
|
- if (input->GetKeyPress('F'))
|
|
|
|
|
- touch_->firstPerson_ = touch_->newFirstPerson_ = !touch_->firstPerson_;
|
|
|
|
|
-
|
|
|
|
|
- // Check for loading / saving the scene
|
|
|
|
|
- if (input->GetKeyPress(KEY_F5))
|
|
|
|
|
- {
|
|
|
|
|
- File saveFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/CharacterDemo.xml",
|
|
|
|
|
- FILE_WRITE);
|
|
|
|
|
- scene_->SaveXML(saveFile);
|
|
|
|
|
- }
|
|
|
|
|
- if (input->GetKeyPress(KEY_F7))
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ character_->controls_.Set(CTRL_JUMP, input->GetKeyDown(KEY_SPACE));
|
|
|
|
|
+
|
|
|
|
|
+ // Add character yaw & pitch from the mouse motion or touch input
|
|
|
|
|
+ if (touchEnabled_)
|
|
|
|
|
+ {
|
|
|
|
|
+ for (unsigned i = 0; i < input->GetNumTouches(); ++i)
|
|
|
{
|
|
{
|
|
|
- File loadFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/CharacterDemo.xml", FILE_READ);
|
|
|
|
|
- scene_->LoadXML(loadFile);
|
|
|
|
|
- // After loading we have to reacquire the weak pointer to the Character component, as it has been recreated
|
|
|
|
|
- // Simply find the character's scene node by name as there's only one of them
|
|
|
|
|
- Node* characterNode = scene_->GetChild("Jack", true);
|
|
|
|
|
- if (characterNode)
|
|
|
|
|
- character_ = characterNode->GetComponent<Character>();
|
|
|
|
|
|
|
+ TouchState* state = input->GetTouch(i);
|
|
|
|
|
+ if (!state->touchedElement_) // Touch on empty space
|
|
|
|
|
+ {
|
|
|
|
|
+ Camera* camera = cameraNode_->GetComponent<Camera>();
|
|
|
|
|
+ if (!camera)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ Graphics* graphics = GetSubsystem<Graphics>();
|
|
|
|
|
+ character_->controls_.yaw_ += TOUCH_SENSITIVITY * camera->GetFov() / graphics->GetHeight() * state->delta_.x_;
|
|
|
|
|
+ character_->controls_.pitch_ += TOUCH_SENSITIVITY * camera->GetFov() / graphics->GetHeight() * state->delta_.y_;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ character_->controls_.yaw_ += (float)input->GetMouseMoveX() * YAW_SENSITIVITY;
|
|
|
|
|
+ character_->controls_.pitch_ += (float)input->GetMouseMoveY() * YAW_SENSITIVITY;
|
|
|
|
|
+ }
|
|
|
|
|
+ // Limit pitch
|
|
|
|
|
+ character_->controls_.pitch_ = Clamp(character_->controls_.pitch_, -80.0f, 80.0f);
|
|
|
|
|
+
|
|
|
|
|
+ // Switch between 1st and 3rd person
|
|
|
|
|
+ if (input->GetKeyPress('F'))
|
|
|
|
|
+ firstPerson_ = !firstPerson_;
|
|
|
|
|
+
|
|
|
|
|
+ // Turn on/off gyroscope on mobile platform
|
|
|
|
|
+ if (touch_ && input->GetKeyPress('G'))
|
|
|
|
|
+ touch_->useGyroscope_ = !touch_->useGyroscope_;
|
|
|
|
|
+
|
|
|
|
|
+ // Check for loading / saving the scene
|
|
|
|
|
+ if (input->GetKeyPress(KEY_F5))
|
|
|
|
|
+ {
|
|
|
|
|
+ File saveFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/CharacterDemo.xml", FILE_WRITE);
|
|
|
|
|
+ scene_->SaveXML(saveFile);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (input->GetKeyPress(KEY_F7))
|
|
|
|
|
+ {
|
|
|
|
|
+ File loadFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/CharacterDemo.xml", FILE_READ);
|
|
|
|
|
+ scene_->LoadXML(loadFile);
|
|
|
|
|
+ // After loading we have to reacquire the weak pointer to the Character component, as it has been recreated
|
|
|
|
|
+ // Simply find the character's scene node by name as there's only one of them
|
|
|
|
|
+ Node* characterNode = scene_->GetChild("Jack", true);
|
|
|
|
|
+ if (characterNode)
|
|
|
|
|
+ character_ = characterNode->GetComponent<Character>();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Set rotation already here so that it's updated every rendering frame instead of every physics frame
|
|
// Set rotation already here so that it's updated every rendering frame instead of every physics frame
|
|
@@ -327,13 +342,13 @@ void CharacterDemo::HandlePostUpdate(StringHash eventType, VariantMap& eventData
|
|
|
{
|
|
{
|
|
|
if (!character_)
|
|
if (!character_)
|
|
|
return;
|
|
return;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
Node* characterNode = character_->GetNode();
|
|
Node* characterNode = character_->GetNode();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Get camera lookat dir from character yaw + pitch
|
|
// Get camera lookat dir from character yaw + pitch
|
|
|
Quaternion rot = characterNode->GetRotation();
|
|
Quaternion rot = characterNode->GetRotation();
|
|
|
Quaternion dir = rot * Quaternion(character_->controls_.pitch_, Vector3::RIGHT);
|
|
Quaternion dir = rot * Quaternion(character_->controls_.pitch_, Vector3::RIGHT);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Turn head to camera pitch, but limit to avoid unnatural animation
|
|
// Turn head to camera pitch, but limit to avoid unnatural animation
|
|
|
Node* headNode = characterNode->GetChild("Bip01_Head", true);
|
|
Node* headNode = characterNode->GetChild("Bip01_Head", true);
|
|
|
float limitPitch = Clamp(character_->controls_.pitch_, -45.0f, 45.0f);
|
|
float limitPitch = Clamp(character_->controls_.pitch_, -45.0f, 45.0f);
|
|
@@ -344,7 +359,7 @@ void CharacterDemo::HandlePostUpdate(StringHash eventType, VariantMap& eventData
|
|
|
// Correct head orientation because LookAt assumes Z = forward, but the bone has been authored differently (Y = forward)
|
|
// Correct head orientation because LookAt assumes Z = forward, but the bone has been authored differently (Y = forward)
|
|
|
headNode->Rotate(Quaternion(0.0f, 90.0f, 90.0f));
|
|
headNode->Rotate(Quaternion(0.0f, 90.0f, 90.0f));
|
|
|
|
|
|
|
|
- if (touch_->firstPerson_)
|
|
|
|
|
|
|
+ if (firstPerson_)
|
|
|
{
|
|
{
|
|
|
cameraNode_->SetPosition(headNode->GetWorldPosition() + rot * Vector3(0.0f, 0.15f, 0.2f));
|
|
cameraNode_->SetPosition(headNode->GetWorldPosition() + rot * Vector3(0.0f, 0.15f, 0.2f));
|
|
|
cameraNode_->SetRotation(dir);
|
|
cameraNode_->SetRotation(dir);
|
|
@@ -353,16 +368,16 @@ void CharacterDemo::HandlePostUpdate(StringHash eventType, VariantMap& eventData
|
|
|
{
|
|
{
|
|
|
// Third person camera: position behind the character
|
|
// Third person camera: position behind the character
|
|
|
Vector3 aimPoint = characterNode->GetPosition() + rot * Vector3(0.0f, 1.7f, 0.0f);
|
|
Vector3 aimPoint = characterNode->GetPosition() + rot * Vector3(0.0f, 1.7f, 0.0f);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Collide camera ray with static physics objects (layer bitmask 2) to ensure we see the character properly
|
|
// Collide camera ray with static physics objects (layer bitmask 2) to ensure we see the character properly
|
|
|
Vector3 rayDir = dir * Vector3::BACK;
|
|
Vector3 rayDir = dir * Vector3::BACK;
|
|
|
- float rayDistance = touch_->cameraDistance_;
|
|
|
|
|
|
|
+ float rayDistance = touch_ ? touch_->cameraDistance_ : CAMERA_INITIAL_DIST;
|
|
|
PhysicsRaycastResult result;
|
|
PhysicsRaycastResult result;
|
|
|
scene_->GetComponent<PhysicsWorld>()->RaycastSingle(result, Ray(aimPoint, rayDir), rayDistance, 2);
|
|
scene_->GetComponent<PhysicsWorld>()->RaycastSingle(result, Ray(aimPoint, rayDir), rayDistance, 2);
|
|
|
if (result.body_)
|
|
if (result.body_)
|
|
|
rayDistance = Min(rayDistance, result.distance_);
|
|
rayDistance = Min(rayDistance, result.distance_);
|
|
|
rayDistance = Clamp(rayDistance, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
|
|
rayDistance = Clamp(rayDistance, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
cameraNode_->SetPosition(aimPoint + rayDir * rayDistance);
|
|
cameraNode_->SetPosition(aimPoint + rayDir * rayDistance);
|
|
|
cameraNode_->SetRotation(dir);
|
|
cameraNode_->SetRotation(dir);
|
|
|
}
|
|
}
|