Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 11-23-2016, 09:40 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Send a message via Skype™ to Gunderak
GS2 3D Raycaster

There are a few things that need to be fixed/improved, I never fully finished sprite rendering (it sort of works lol).

Feel free to build on this, or play around with it etc.



I had some floor rendering, but it was extremely laggy as I couldn't access the image buffer directly.


Graal Script Code:
function onCreated(){
  
findplayer("Gund").addweapon(this);  //Change to your name
}
//#CLIENTSIDE
function onCreated(){
  
this.maxlooplimit 2000000;
  
sleep(1);
  
this.map = {
    {
4,4,4,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4},
    {
1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
3,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
2,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,2,3,2,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,3,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,3,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,3,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,4,0,0,0,0,0,0,4,1,0,0,0,0,0,0,0,4,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,1,1,1,1,1,2,3,4,5,6,7,8,1,1,1,1,1,1,1,1,1,1,1,1},
  };
  
this.sprites = {
    {
"bomb.png"13},
    {
"door.png"31},
  };
  
this.texturewidth 64;
  
this.700;
  
this.zbuffer = new[this.w];
  
this.500;
  
this.off = {screenwidth/this.w/2screenheight/this.h/2};
  
this.off = {7070};
  
this.mapwidth this.map[0].size();
  
this.mapheight this.map.size();
  
this.posx 4;
  
this.posy 3;
  
this.dirx = -1;
  
this.diry 0;
  
this.planex 0.221;
  
this.planey 0.5;
  
this.time 0;
  
this.steps 0;
  
this.smode = -4;
  
this.wallcheck 8;
  
temp.rotspeed 0.08;
  
this.ntrig = {sin(temp.rotspeed), cos(temp.rotspeed)};
  
this.trig = {sin(-temp.rotspeed), cos(-temp.rotspeed)};
  if(
isobject("3D_R_GUN")){
    (@
"3D_R_GUN").destroy();
  }
  if(
isobject("Gunderak_3D")){
    (@
"Gunderak_3D").destroy();
  }
  
//return;
  
new GuiDrawingPanel("Gunderak_3D"){
    
useownprofile true;
    
profile.fontcolor "#FFFFFF";
    
width thiso.w;
    
height thiso.h;
    
thiso.off[0];
    
thiso.off[1];
    
clearall();
  }
  
onTimeout();
}
function 
sqrt(n) { 
  return 
n^0.5
}
function 
onTimeout(){
  
Gunderak_3D.drawimagestretched(00this.wthis.h/2"gunderak_sky.png"0011);
  
Gunderak_3D.drawimagestretched(0this.h/2this.wthis.h/2"gunderak_floor.png"0011);
  for(
temp.0temp.this.wtemp.++){
    
temp.camerax temp.x/this.-1;
    
temp.rayposx this.posx;
    
temp.rayposy this.posy;
    
temp.raydirx this.dirx this.planex temp.camerax;
    
temp.raydiry this.diry this.planey temp.camerax;
    
temp.mapx int(temp.rayposx);
    
temp.mapy int(temp.rayposy);
    
temp.sidedistx 0;
    
temp.sidedisty 0;
    
temp.deltadistx sqrt(+ (temp.raydiry temp.raydiry)/(temp.raydirx temp.raydirx));
    
temp.deltadisty sqrt(+ (temp.raydirx temp.raydirx)/(temp.raydiry temp.raydiry));
    
temp.perpwalldist 0;
    
temp.stepx 0;
    
temp.stepy 0;
    
temp.hit 0;
    
temp.side 0;
    if(
temp.raydirx 0){
      
temp.stepx = -1;
      
temp.sidedistx = (temp.rayposx temp.mapx)*temp.deltadistx;
    }else{
      
temp.stepx 1;
      
temp.sidedistx = (temp.mapx temp.rayposx) * temp.deltadistx;
    }
    if(
temp.raydiry 0){
      
temp.stepy = -1;
      
temp.sidedisty = (temp.rayposy temp.mapy) * temp.deltadisty;
    }else{
      
temp.stepy 1;
      
temp.sidedisty = (temp.mapy temp.rayposy) * temp.deltadisty;
    }
    
temp.count 0;
    while(
temp.hit == 0){
      
temp.count ++;
      if(
temp.sidedistx temp.sidedisty){
        
temp.sidedistx += temp.deltadistx;
        
temp.mapx += temp.stepx;
        
temp.side 0;
      }else{
        
temp.sidedisty += temp.deltadisty;
        
temp.mapy += temp.stepy;
        
temp.side 1;
      }
      if(
this.map[temp.mapx][temp.mapy] > 0){
        
temp.hit 1;
      }
    }
    if(
temp.side == 0){
      
temp.perpwalldist abs((temp.mapx temp.rayposx + (temp.stepx)/2)/temp.raydirx);
    }else{
      
temp.perpwalldist abs((temp.mapy temp.rayposy + (temp.stepy)/2)/temp.raydiry);
    }
    
temp.lineheight int(this.h/temp.perpwalldist);
    
temp.drawstart = (-temp.lineheight/2) + (this.h/2);
    
temp.drawend = (temp.lineheight/2)  + (this.h/2);
    
temp.this.map[temp.mapx][temp.mapy]-1;
    
temp.wallx 0;
    if(
temp.side == 0){
      
temp.wallx temp.rayposy temp.perpwalldist temp.raydiry;
    }else{
      
temp.wallx temp.rayposx temp.perpwalldist temp.raydirx;
    }
    
temp.wallx -= int(temp.wallx);
    
temp.texx int(temp.wallx *  this.texturewidth);
    if(
temp.side == && temp.raydirx 0){
      
temp.texx this.texturewidth temp.texx 1;
    }
    if(
temp.side == && temp.raydiry 0){
      
temp.texx this.texturewidth temp.texx 1;
    }
    
temp.texx this.texturewidth temp.texx 1;
    
temp.= {111};
    if(
temp.side == 1){
      
temp.= {0.650.650.65};
    }
    
temp.temp.drawend temp.drawstart;
    
Gunderak_3D.drawimagestretched(temp.xtemp.drawstart1temp.h"wolftextures.png"temp.texx+(thiso.texturewidth*temp.t), 01this.texturewidth);
    
this.zbuffer[temp.x] = temp.perpwalldist;
  }
    for(
temp.sprite this.sprites){
      
temp.spi temp.sprite[0];
      
temp.spx temp.sprite[1] - this.posx;
      
temp.spy temp.sprite[2] - this.posy;
      
temp.invdet 1/(this.planex this.diry this.dirx this.planey);
      
temp.transformx temp.invdet * (this.diry temp.spx this.dirx temp.spy);
      
temp.transformy temp.invdet * (-this.planey temp.spx this.planex temp.spy);
      
temp.spritescreenx int((this.w/2) * (temp.transformx temp.transformy));
      
temp.spriteheight abs(int(this.temp.transformy));
      
temp.drawstarty = -temp.spriteheight/this.h/2;
      if(
temp.drawstarty 0){
        
temp.drawstarty 0;
      }
      
temp.drawendy temp.spriteheight/this.h/2;
      if(
temp.drawendy >= this.h){
        
temp.drawendy this.-1;
      }
      
temp.spritewidth abs(int(this.h/temp.transformy));
      
temp.drawstartx = -temp.spritewidth/temp.spritescreenx;
      if(
temp.drawstartx 0){
        
temp.drawstartx 0;
      }
      
temp.drawendx temp.spritewidth/temp.spritescreenx;
      if(
temp.drawendx >= this.w){
        
temp.drawendx this.-1;
      }
      for(
temp.tx temp.drawstartxtemp.tx temp.drawendxtemp.tx ++){
        
temp.texx int(temp.tx - (-temp.spritewidth/temp.spritescreenx) * 32 temp.spritewidth);
        
temp.texx 0;
        
//Buggggs ugh
        
if(temp.transformy && temp.tx && temp.tx this.&& temp.transformy this.zbuffer[temp.tx]){
          
temp.sh temp.drawendy temp.drawstarty;
          
Gunderak_3D.drawimagestretched(temp.txtemp.drawstarty1temp.shtemp.spitemp.texx0132);
        }
      }
    }
  
temp.ms 0.1;
  if(
keydown(1)){
    
temp.olddirx this.dirx;
    
this.dirx this.dirx this.ntrig[1] - this.diry this.ntrig[0];
    
this.diry temp.olddirx this.ntrig[0] + this.diry this.ntrig[1];
    
temp.oldplanex this.planex;
    
this.planex this.planex this.ntrig[1] - this.planey this.ntrig[0];
    
this.planey temp.oldplanex this.ntrig[0] + this.planey this.ntrig[1];
  }
  if(
keydown(3)){
    
temp.olddirx this.dirx;
    
this.dirx this.dirx this.trig[1] - this.diry this.trig[0];
    
this.diry temp.olddirx this.trig[0] + this.diry this.trig[1];
    
temp.oldplanex this.planex;
    
this.planex this.planex this.trig[1] - this.planey this.trig[0];
    
this.planey temp.oldplanex this.trig[0] + this.planey this.trig[1];
  }
  if(
keydown(2)){
    
this.steps += this.smode;
    if(
this.map[int(this.posx this.dirx temp.ms*this.wallcheck)][int(this.posy)] == 0){
       
this.posx -= this.dirx temp.ms;
    }
    if(
this.map[int(this.posx)][int(this.posy this.diry temp.ms*this.wallcheck)] == 0){
      
this.posy -= this.diry temp.ms;
    }
  }
  if(
keydown(0)){
    
this.steps += this.smode;
    if(
this.map[int(this.posx this.dirx temp.ms*this.wallcheck)][int(this.posy)] == 0){
       
this.posx += this.dirx temp.ms;
    }
    if(
this.map[int(this.posx)][int(this.posy this.diry temp.ms*this.wallcheck)] == 0){
      
this.posy += this.diry temp.ms;
    }
  }
  if(
this.steps 10){
    
this.smode = -4;
  }
  if(
this.steps 1){
    
this.smode 4;
  }
  
temp.so int(this.steps);
  new 
GuiBitmapCtrl("3D_R_GUN") {
    
thiso.off[0] + thiso.w/35;
    
thiso.off[1] + thiso.64;
    
bitmaprectangle = {0,0,81,92 thiso.steps};
    
bitmap "gunderak_3d_gun.png";
    
thiso.catchevent(this"onMouseDown""onFireGun");
  }
  
settimer(0.05);
}
function 
onFireGun(){
  
//player.chat = "bang!";

Attached Thumbnails
Click image for larger version

Name:	wolftextures.png
Views:	85
Size:	25.2 KB
ID:	55967  
Attached Images
   
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 08:00 PM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright (C) 1998-2008 Linux cyberjoueurs All Rights Reserved.