Difference between revisions of "Code snippets"

From Project X Wiki
Jump to: navigation, search
(Simple)
(Simple)
Line 59: Line 59:
 
   super;
 
   super;
 
  }
 
  }
 +
 +
 +
===Mixing Minigun and Bazooka===
 +
  override void CWorm::FireFinal(CWeapon* Weap,CShootDesc* Desc)
 +
  {
 +
 +
 
 +
  if(Weap->CheckName("Minigun"))
 +
  {
 +
  Desc->SpX = (TurnSide * (0.6 - sqrt((0.5 - FireAngle)*(0.5 - FireAngle)))) * 30;
 +
  Desc->SpY = 30*(0.4 - FireAngle); 
 +
  super(GetWeaponByName("Bazooka"), Desc);
 +
  return;
 +
  }
 +
 
 +
  super;
 +
  }
  
 
===How to find angle between two objects and convert it into fixed (0.0 - 1.0)===
 
===How to find angle between two objects and convert it into fixed (0.0 - 1.0)===

Revision as of 19:13, 13 January 2012

Simple

Iteration through every object, excluding landscape:

  for(local i = 1; i < Env->Objs.Count; i+=1)
  {
    local obj = Env->Objs.Objs[i];
    if(obj == NullObj) continue;
    //do something with obj
  };

Creating a mine:

CMineParams MParams;
CShootDesc SDesc;
zero(&MParams); 
zero(&SDesc);
MParams.Prefuse = 0;  //time before ability to activate
MParams.Flags = 60;    //flags of objects to react on
MParams.Bias = 0;     //Y shift of explosion
MParams.Damage = 48;  //damage
MParams.BlastPower = 100;
MParams.Fuse = 0;
MParams.Radius = 10;
SDesc.Team = 0;
SDesc.X = PosX;  //position
SDesc.Y = PosY;
SDesc.SpX = 0;   //speed
SDesc.SpY = 0;
local Mine = new CMine(Root->Store->GetObject(25, 0), &MParams, &SDesc, false, 0);

Allowing the player to shoot again (inside FireFinal, for example):

nAvalShoots = 1;
nTotalShoots = 0;

Getting a worm's team color:

GetTeamColor(worm->WormTeam)

Retrieving the mouse pos (for example, for airstrike-type weapons):

override void CWorm::FireFinal(CWeapon* Weap, CShootDesc* Desc)
{
    local x = Desc->AddX;
    local y = Desc->AddY;
//....
}

Checking if place is occupied by landscape at spawning object

override void CWorm::FireFinal(CWeapon* Weap,CShootDesc* Desc) 
{
        if (Weap->CheckName("Construct Detector")) 
        {         
        Detector = new CDetector(Root->GetObject(25, 0), -100, -100);
        local Obje = Env->CheckCollision( Detector, PosX+20*TurnSide, PosY-5);
        Detector->PosX = PosX+20*TurnSide;
        Detector->PosY = PosY-5;
        if(Obje != NullObj){if(Obje->ClType == OC_Landscape){Detector->Free(true);};}
        }
  super;
}


Mixing Minigun and Bazooka

 override void CWorm::FireFinal(CWeapon* Weap,CShootDesc* Desc) 
 {


 if(Weap->CheckName("Minigun"))
 {
 Desc->SpX = (TurnSide * (0.6 - sqrt((0.5 - FireAngle)*(0.5 - FireAngle)))) * 30; 
 Desc->SpY = 30*(0.4 - FireAngle);  
 super(GetWeaponByName("Bazooka"), Desc);
 return;
 }
 
 super;
 }

How to find angle between two objects and convert it into fixed (0.0 - 1.0)

local TrgY = AobjPosY - BobjPosY; 
local TrgX = AobjPosX - BobjPosX;
        float angle = atan2(abs(TrgX), -TrgY);
        angle = 1 - angle /  3.14;
        WormDirection = Sign(TrgX); 
        FireAngle = angle;

Complicated

Determining what object is at certain position

For this one, you need to create a collision mask. Utils.pxl has some nice functions for this. Say your script is named "postest":

CColMask* circle;
void postest::InitGraphic() {
         circle = new CColMask(10,10,MakeCircleMask(10));
}

Then, whenever you need to find the object...

local obj = CheckMaskAt(CGObject(Env->Objs.Objs[0]), circle, x, y, -1);