Turret MiniGame

From Graal Bible
Revision as of 03:32, 9 June 2021 by JimmyAkl (talk | contribs) (Created page with "Welcome to the Turret Minigame! In this Tutorial, we will build a turret rotates and shoots targets around it. The Assets are available on the server and a link will be provi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Welcome to the Turret Minigame!

In this Tutorial, we will build a turret rotates and shoots targets around it. The Assets are available on the server and a link will be provided here if you want to download them.

C# scripts will also be available in order to compare how things change when coding on Graal3D.

Without further ado let's get into it.

Part 1 : Loading and Instantiating the Turret and Obstacles

Gameobjetcs Used:

All the prefabs are under the asset bundle: minigame (you can view them in the bundle explorer "F10")

Turret that shoots the projectiles:

Turret.png

Robot that will serve as a target:

Robot Obstacle Minigame.png

Cubes Instantiated in different position also for targets:

Turret Minigame targets.png

The way I start the minigame;

findplayer("GraalID").addweapon(this.name);

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

Having uploaded the Asset bundle that contains the prefabs (Uploading and Loading AssetBundles for info on how to upload assets), I start off by loading them, instantiating them and giving them a position;

function start() {
  this.start = true;
  //PLAYERMOVEMENT.Freeze();
  
  //for turret control
  this.rotateangle = 0;
  this.rotateangleB = 270;
  this.speed = 15;
  
  this.currentX = player.x;
  this.currentY = player.y;
  this.currentZ = player.z;
  echo ("TurretMiniGame warp from=" @ this.currentLevel @ " x=" @ this.currentX @ " y=" @ this.currentY @ " z=" @ this.currentZ);
  WARPMANAGER.Warp(19, 14 ,0.7 ,"only_ground.nw");
  sleep(1.5);

  Quattro::AssetManagement::LoadAssetBundle("minigame"); //loading the assetbundle
}


function onAssetBundleDownloaded(bundlename) {
  if (bundlename == "minigame") { 
    SetTimer(9999);
    this.empty = GameObject::Create("empty"); // for parenting and deleting

    this.turret = GameObject::createfromassetbundle("minigame", "assets/jimmyminigame/turret.prefab");
    this.robot1 = GameObject::createfromassetbundle("minigame", "assets/jimmyminigame/robot.prefab");
    this.robot2 = GameObject::createfromassetbundle("minigame", "assets/jimmyminigame/robot.prefab");
    this.wreckedrobotprefab = GameObject::createfromassetbundle("minigame", "assets/jimmyminigame/robot wrecked.prefab");
    this.projectileprefab = GameObject::createfromassetbundle("minigame", "assets/jimmyminigame/projectile.prefab");
    this.explosion = GameObject::createfromassetbundle("minigame", "assets/jimmyminigame/explosion.prefab");
    
    this.turret = Object::Instantiate(Type::GameObject, this.turret);
    this.robot1 = Object::Instantiate(Type::GameObject, this.robot1);
    this.robot2 = Object::Instantiate(Type::GameObject, this.robot2);
    
    this.turret.transform.position = Vector3::Create(player.x, player.z, player.y);
    this.robot1.transform.position = Vector3::Create(player.x - 5, player.z - 1, player.y + 10);
    this.robot2.transform.position = Vector3::Create(player.x + 5, player.z - 1, player.y + 10);
    
    this.turret.transform.localscale = Vector3::Create(1,1,1);
    this.robot1.transform.localscale = Vector3::Create(1,1,1);
    this.robot2.transform.localscale = Vector3::Create(1,1,1);
    
    LoadCubes();
    
    player.charactercontroller.enabled = false;
    player.gameobject.transform.position = Vector3::Create(player.x, player.z+5, player.y);

    this.launcher = GameObject::Find("Launcher");
    this.barrel = GameObject::Find("Barrel");
  }
}

function LoadCubes() {
  temp.width = 10;
  temp.height = 4;

  for (temp.y = 0; temp.y < temp.height; ++temp.y) {
    for (temp.x = 0; temp.x < temp.width; ++temp.x) {

      temp.block = GameObject::CreatePrimitive(PrimitiveType::Cube);
      temp.block.Name = "myblock";
      temp.block.transform.parent = this.empty.transform; // for deleting
      temp.block.transform.position = Vector3::Create(player.x - 5 + temp.x, player.z + temp.y, player.y + 15);
      temp.block.AddComponent(Type::RigidBody);
      temp.block.layer = this.projectile.layer; 
    }
  }

  temp.nbofobjects = 20;
  temp.radius = 7f;

  for (temp.i = 0; temp.i < temp.nbofobjects; ++temp.i) {
    temp.angle = temp.i * Mathf::PI * 2 / temp.nbofobjects;
    temp.x1 = cos(temp.angle) * temp.radius;
    temp.y1 = sin(temp.angle) * temp.radius;
    this.cube = GameObject::CreatePrimitive(PrimitiveType::Cube);
    this.cube.transform.parent = this.empty.transform;
    this.cube.transform.position = Vector3::create(player.x + temp.x1, player.z, player.y + temp.y1);
    this.cube.AddComponent(Type::RigidBody);
    temp.angleDegrees = -temp.angle * Mathf::Rad2Deg;
    this.cube.transform.rotation = Quaternion::euler(0, temp.angleDegrees, 0); 
  }
}