Guest User

Untitled

a guest
Jul 28th, 2012
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.     Some 'Simon' type game thing for Garry's Mod.
  3.     Created by Chessnut.
  4.  
  5.     Type 'pressbtn' in console to play.
  6. --]]
  7.  
  8. local currentCount = 0;
  9. local level = 1;
  10. local inGame = false;
  11. local pressTable = {};
  12. local BUTTON_RED = 1;
  13. local BUTTON_YELLOW = 2;
  14. local BUTTON_GREEN = 3;
  15. local BUTTON_BLUE = 4;
  16. local redButton, yellowButton, greenButton, blueButton;
  17. local DermaPanel;
  18. local initButtons = false;
  19.  
  20. local function newGame(start)
  21.     if (!inGame) then
  22.         if (start) then
  23.             level = 1;
  24.         end;
  25.        
  26.         DermaPanel:SetTitle("Watch the buttons! Current level: "..level);
  27.        
  28.         initButtons = false;          
  29.         pressTable = {};
  30.         currentCount = 0;
  31.                        
  32.         for i = 1, level do
  33.             timer.Simple(i * 0.5, function()
  34.                 local value = math.random(1, 4);
  35.                                            
  36.                 pressTable[i] = value;
  37.                
  38.                 if (value == 1) then
  39.                     redButton:SimulatePress();
  40.                 elseif (value == 2) then
  41.                     yellowButton:SimulatePress();
  42.                 elseif (value == 3) then
  43.                     greenButton:SimulatePress();
  44.                 elseif (value == 4) then
  45.                     blueButton:SimulatePress();
  46.                 end;
  47.             end);
  48.         end;
  49.        
  50.         timer.Simple(level * 0.6, function()
  51.             inGame = true;
  52.             initButtons = true;
  53.            
  54.             DermaPanel:SetTitle("Alright, your turn!");
  55.         end);
  56.     end;
  57. end;
  58.  
  59. local function createButton(class, left, top, right, bottom, parent)
  60.     local colorClass = {};
  61.     colorClass[BUTTON_RED] = Color(255, 25, 25, 255);
  62.     colorClass[BUTTON_YELLOW] = Color(255, 255, 25, 255);
  63.     colorClass[BUTTON_GREEN] = Color(25, 255, 25, 255);
  64.     colorClass[BUTTON_BLUE] = Color(25, 50, 255, 255);
  65.    
  66.     local button = vgui.Create("DButton", parent);
  67.     button:StretchToParent(left, top, right, bottom);
  68.     button:SetParent(parent);
  69.     button:SetText("");
  70.     button._hovered = false;
  71.    
  72.     function button:OnCursorEntered()
  73.         self._hovered = true;
  74.     end;
  75.    
  76.     function button:OnCursorExited()
  77.         self._hovered = false;
  78.     end;
  79.    
  80.     function button:SimulatePress()
  81.         self:DoClick(true);
  82.     end;
  83.    
  84.     function button:DoClick(simulated)
  85.         if (inGame and initButtons) then    
  86.             LocalPlayer():EmitSound( "buttons/blip1.wav", 100, 100 - (class * 10) );
  87.            
  88.             self.click = CurTime() + 0.05;
  89.  
  90.             RunConsoleCommand("pressbtn", class);
  91.         elseif (simulated) then
  92.             LocalPlayer():EmitSound( "buttons/blip1.wav", 100, 100 - (class * 10) );
  93.            
  94.             self.click = CurTime() + 0.05;
  95.         end;
  96.     end;
  97.    
  98.     function button:Paint()        
  99.         local buttonColor = colorClass[class];
  100.        
  101.         if ( (self.click or 0) > CurTime() ) then
  102.             buttonColor = Color(255, 255, 255, 255);
  103.         end;    
  104.        
  105.         surface.SetDrawColor(buttonColor);
  106.         surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() );
  107.     end;
  108.    
  109.     return button;
  110. end;
  111.  
  112. local function createInterfact()
  113.     if ( DermaPanel ) then
  114.         DermaPanel:Remove();
  115.     end;
  116.    
  117.     DermaPanel = vgui.Create("DFrame")
  118.     DermaPanel:SetSize(512, 512);
  119.     DermaPanel:SetTitle("Simon Memory Game");
  120.     DermaPanel:SetVisible(true);
  121.     DermaPanel:SetDraggable(true);
  122.     DermaPanel:ShowCloseButton(true);
  123.     DermaPanel:Center();
  124.     DermaPanel:MakePopup();
  125.    
  126.     redButton = createButton(BUTTON_RED, 5, 27, (DermaPanel:GetWide() * 0.5) + 2.5, 258.5, DermaPanel);
  127.     yellowButton = createButton(BUTTON_YELLOW, (DermaPanel:GetWide() * 0.5) + 2.5, 27, 5, 258.5, DermaPanel);
  128.     greenButton = createButton(BUTTON_GREEN, 5, (DermaPanel:GetTall() * 0.5) + 2.5, (DermaPanel:GetWide() * 0.5) + 2.5, 5, DermaPanel);
  129.     blueButton = createButton(BUTTON_BLUE, 258.5, (DermaPanel:GetWide() * 0.5) + 2.5, 5, 5, DermaPanel);
  130. end;
  131.  
  132. concommand.Add("pressbtn", function(p, c, a)
  133.     if (inGame) then
  134.         currentCount = currentCount + 1;
  135.        
  136.         if ( pressTable[currentCount] != tonumber( a[1] ) ) then
  137.             inGame = false;
  138.             initButtons = false;
  139.             DermaPanel:SetTitle("You lose! Starting a new game!");
  140.            
  141.             LocalPlayer():EmitSound("buttons/button10.wav");
  142.            
  143.             timer.Simple(2.5, function()
  144.                 newGame(true);
  145.             end);
  146.         elseif (#pressTable == currentCount) then
  147.             DermaPanel:SetTitle("Nice! Now for the next one.");
  148.            
  149.             inGame = false;
  150.             level = level + 1;
  151.             initButtons = false;
  152.            
  153.             timer.Simple(1, function()
  154.                 newGame();
  155.             end);
  156.         end;
  157.     else
  158.         createInterfact();
  159.         newGame();
  160.     end;
  161. end);
RAW Paste Data