Advertisement
Guest User

lava.cpp

a guest
Oct 29th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. const float PI = 3.141592654;
  2.  
  3. class Lava{
  4.    
  5.     uint colour;
  6.     uint bottomColour;
  7.     float y;
  8.     float amplitude;
  9.     float wavelength;
  10.     float rectWidth;
  11.     float ySpeed;
  12.     float xOffset;
  13.     float xSpeed;
  14.    
  15.     float checkY;
  16.     float checkX;
  17.    
  18.     bool isDead;
  19.    
  20.     int num_players;
  21.    
  22.     array<camera@> cam;
  23.     scene@ g;
  24.     array<dustman@> dm;
  25.    
  26.     Lava(uint colour, uint bottomColour, float y, float amplitude, float wavelength, float rectWidth, float ySpeed, float xOffset, float xSpeed){
  27.         this.colour = colour;
  28.         this.bottomColour = bottomColour;
  29.         this.y = y;
  30.         this.amplitude = amplitude;
  31.         this.wavelength = wavelength;
  32.         this.rectWidth = rectWidth;
  33.         this.ySpeed = ySpeed;
  34.         this.xOffset = xOffset;
  35.         this.xSpeed = xSpeed;
  36.         @g = get_scene();
  37.        
  38.         this.isDead = false;
  39.        
  40.         num_players = num_cameras();
  41.         dm.resize(num_players);
  42.         cam.resize(num_players);
  43.        
  44.         for(int i = 0; i < num_players; ++i){
  45.             @cam[i] = get_camera(i);
  46.         }
  47.     }
  48.    
  49.     array<float> getBounds(){ //returns left, right, bottom, and height, in that order
  50.         camera@ cam = get_active_camera();
  51.         array<float> result = {cam.x(), cam.x(), cam.y(), cam.screen_height()};
  52.        
  53.         return result;
  54.     }
  55.    
  56.     void draw(float sub_frame){
  57.         array<float> bounds = getBounds();
  58.         float left = bounds[0] - bounds[3]; //Should work as long as nobody's using a monitor with a greater than 2:1 aspect ratio
  59.         float right = bounds[1] + bounds[3];
  60.        
  61.         for(float x = floor(left/rectWidth)*rectWidth; x < right+rectWidth; x += rectWidth){
  62.             float top = y + sin((2*PI*(x+(rectWidth/2)) - xOffset)/wavelength)*amplitude;
  63.            
  64.             g.draw_gradient_world(20, 1, x, bounds[2] + bounds[3], x + rectWidth, top, bottomColour, bottomColour, colour, colour);
  65.         }
  66.     }
  67.    
  68.     void kill(){
  69.         for(int i = 0; i < num_players; ++i){
  70.             if(@dm[i] == null){
  71.                 entity@ e = controller_entity(i);
  72.                 if(@e != null){
  73.                     @dm[i] = e.as_dustman();
  74.                 }
  75.             }
  76.             else{
  77.                 if(dm[i].y() - 10 > y + sin((2*PI*dm[i].x() - xOffset)/wavelength)*amplitude && not isDead){
  78.                     g.combo_break_count(g.combo_break_count() + 1);
  79.                     if(num_players == 1){
  80.                         dm[i].kill(false);
  81.                         isDead = true;
  82.                     } else {
  83.                         scriptenemy@ dead_player = create_scriptenemy(NullPlayer());
  84.                         dead_player.x(dm[i].x());
  85.                         dead_player.y(dm[i].y());
  86.                         g.add_entity(dead_player.as_entity());
  87.                         controller_entity(i, dead_player.as_controllable());
  88.                         g.remove_entity(dm[i].as_entity());
  89.                         @dm[i] = null;
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
  95.    
  96.     void step(){
  97.         this.kill();
  98.        
  99.         y -= ySpeed;
  100.         xOffset += xSpeed;
  101.     }
  102.    
  103.     void checkpoint_save(){
  104.         checkY = y;
  105.         checkX = xOffset;
  106.     }
  107.    
  108.     void checkpoint_load(){
  109.         @dm[0] = null;
  110.         isDead = false;
  111.         y = checkY;
  112.         xOffset = checkX;
  113.     }
  114.    
  115. }
  116.  
  117. class NullPlayer : enemy_base
  118. {
  119.     NullPlayer()
  120.     {
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement