Driving MiniGame

From Graal Bible
Revision as of 11:06, 28 June 2021 by JimmyAkl (talk | contribs) (Created page with "Welcome to the Driving MiniGame tutorial. This is a simple and easy car driving example. It is not focused on the actual driving experience itself, but more on introducing ne...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Welcome to the Driving MiniGame tutorial.

This is a simple and easy car driving example. It is not focused on the actual driving experience itself, but more on introducing new Unity functionalities.

Asset Bundle: minigame2, env

using the bundle explorer (F10) :

minigame2:

Minigame2.png

env:

Minigame2 env.png

The script is found in the weapon: 3D/Jimmy/Minigame2

findplayer("Graal5918039").addweapon(this.name);
this.join("3d_merlin_camera");

//#CLIENTSIDE
function onPlayerChats() {
  if (player.chat == "dest") dest();
  if (player.chat == "drive") drive();
}

function drive() {
  destLevelName = "only_ground.nw";
  x = 19;
  y = 10;
  z = 0.7;//0.7;
  WARPMANAGER.Warp(x, y ,z ,destLevelName);

  sleep(1);
 
  new GuiWindowCtrl("MyGUI_Minigame_Window1") {
    profile = GuiBlueWindowProfile;
    style = $pref::Video::defaultguistyle;
    clientrelative = true;
    clientextent = "320,240";

    canmove = true;
    canresize = true;
    closequery = false;
    destroyonhide = false;
    text = "Window 1";
    x = 490;
    y = 242;

    new GuiButtonCtrl("MyGUI_Minigame_Button1") {
      profile = GuiBlueButtonProfile;
      text = "Car";
      width = 80;
      x = 77;
      y = 31;
    }
    new GuiButtonCtrl("MyGUI_Minigame_Button2") {
      profile = GuiBlueButtonProfile;
      height = 31;
      text = "Bus";
      width = 80;
      x = 77;
      y = 69;
    }
    new GuiButtonCtrl("MyGUI_Minigame_Button3") {
      profile = GuiBlueButtonProfile;
      height = 31;
      text = "Quit";
      width = 80;
      x = 78;
      y = 109;
    }
    new GuiTextCtrl("MyGUI_Minigame_Text1") {
      profile = GuiBlueTextProfile;
      height = 20;
      text = "Select Type:";
      width = 71;
      x = 11;
      y = 5;
    }
  }
}

function MyGUI_Minigame_Button1.onAction() {
  // Button "Car" has been pressed
  Quattro::AssetManagement::LoadAssetBundle("minigame2");
  Quattro::AssetManagement::LoadAssetBundle("env");
  this.vehicle = "car";
}

function MyGUI_Minigame_Button2.onAction() {
  // Button "Bus" has been pressed
  Quattro::AssetManagement::LoadAssetBundle("minigame2");
  Quattro::AssetManagement::LoadAssetBundle("env");
  this.vehicle = "bus";
}

function MyGUI_Minigame_Button3.onAction() {
  dest();
}

function onAssetBundleDownloaded(bundlename) {
  if (bundlename == "env") {
    this.env = GameObject::createfromassetbundle("env", "assets/jimmyenv2/Environment.prefab");
    this.env = Object::Instantiate(Type::GameObject, this.env);
    this.env.transform.position = Vector3::Create(player.x, player.z, player.y);
  }
  if (bundlename == "minigame2") {
    if (this.vehicle == "bus") {
      dest();
      this.vh = GameObject::createfromassetbundle("minigame2", "assets/jimmyminigame2/Veh_Bus_Blue_Z.prefab");
    }
    
    if (this.vehicle == "car") {
      dest();
      this.vh = GameObject::createfromassetbundle("minigame2", "assets/jimmyminigame2/Veh_Car_Blue_Z.prefab");
    }
    
    this.vh = Object::Instantiate(Type::GameObject, this.vh);
    this.vh.transform.position = Vector3::Create(player.x - 2, player.z, player.y - 5);
    this.vh.AddComponent(Type::RigidBody);

    PLAYERMOVEMENT.Freeze();
    Quattro3D::PlayerCamera::Instance.FreeCam = true;

    handleMovement();
  }
}

function handleMovement() {
  this.on = true;
  SetTimer(0.05);
  this.speed = 40;
  this.rotation = 400;
}

function onTimeOut() {
  if(this.on) {
    temp.campos = this.vh.transform.position.Add(Vector3::create(0,10,-7));
    Quattro3D::PlayerCamera::Instance.transform.position = temp.campos;
    
    this.vInput = Input::GetAxis("Vertical");
    
    this.hInput = Input::GetAxis("Horizontal");
    if (this.vInput < 0) this.hInput = - Input::GetAxis("Horizontal");

    this.vh.transform.Translate(Vector3::forward.Mult(Time::deltaTime * this.speed * this.vInput));
    this.vh.transform.Rotate(Vector3::up.Mult(Time::deltaTime * this.rotation * this.hInput));
    SetTimer(0.05);
  }
}

function dest() {
  this.on = false;
  cam1();
  PLAYERMOVEMENT.unfreeze();
  Object::Destroy(this.vh);
  Object::Destroy(this.env);
}