3D Rad - Free 3D game maker - Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Coming soon: software updates

Author Topic: Coin collect script  (Read 434 times)

Daniel Cremers

  • 3D Rad Guru
  • *****
  • Posts: 1092
    • WWW
Coin collect script
« on: April 12, 2010, 03:28:19 AM »
Lots of games use coins or other collectable objects to increase the score. Using this script you will be able to create many of them, using very little resources. The game will load super fast.

Important: connect the valueprint to the counter object

If something goes wrong with the order of linking, just delete the script object, add a new one and start to link again in the correct order.
The coins.txt needs to be in the root of the 3drad flder (not in the 3drad_res). If you compile your prject, don't forget to copy over the coin.txt to the compild folder.
You only need to adjust the number of coins, and the ammount of points. Rest should wok directly.


If you like this code made by Loop, please donate (go to gamesatnight.com, at the top of the page "Gold members" and subscribe for 10$. You will also get to play some cool 3drad games and have access to the FPS maker for 3drad).

Here is a demo to see it in action: http://girlsvault.com/collectables.zip

Code: [Select]
// COIN COLLECTOR v.1 by loop -------------------------------
//-----------------------------------------------------------


// INSTRUCTIONS ---------------------------------------------
//-----------------------------------------------------------
// link this script to the following objects, in this exact order:

//    - the player
//    - the coin skinmesh
//    - the counter that keep the score
//    - the camera

//    - optional: any other object you want to start when a coin is collected, in ex: sounds, particles
//-----------------------------------------------------------


// SETTINGS -------------------------------------------------
//-----------------------------------------------------------
int Ncoins = 20; //how much coins??
int coinsRadius=2; //the radius of the coins
int pointsPerCoin=10; //how much points per coin

// to collect the coins, a vertical intersecting line is used as the player body.
// this line goes from the feet to the head of the player:
int playerFeet=0; //if the center of the player are the feets (like happens with Andro) then this is = 0, else is a NEGATIVE number = distance from center to the feet
int playerHead=2; //the head of the player, this is a positive number: the height of the player

//the path of the file that contain the coordinates for the coins
string coinsFile="coins.txt";

//-----------------------------------------------------------




Vector3 playerLoc;
Vector3[] coins(0);
bool[] collected(0);

Vector3 intersectionPoint1,intersectionPoint2;

Quaternion coinsOrientation;

int i,j;

string filePath,coordinates,coordinateX;
int fileHandle;

void Main()
{

   if (iInitializing()) {
     
      coins.resize(Ncoins);
      collected.resize(Ncoins);

      iObjectImpostersCreate(OBJ_22,Ncoins);
      iObjectOrientation(OBJ_22,coinsOrientation);

      filePath = coinsFile;
      fileHandle = iFileReadOpen(filePath);

      if (fileHandle != -1) {

         for (i=0; i<Ncoins; i++) {
            iFileStringRead(fileHandle,coordinates);
            iStringMid(coordinateX,coordinates,0,iStringFind(coordinates,",",0,true));
            iStringMid(coordinates,coordinates,iStringFind(coordinates,",",0,true)+1,iStringLen(coordinates));
            coins[i].x=iStringVal(coordinateX);
            iStringMid(coordinateX,coordinates,0,iStringFind(coordinates,",",0,true));
            iStringMid(coordinates,coordinates,iStringFind(coordinates,",",0,true)+1,iStringLen(coordinates));
            coins[i].y=iStringVal(coordinateX);
            coins[i].z=iStringVal(coordinates);
            collected[i]=false;
            iObjectImposterHide(OBJ_22,i);
            iObjectImposterSet(OBJ_22,i,coinsOrientation,coins[i]);
         }

         iFileClose(fileHandle);
      }

   }

   iObjectLocation(OBJ_0,playerLoc);

   for (i=0; i<Ncoins; i++) {
      if (!collected[i]) {
         if (iSphereVisible(OBJ_66,coins[i],coinsRadius)) iObjectImposterShow(OBJ_22,i); else iObjectImposterHide(OBJ_22,i);
         if (iSphereSegmentIntersect(coins[i],coinsRadius,Vector3(playerLoc.x,playerLoc.y+playerFeet,playerLoc.z),Vector3(playerLoc.x,playerLoc.y+playerHead,playerLoc.z),intersectionPoint1,intersectionPoint2)>0) {
            iObjectImposterHide(OBJ_22,i);
            OUT_44=IN_44+pointsPerCoin;
            for (j=4; j<iObjectHandle(-1); j++) {
               iObjectLocationSet(iObjectHandle(j),coins[i]);
               iObjectStart(iObjectHandle(j));
            }
            collected[i]=true;
         }
      }
   }


}
« Last Edit: July 29, 2010, 04:25:34 AM by Daniel Cremers »
Logged
Visit my website with > 2.000 free flash games! GamesAtNight
Want to start making games? Read this: 3d game development

Filip Macedonia

  • Full Member
  • ***
  • Posts: 110
Re: Coin collect script
« Reply #1 on: July 30, 2010, 06:28:02 AM »
There it is.. I have put the coins.txt in the 3D Rad folder as explained and still doesn't work...  ???
« Last Edit: July 30, 2010, 07:08:34 AM by Filip Macedonia »
Logged
Using : 3D Rad V6.49

filipmacedonia@gmail.com

loop

  • 3D Rad Guru
  • *****
  • Posts: 1232
Re: Coin collect script
« Reply #2 on: July 30, 2010, 06:52:06 AM »
Im sorry but you didnt follow the instructions.
you ... mmm .... innovated  ;D so you broke the script and it didnt wanted to work for you.

line 64:
filePath = localFolder +"3DRad\\coins.txt";

WT :-X ?!

that line, in the original script says
filePath = coinsFile;

and guess what, that works just fine.

try again.
Logged
Oops my CD just skipped!