Bomberman MiniGame: Difference between revisions

From Graal Bible
No edit summary
No edit summary
Line 3: Line 3:
Assets will be available for you to download and follow the tuto.
Assets will be available for you to download and follow the tuto.


Since this game is meant to be multiplayer, we will be placing the GameObjects in npc for them to be visible to all the players. (more info on that here: [[Placing GameObjects in NPC]])
Since this game is meant to be multiplayer, we will be placing the GameObjects in NPCs for them to be visible to all the players. (more info on that here: [[Placing GameObjects in NPC]])


First, we'll have the weapon that starts up the game and places the NPCs (''found under '''weapon: Merlin/BomberMan/Game''''')
First, we'll have the weapon that starts up the game and places the NPCs (''found under '''weapon: Merlin/BomberMan/Game''''')
Line 10: Line 10:
     temp.pnpc = putnpc2(x, y, "");
     temp.pnpc = putnpc2(x, y, "");
     temp.pnpc.join("merlin_bomberman_bomb");
     temp.pnpc.join("merlin_bomberman_bomb");
    //temp.pnpc.DropBomb(explvl);
   }
   }
   if (cmd == "level") {
   if (cmd == "level") {
Line 54: Line 53:
   
   
  public function BomberManQuit() {
  public function BomberManQuit() {
  echo("KartQui warp back to=" @ this.currentLevel @ " x=" @ this.currentX @ " y=" @ this.currentY @ " z=" @ this.currentZ);
   if (isObject("bomberman_Quit")) {
   if (isObject("bomberman_Quit")) {
     "bomberman_Quit".destroy();
     "bomberman_Quit".destroy();
Line 61: Line 59:
   PLAYERMOVEMENT.canJump = true;
   PLAYERMOVEMENT.canJump = true;
   this.GAMEON = false;
   this.GAMEON = false;
  Quattro3D::PlayerCamera::Instance.FreeCam = false;
  findweapon("Merlin/BomberMan/Player").onQuit();
  Object::destroy(this.bomberManParentNode);
  this.bomberManParentNode = null;
  }
  }
   
   
Line 86: Line 80:
   sleep(0.05);
   sleep(0.05);
   triggerserver("gui", this.name, "bomb", this.hit.transform.gameobject.transform.position.x, this.hit.transform.gameobject.transform.position.z);
   triggerserver("gui", this.name, "bomb", this.hit.transform.gameobject.transform.position.x, this.hit.transform.gameobject.transform.position.z);
}
Here we trigger the server every time we want to place a GameObject.
In the function start, we trigger the server with the command "'''level'''" to load the bomberman map by placing an NPC and joining it with the class "'''merlin_bomberman_level'''".
Once we start the game, the player is not allowed to Jump: '''PLAYERMOVEMENT.canJump = false;'''
And pressing space will be used to drop a bomb, '''''function onKeyPressed()''''' checks if space is pressed and calls '''''DropBomb()'''.''
===== <u>New : RayCast</u> =====
Think of RayCast as '''''triggeraction()''''' that triggers NPCs at given (x,y). Similarly, '''''RayCast''''' casts a ray given a position, a direction and a length, that detects colliders.
{| class="wikitable"
!Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
|}
'''Returns:''' Boolean indicating if the RayCast hit a Collider.
''(More info here: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html<nowiki/>)''
Here, since the bomberman level is a grid and the floor is made of squares. We want the player to drop the bomb exactly in one square and not in between two. Therefore, when space is pressed, we RayCast with the PlayerGameObject as '''origin''', with a Vector3::Down '''direction''', '''''this.hit''''' as the '''output''' and 100 for '''length'''. The other parameters are kept at default.
This will return the collider of the GameObject underneath the player, which is the square tile.
We extract the tile's position from '''''this.hit''''' :
{| class="wikitable"
!this.hit.transform.gameobject.transform.position.x
|}
And we trigger the server to place a bomb NPC;
function onActionServerside(cmd, x, y) {
  if (cmd == "bomb") {
    temp.pnpc = putnpc2(x, y, "");
    temp.pnpc.join("merlin_bomberman_bomb");
  }
  if (cmd == "level") {
      temp.bombernpc = putnpc2(x, y, "");
      temp.bombernpc.join("merlin_bomberman_level");
  }
  }
  }

Revision as of 00:39, 21 June 2021

Welcome to the Bomberman Minigame Tutorial!

Bomberman cover.png

Assets will be available for you to download and follow the tuto.

Since this game is meant to be multiplayer, we will be placing the GameObjects in NPCs for them to be visible to all the players. (more info on that here: Placing GameObjects in NPC)

First, we'll have the weapon that starts up the game and places the NPCs (found under weapon: Merlin/BomberMan/Game)

function onActionServerside(cmd, x, y) {
  if (cmd == "bomb") {
    temp.pnpc = putnpc2(x, y, "");
    temp.pnpc.join("merlin_bomberman_bomb");
  }
  if (cmd == "level") {
     temp.bombernpc = putnpc2(x, y, "");
     temp.bombernpc.join("merlin_bomberman_level");
  }
}

//#CLIENTSIDE
function onCreated() {
  echo(this.name @ " OnCreated");
  level.explosionpower = 4; //for powerups with explosion
  level.numberofbombs = 2; //number of bombs allowed to place at a time
}

public function Load() {
  echo(this.name @ " LOAD");
  Object::destroy(this.bomberManParentNode);
  if (isObject("bomberman_Quit")) {
    "bomberman_Quit".destroy();
  }
  new GuiButtonCtrl("bomberman_Quit") {
    profile = GuiBlueButtonProfile;
    x = 600;
    y = 200;
    width = 150;
    height = 30;
    text = "bombermanQuit";
  }

  temp.warpLevel = "only_ground.nw";
  temp.warpX = 4.65; 
  temp.warpY = 21.33; 
  temp.warpZ = 0.7; 
  WARPMANAGER.Warp(temp.warpX, temp.warpY ,temp.warpZ ,temp.warpLevel);
  sleep(0.5);
  start(); 
}

function bomberman_Quit.onAction() {
  BomberManQuit();
}

public function BomberManQuit() {
  if (isObject("bomberman_Quit")) {
    "bomberman_Quit".destroy();
  }
  PLAYERMOVEMENT.UnFreeze();
  PLAYERMOVEMENT.canJump = true;
  this.GAMEON = false;
}

function start() {
  PLAYERMOVEMENT.canJump = false;
  this.GAMEON = true;
  triggerserver("gui", this.name, "level", -60 , 14);
}

function onKeyPressed(keycode, keychar) {
  if (this.GAMEON == true) {
    if (keycode == "32, ,57") {
      DropBomb();
    }
  }
}

function DropBomb() {
  this.hit = RaycastHit::create();
  done = Physics::Raycast(player.gameobject.transform.position, Vector3::Down, this.hit, 100);
  sleep(0.05);
  triggerserver("gui", this.name, "bomb", this.hit.transform.gameobject.transform.position.x, this.hit.transform.gameobject.transform.position.z);
}

Here we trigger the server every time we want to place a GameObject.

In the function start, we trigger the server with the command "level" to load the bomberman map by placing an NPC and joining it with the class "merlin_bomberman_level".

Once we start the game, the player is not allowed to Jump: PLAYERMOVEMENT.canJump = false;

And pressing space will be used to drop a bomb, function onKeyPressed() checks if space is pressed and calls DropBomb().

New : RayCast

Think of RayCast as triggeraction() that triggers NPCs at given (x,y). Similarly, RayCast casts a ray given a position, a direction and a length, that detects colliders.

Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Returns: Boolean indicating if the RayCast hit a Collider.

(More info here: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html)

Here, since the bomberman level is a grid and the floor is made of squares. We want the player to drop the bomb exactly in one square and not in between two. Therefore, when space is pressed, we RayCast with the PlayerGameObject as origin, with a Vector3::Down direction, this.hit as the output and 100 for length. The other parameters are kept at default.

This will return the collider of the GameObject underneath the player, which is the square tile.

We extract the tile's position from this.hit :

this.hit.transform.gameobject.transform.position.x

And we trigger the server to place a bomb NPC;

function onActionServerside(cmd, x, y) {
  if (cmd == "bomb") {
    temp.pnpc = putnpc2(x, y, "");
    temp.pnpc.join("merlin_bomberman_bomb");
  }
  if (cmd == "level") {
     temp.bombernpc = putnpc2(x, y, "");
     temp.bombernpc.join("merlin_bomberman_level");
  }
}