game-scene.cpp 4.54 KB
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <string.h>

#include "game-camera.h"
#include "game-entity.h"
#include "game-model.h"
#include "game-renderer.h"
#include "game-scene.h"
#include "game-texture.h"

#include "third-party/pico-json.h"

#include <dali/dali.h>

using namespace Dali;
using namespace picojson;

using std::vector;

using namespace GameUtils;

GameScene::GameScene()
{
}

GameScene::~GameScene()
{
}

bool GameScene::Load(Window window, const char* filename)
{
  ByteArray bytes;
  if(!LoadFile(filename, bytes))
  {
    return false;
  }

  // add EOL
  bytes.push_back('\0');

  picojson::value root;
  picojson::parse(root, bytes.data());

  bool failed(false);

  if(root.is<object>())
  {
    object rootObject = root.get<object>();
    for(object::iterator it = rootObject.begin(); it != rootObject.end(); ++it)
    {
      std::string entityName((*it).first);

      GameEntity* entity = new GameEntity(entityName.c_str());
      mEntities.PushBack(entity);

      value& val((*it).second);
      value& vLocation = val.get("location");
      value& vRotation = val.get("rotation");
      value& vScale    = val.get("scale");
      value& vSize     = val.get("size");
      value& vModel    = val.get("model");
      value& vTexture  = val.get("texture");

      if(!vLocation.is<null>())
      {
        array& location = vLocation.get<array>();
        entity->SetLocation(Vector3(
          location.at(0).get<double>(),
          location.at(1).get<double>(),
          location.at(2).get<double>()));
      }

      if(!vRotation.is<null>())
      {
        array& rotation = vRotation.get<array>();
        entity->SetRotation(Quaternion(Vector4(
          -rotation.at(0).get<double>(),
          rotation.at(1).get<double>(),
          -rotation.at(2).get<double>(),
          rotation.at(3).get<double>())));
      }

      if(!vScale.is<null>())
      {
        array& scale = vScale.get<array>();
        entity->SetScale(Vector3(
          scale.at(0).get<double>(),
          scale.at(1).get<double>(),
          scale.at(2).get<double>()));
      }

      if(!vSize.is<null>())
      {
        array& size = vSize.get<array>();
        entity->SetSize(Vector3(
          size.at(0).get<double>(),
          size.at(1).get<double>(),
          size.at(2).get<double>()));
      }

      GameModel*   model(NULL);
      GameTexture* texture(NULL);

      if(!vModel.is<null>())
      {
        std::string& strModel = vModel.get<std::string>();
        model                 = GetResource(strModel.c_str(), mModelCache);
      }

      if(!vTexture.is<null>())
      {
        std::string& strTexture = vTexture.get<std::string>();
        texture                 = GetResource(strTexture.c_str(), mTextureCache);
      }

      if(!model || !texture)
      {
        failed = true;
        break;
      }

      entity->GetGameRenderer().SetModel(model);
      entity->GetGameRenderer().SetMainTexture(texture);
    }
  }

  if(failed)
  {
    return false;
  }

  // add all to the window
  mRootActor = Actor::New();
  mRootActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
  mRootActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
  window.GetRootLayer().Add(mRootActor);
  mRootActor.SetProperty(Actor::Property::SCALE, Vector3(-1.0, 1.0, 1.0));
  mRootActor.SetProperty(Actor::Property::POSITION, Vector3(0.0, 0.0, 0.0));
  mRootActor.SetProperty(Actor::Property::ORIENTATION, Quaternion(Degree(90), Vector3(1.0, 0.0, 0.0)));
  for(size_t i = 0; i < mEntities.Size(); ++i)
  {
    Actor actor(mEntities[i]->GetActor());
    actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
    actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
    mRootActor.Add(actor);
    mEntities[i]->UpdateRenderer();
  }

  // update camera
  mCamera.Initialise(window.GetRenderTaskList().GetTask(0).GetCameraActor(), 60.0f, 0.1f, 100.0f, window.GetSize());

  return true;
}

Dali::Actor& GameScene::GetRootActor()
{
  return mRootActor;
}