Advertisement
Guest User

SUPAPOWAH

a guest
Apr 20th, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.52 KB | None | 0 0
  1. Scene@ scene_;
  2. Node@ cameraNode;
  3. Node@ playerNode;
  4. int zombie_spawn_time = 333;
  5. int maxZombies = 300;
  6. int numZombies = 0;
  7. float zombie_agrDist = 25;
  8. bool drawDebug = false;
  9. Text@ uiHealth;
  10. Text@ uiZombies;
  11. bool dead = false;
  12. bool fpscap = false;
  13. float charge = 3;
  14. Text@ uiCharge;
  15. Node@ wpNode;
  16. bool ok = true;
  17. int score = 0;
  18. Text@ uiScore;
  19. float health = 100;
  20. int maxChain = 0;
  21. int chainTimer = 0;
  22. Text@ uiChain;
  23. Text@ helpText;
  24. int helpTimer = 800;
  25. Material@ powmat;
  26. Sprite@ splash;
  27. int splashtimer = 400;
  28.  
  29. void Start()
  30. {  
  31.     CreateConsoleAndDebugHud();
  32.    
  33.     scene_ = Scene();
  34.    
  35.     initStuff();
  36.    
  37.     SubscribeToEvent("KeyDown", "HandleKeyDown");
  38.     SubscribeToEvent("PostRenderUpdate", "HandlePostRenderUpdate");
  39.    
  40.     powmat = cache.GetResource("Material", "Materials/pow.xml");
  41.    
  42.     ValueAnimation@ ColorAnimation = ValueAnimation();
  43.        
  44.     ColorAnimation.SetKeyFrame(0.0f, Variant(Color(0.6, 0.8, 1, 1)));
  45.     ColorAnimation.SetKeyFrame(0.2f, Variant(Color(0.0f, 0.1f, 0.3f, 1.0f)));
  46.        
  47.     powmat.SetShaderParameterAnimation("MatDiffColor", ColorAnimation);
  48.    
  49.      Texture2D@ splashTex = cache.GetResource("Texture2D", "Textures/splash.png");
  50.    
  51.     splash = Sprite();
  52.     splash.texture = splashTex;
  53.     splash.size = IntVector2(1024,512);
  54.     splash.hotSpot = IntVector2(512, 512);
  55.     splash.verticalAlignment = VA_CENTER;
  56.     splash.horizontalAlignment = HA_CENTER;
  57.     ui.root.AddChild(splash);
  58.    
  59.     uiHealth = ui.root.CreateChild("Text");
  60.     uiHealth.text = "100";
  61.     uiHealth.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 75);
  62.     uiHealth.SetPosition(-20, -20);
  63.     uiHealth.horizontalAlignment = HA_RIGHT;
  64.     uiHealth.verticalAlignment = VA_BOTTOM;
  65.    
  66.     Text@ uiHealth2 = ui.root.CreateChild("Text");
  67.     uiHealth2.text = "Health";
  68.     uiHealth2.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 25);
  69.     uiHealth2.SetPosition(-20, -120);
  70.     uiHealth2.horizontalAlignment = HA_RIGHT;
  71.     uiHealth2.verticalAlignment = VA_BOTTOM;
  72.        
  73.     uiZombies = ui.root.CreateChild("Text");
  74.     uiZombies.text = "0";
  75.     uiZombies.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 75);
  76.     uiZombies.SetPosition(20, -20);
  77.     uiZombies.horizontalAlignment = HA_LEFT;
  78.     uiZombies.verticalAlignment = VA_BOTTOM;
  79.    
  80.     Text@ uiZombies2 = ui.root.CreateChild("Text");
  81.     uiZombies2.text = "Zombies";
  82.     uiZombies2.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 25);
  83.     uiZombies2.SetPosition(20, -120);
  84.     uiZombies2.horizontalAlignment = HA_LEFT;
  85.     uiZombies2.verticalAlignment = VA_BOTTOM;
  86.    
  87.     Text@ uiGamover = ui.root.CreateChild("Text");
  88.     uiGamover.text = "You are DEAD!";
  89.     uiGamover.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 125);
  90.     uiGamover.horizontalAlignment = HA_CENTER;
  91.     uiGamover.verticalAlignment = VA_CENTER;
  92.     uiGamover.color = RED;
  93.     uiGamover.visible = false;
  94.     uiGamover.name = "TextGamover";
  95.    
  96.     Text@ uiGamover2 = ui.root.CreateChild("Text");
  97.     uiGamover2.text = "Press F5 to restart.";
  98.     uiGamover2.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 35);
  99.     uiGamover2.horizontalAlignment = HA_CENTER;
  100.     uiGamover2.verticalAlignment = VA_CENTER;
  101.     uiGamover2.SetPosition(0,100);
  102.     uiGamover2.color = RED;
  103.     uiGamover2.visible = false;
  104.     uiGamover2.name = "TextGamover2";
  105.    
  106.     Text@ crosshair = ui.root.CreateChild("Text");
  107.     crosshair.text = "+";
  108.     crosshair.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 25);
  109.     crosshair.horizontalAlignment = HA_CENTER;
  110.     crosshair.verticalAlignment = VA_CENTER;
  111.     crosshair.color = Color(1,1,1,0.5);
  112.    
  113.  
  114.     uiCharge = ui.root.CreateChild("Text");
  115.     uiCharge.text = "3";
  116.     uiCharge.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 75);
  117.     uiCharge.SetPosition(300, -20);
  118.     uiCharge.horizontalAlignment = HA_LEFT;
  119.     uiCharge.verticalAlignment = VA_BOTTOM;
  120.    
  121.    
  122.     Text@ uiCharge2 = ui.root.CreateChild("Text");
  123.     uiCharge2.text = "Charge";
  124.     uiCharge2.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 25);
  125.     uiCharge2.SetPosition(300, -120);
  126.     uiCharge2.horizontalAlignment = HA_LEFT;
  127.     uiCharge2.verticalAlignment = VA_BOTTOM;
  128.    
  129.     uiScore = ui.root.CreateChild("Text");
  130.     uiScore.text = "0";
  131.     uiScore.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 75);
  132.     uiScore.SetPosition(-20, 20);
  133.     uiScore.horizontalAlignment = HA_RIGHT;
  134.     uiScore.verticalAlignment = VA_TOP;
  135.    
  136.    
  137.     Text@ uiScore2 = ui.root.CreateChild("Text");
  138.     uiScore2.text = "Score";
  139.     uiScore2.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 25);
  140.     uiScore2.SetPosition(-20, 120);
  141.     uiScore2.horizontalAlignment = HA_RIGHT;
  142.     uiScore2.verticalAlignment = VA_TOP;
  143.    
  144.     uiChain = ui.root.CreateChild("Text");
  145.     uiChain.text = "";
  146.     uiChain.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 75);
  147.     uiChain.horizontalAlignment = HA_CENTER;
  148.     uiChain.verticalAlignment = VA_TOP;
  149.     uiChain.SetPosition(0,100);
  150.     uiChain.color = YELLOW;
  151.    
  152.     helpText = ui.root.CreateChild("Text");
  153.     helpText.text =
  154.                     "* Your weapon charges only when zombies are nearby.\n"
  155.                     "* Gather a hoard, then kill em with one shot, to get\n"
  156.                     "more score points and restore health.\n"
  157.                     "* Hop uphill to gain momentum. Move straight to keep it.\n"
  158.                     "Controls:\n"
  159.                     "SPACE - show help\n"
  160.                     "WASD - hop around\n"
  161.                     "LMB - shoot the zapper\n"
  162.                     "F5 - restart\n"
  163.                     "F1 - console (nothing to see here)\n"
  164.                     "F2 - show profiler\n"
  165.                     "F3 - enable SUPAHAKER vision\n"
  166.                     "F9 - spawn more zombies\n"
  167.                     "F12 - take screenshot\n";
  168.    
  169.     helpText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  170.     helpText.horizontalAlignment = HA_CENTER;
  171.     helpText.verticalAlignment = VA_CENTER;
  172.     helpText.SetPosition(0,200);
  173.     helpText.color = YELLOW;
  174.    
  175.    
  176.    
  177.    
  178.    
  179. }
  180.  
  181. void initStuff()
  182. {
  183.     scene_.LoadXML(cache.GetFile("Scenes/scene_1.xml"));
  184.    
  185.     //scene_.CreateComponent("PhysicsWorld");
  186.    
  187.     cameraNode = Node();
  188.     Camera@ camera = cameraNode.CreateComponent("Camera");
  189.     renderer.viewports[0] = Viewport(scene_, camera);
  190.    
  191.     camera.fov = 80.0f;
  192.    
  193.     audio.listener = cameraNode.CreateComponent("SoundListener");
  194.    
  195.     //cameraNode.position = Vector3(0,1,-10);
  196.    
  197.     Node@ SpawnerNode = scene_.CreateChild("spawner");
  198.     Spawner@ spawner = cast<Spawner>(SpawnerNode.CreateScriptObject(scriptFile, "Spawner"));
  199.     spawner.Init();
  200.    
  201.     scene_.physicsWorld.gravity = Vector3( 0 , -9.8 * 2 , 0 );
  202.    
  203.     playerNode = scene_.CreateChild("player");
  204.     playerNode.position = Vector3(0,6,0);
  205.     //playerNode.rotation = Quaternion(180, Vector3(0,1,0));
  206.     Player@ player = cast<Player>(playerNode.CreateScriptObject(scriptFile, "Player"));
  207.     player.Init();
  208.    
  209.     engine.maxFps = 60;
  210. }
  211.  
  212. void Restart()
  213. {
  214.     scene_.RemoveAllChildren();
  215.     initStuff();
  216.     dead = false;
  217.     ui.root.GetChild("TextGamover").visible = false;
  218.     ui.root.GetChild("TextGamover2").visible = false;
  219.    
  220.     charge = 3;
  221.     numZombies = 0;
  222.     ok = true;
  223.     health = 100;
  224. }
  225.  
  226. void CreateConsoleAndDebugHud()
  227. {
  228.     // Get default style
  229.     XMLFile@ xmlFile = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  230.     if (xmlFile is null)
  231.         return;
  232.  
  233.     // Create console
  234.     Console@ console = engine.CreateConsole();
  235.     console.defaultStyle = xmlFile;
  236.     console.background.opacity = 0.8f;
  237.  
  238.     // Create debug HUD
  239.     DebugHud@ debugHud = engine.CreateDebugHud();
  240.     debugHud.defaultStyle = xmlFile;
  241. }
  242.  
  243.  
  244. void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  245. {
  246.     int key = eventData["Key"].GetInt();
  247.  
  248.     // Close console (if open) or exit when ESC is pressed
  249.     if (key == KEY_ESC)
  250.     {
  251.         if (!console.visible)
  252.             engine.Exit();
  253.         else
  254.             console.visible = false;
  255.     }
  256.  
  257.     // Toggle console with F1
  258.     else if (key == KEY_F1)
  259.         console.Toggle();
  260.  
  261.     // Toggle debug HUD with F2
  262.     else if (key == KEY_F2)
  263.         debugHud.ToggleAll();
  264.  
  265.     // Take screenshot
  266.     else if (key == KEY_F12)
  267.         {
  268.             Image@ screenshot = Image();
  269.             graphics.TakeScreenShot(screenshot);
  270.             // Here we save in the Data folder with date and time appended
  271.             screenshot.SavePNG(fileSystem.programDir + "Data/Screenshot_" +
  272.                 time.timeStamp.Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
  273.         }
  274.     // Toggle physics debug
  275.     else if (key == KEY_F3)
  276.         drawDebug = !drawDebug;
  277.        
  278.     else if (key == KEY_F5) Restart();
  279.     else if (key == KEY_SPACE)
  280.     {
  281.         helpText.visible = true;
  282.         helpTimer = 300;
  283.     }
  284.  
  285. }
  286.  
  287. void HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  288. {
  289.     // If draw debug mode is enabled, draw physics debug geometry. Use depth test to make the result easier to interpret
  290.     // Note the convenience accessor to the physics world component
  291.     if (drawDebug)
  292.     scene_.physicsWorld.DrawDebugGeometry(true);
  293.    
  294.     uiZombies.text = numZombies;
  295.     uiScore.text = score;
  296. }
  297.  
  298. class Player : ScriptObject
  299. {
  300.     RigidBody@ body;
  301.     Vector3 dir = Vector3(0,0,1);
  302.     Vector2 walk = Vector2(0,0);
  303.     float speed = 30;
  304.     float jump = 40;
  305.     int jumpTime = 10;
  306.     int jumpTimer = 0;
  307.     Vector2 mouseInput = Vector2(180,0);
  308.     float sens = 0.2;
  309.     int stunTime = 60;
  310.     int stunTimer = 62;
  311.    
  312.    
  313.     void Init()
  314.     {
  315.         StaticModel@ plModel = node.CreateComponent("StaticModel");
  316.         plModel.model = cache.GetResource("Model", "Models/hatman.mdl");
  317.         plModel.material = cache.GetResource("Material", "Materials/hatman.xml");
  318.         plModel.castShadows = true;
  319.        
  320.         body = node.CreateComponent("RigidBody");
  321.         CollisionShape@ ColShape = node.CreateComponent("CollisionShape");
  322.         ColShape.SetCapsule(1,2);
  323.         body.mass = 10;
  324.         body.linearDamping = 0.4;
  325.         body.angularFactor = Vector3(0.0f, 0.0f, 0.0f);
  326.         body.collisionLayer = 2;
  327.        
  328.         node.AddChild(cameraNode);
  329.         cameraNode.position = Vector3(0,0.6,0);
  330.        
  331.         wpNode = playerNode.CreateChild('wpNode');
  332.         StaticModel@ wpMpdel = wpNode.CreateComponent("StaticModel");
  333.         wpMpdel.model = cache.GetResource("Model", "Models/zapper.mdl");
  334.         wpMpdel.material = cache.GetResource("Material", "Materials/zapper.xml");
  335.         wpMpdel.castShadows = true;
  336.         wpNode.position = Vector3(0.5,0,-0.1);
  337.        
  338.     }
  339.    
  340.     void Update(float timeStep)
  341.     {
  342.         if (health > 0 && stunTimer>stunTime)
  343.         {
  344.             //body.rotation = Quaternion(Vector3(0,0,-1), Vector3(dir.x,0,dir.z));
  345.             body.angularVelocity = Vector3(0,0,0);
  346.            
  347.             walk = Vector2(0,0);
  348.             if (input.keyDown['w']) walk.y = -1;
  349.             if (input.keyDown['a']) walk.x = 1;
  350.             if (input.keyDown['d']) walk.x = -1;
  351.             if (input.keyDown['s']) walk.y = 1;
  352.             walk.Normalize();
  353.            
  354.             mouseInput.x += input.mouseMoveX * sens;
  355.             mouseInput.y += input.mouseMoveY * sens;
  356.             mouseInput.y  = Clamp( mouseInput.y , -90.0f, 90.0f);
  357.            
  358.             cameraNode.rotation = Quaternion(mouseInput.y , Vector3(1,0,0));
  359.             //cameraNode.rotation = cameraNode.rotation * Quaternion(mouseInput.x , Vector3(0,1,0));
  360.             body.rotation = Quaternion(mouseInput.x , Vector3(0,1,0));
  361.            
  362.             if (charge>3) charge = 3;
  363.            
  364.             if (charge < 1)
  365.             {
  366.                 uiCharge.color = RED;
  367.                 ok = false;
  368.             }
  369.             else if (!ok)
  370.             {
  371.                     uiCharge.color = WHITE;
  372.                    
  373.                     Sound@ sound = cache.GetResource("Sound", "Sounds/ok.wav");
  374.                     SoundSource@ soundSource = node.CreateComponent("SoundSource");
  375.                     soundSource.Play(sound);
  376.                     soundSource.gain = 0.7f;
  377.                     soundSource.autoRemove = true;
  378.                    
  379.                     ok = true;
  380.                    
  381.             }
  382.                
  383.             uiCharge.text = Floor(charge * 1000)/1000;
  384.            
  385.             wpNode.rotation = cameraNode.rotation;
  386.             wpNode.position = Vector3(0.5,body.linearVelocity.y * -0.006,-0.1);
  387.         }
  388.        
  389.        
  390.     }
  391.    
  392.     void PostUpdate(float timeStep)
  393.     {
  394.         //body.rotation = Quaternion(mouseInput.x , Vector3(0,1,0));
  395.         //body.angularVelocity = Vector3(0,0,0);
  396.         //cameraNode.position = node.position + node.up * 0.6f;
  397.        
  398.     }
  399.    
  400.     void FixedUpdate(float timeStep)
  401.     {
  402.         if (health > 0)
  403.         {
  404.             if (stunTimer>stunTime)
  405.             {
  406.                 //Quaternion rot = cameraNode.rotation;
  407.                 //body.rotation = Quaternion(rot.yaw, Vector3(0,1,0));
  408.                 body.angularVelocity = Vector3(0,0,0);
  409.                 body.angularFactor = Vector3(0.0f, 0.0f, 0.0f);
  410.                
  411.                 if (input.mouseButtonPress[MOUSEB_LEFT] && charge>=1) ThrowWeapon();
  412.                
  413.             } else {
  414.                 body.angularFactor = Vector3(1.0f, 1.0f, 1.0f);
  415.             }
  416.            
  417.             if(body.collidingBodies.length>0)
  418.             {
  419.            
  420.                 if (jumpTimer>jumpTime && stunTimer>stunTime)
  421.                 {
  422.                     jumpTimer = 0;
  423.                     if (walk.length>0.1f)
  424.                     {
  425.                         //body.linearVelocity = Vector3(0,0,0);
  426.                         body.ApplyImpulse(body.rotation * Vector3(-1 * speed * walk.x , jump , -1 *  speed * walk.y));
  427.                        
  428.                         //Audio: PCM 44100Hz mono 705kbps [output]
  429.                         Sound@ sound = cache.GetResource("Sound", "Sounds/step.wav");
  430.                         SoundSource@ soundSource = node.CreateComponent("SoundSource");
  431.                         soundSource.Play(sound);
  432.                         soundSource.gain = 0.3f;
  433.                         soundSource.autoRemove = true;
  434.                     }
  435.                 }
  436.                
  437.                 for (uint i=0; i<body.collidingBodies.length ; i++)
  438.                 {
  439.                     RigidBody@ body2 = body.collidingBodies[i];
  440.                    
  441.                     if (body2.node.name == "Zombie" && stunTimer>stunTime)
  442.                     {
  443.                         Zombie@ obj = cast<Zombie>(body2.node.GetScriptObject("Zombie"));
  444.                         if (obj.dead == false)
  445.                         {
  446.                             Vector3 vec = node.position - body2.node.position;
  447.                             vec.Normalize();
  448.                             stunTimer = 0;
  449.                             health = health - 15;
  450.                             charge = charge + 0.5;
  451.                             body.ApplyImpulse(Vector3(vec.x * 150, vec.y * 30 +20, vec.z * 150),Vector3(Random(-0.2,0.2),0.003,Random(-0.2,0.2)));
  452.                             //dir = node.rotation * Vector3(0,0,1);
  453.                             mouseInput = Vector2(0,0);
  454.                            
  455.                             Sound@ sound = cache.GetResource("Sound", "Sounds/hit.wav");
  456.                             SoundSource@ soundSource = node.CreateComponent("SoundSource");
  457.                             soundSource.Play(sound);
  458.                             soundSource.gain = 0.7f;
  459.                             soundSource.autoRemove = true;
  460.                        
  461.                         }
  462.                     }
  463.                 }
  464.                
  465.                
  466.             }
  467.             uiHealth.text = Floor(health);
  468.             if (health<30) uiHealth.color = RED;
  469.             else if (health<50) uiHealth.color = Color(1,0.5,0);
  470.             else if (health<80) uiHealth.color = YELLOW;
  471.             else uiHealth.color = WHITE;
  472.  
  473.            
  474.             //if (health < 100) health = health + 0.01;
  475.            
  476.            
  477.             if (maxChain > 5)
  478.             {
  479.                 uiChain.text = maxChain + "x chain!";
  480.                 chainTimer++;
  481.                 if (chainTimer > 120)
  482.                 {
  483.                     maxChain = 0;
  484.                     uiChain.text = "";
  485.                 }
  486.             }
  487.            
  488.            
  489.         } else if (!dead) {
  490.             ui.root.GetChild("TextGamover").visible = true;
  491.             ui.root.GetChild("TextGamover2").visible = true;
  492.             dead = true;
  493.             body.angularFactor = Vector3(1.0f, 1.0f, 1.0f);
  494.            
  495.             body.friction = 2;
  496.             body.angularDamping = 0.7;
  497.            
  498.             Sound@ sound = cache.GetResource("Sound", "Sounds/death.wav");
  499.             SoundSource@ soundSource = node.CreateComponent("SoundSource");
  500.             soundSource.Play(sound);
  501.             soundSource.gain = 0.7f;
  502.             soundSource.autoRemove = true;
  503.         }
  504.         jumpTimer++;
  505.         stunTimer++;
  506.        
  507.         if (node.position.y<-10) health = -1;
  508.        
  509.         helpTimer--;
  510.         if (helpTimer<1) helpText.visible = false;
  511.         splashtimer--;
  512.         if (splashtimer<1) splash.visible = false;
  513.     }
  514.    
  515.     void ThrowWeapon()
  516.     {
  517.         Vector3 camdir = cameraNode.worldRotation * Vector3(0,0,1);
  518.         PhysicsRaycastResult result = scene_.physicsWorld.RaycastSingle(Ray(cameraNode.worldPosition, camdir),50,1);
  519.         if (result.body !is null)
  520.             {
  521.  
  522.             if (result.body.node.name == "Zombie")
  523.             {
  524.  
  525.                 result.body.ApplyImpulse(result.normal * -200,result.position - result.body.position);
  526.                 Zombie@ obj = cast<Zombie>(result.body.node.GetScriptObject("Zombie"));
  527.                 obj.dead = true;
  528.                 obj.PowaTimer = 0;
  529.                 obj.DeadTimer = 0;
  530.                 charge--;
  531.                
  532.                 Node@ beamNode = scene_.CreateChild('beam');
  533.                 Beam@ beam = cast<Beam>(beamNode.CreateScriptObject(scriptFile, "Beam"));
  534.                 beam.target = result.body.node;
  535.                 //beam.tgPos = result.position - result.body.node.position;
  536.                 beam.Init();
  537.                
  538.                 Sound@ sound = cache.GetResource("Sound", "Sounds/shot.wav");
  539.                 SoundSource@ soundSource = node.CreateComponent("SoundSource");
  540.                 soundSource.Play(sound);
  541.                 soundSource.gain = 0.7f;
  542.                 soundSource.autoRemove = true;
  543.             }
  544.         }
  545.        
  546.     }
  547.    
  548. }
  549.  
  550. class Beam : ScriptObject
  551. {
  552.     Node@ target;
  553.     //Vector3 tgPos;
  554.     int ttl = 30;
  555.    
  556.     void Init()
  557.     {
  558.         StaticModel@ beamModel = node.CreateComponent("StaticModel");
  559.         beamModel.model = cache.GetResource("Model", "Models/beam.mdl");
  560.        
  561.         beamModel.material = powmat;
  562.        
  563.         Vector3 tgVec = target.worldPosition - node.worldPosition;
  564.         node.rotation = Quaternion(Vector3(0,0,1) , tgVec.Normalized());
  565.        
  566.         node.Roll(Random(360));
  567.        
  568.         node.position = wpNode.worldPosition + (wpNode.worldRotation * Vector3(0,0,1) );
  569.        
  570.     }
  571.    
  572.     void Update(float timeStep)
  573.     {
  574.         Vector3 tgVec = target.worldPosition - node.worldPosition;
  575.        
  576.         node.rotation = Quaternion(Vector3(0,0,1) , tgVec.Normalized());
  577.        
  578.         node.Roll(Random(360));
  579.        
  580.         node.position = wpNode.worldPosition + (wpNode.worldRotation * Vector3(0,0,1) );
  581.        
  582.         node.scale = Vector3((35-ttl)/20.0f,(35-ttl)/20.0f,tgVec.length/5); //
  583.        
  584.        
  585.     }
  586.    
  587.     void FixedUpdate(float timeStep)
  588.     {
  589.         ttl--;
  590.        
  591.         if (ttl<1) node.Remove();
  592.     }
  593.    
  594. }
  595.  
  596. class Zombie : ScriptObject
  597. {
  598.     RigidBody@ Zbody;
  599.     Vector2 dir = Vector2(1,0);
  600.     float speed = 20;
  601.     float jump = 50;
  602.     float roam = 0.6;
  603.     int jumpTime = 50;
  604.     int jumpTimer = 0;
  605.     bool dead = false;
  606.     int DeadTime = 800;
  607.     int DeadTimer = 0;
  608.     Node@ powanode;
  609.     int PowaTime = 300;
  610.     int PowaTimer = 0;
  611.     int chain = 1;
  612.    
  613.    
  614.     void Init()
  615.     {
  616.         StaticModel@ zombieModel = node.CreateComponent("StaticModel");
  617.         zombieModel.model = cache.GetResource("Model", "Models/capsule.mdl");
  618.         zombieModel.material = cache.GetResource("Material", "Materials/zombie.xml");
  619.         zombieModel.castShadows = true;
  620.        
  621.         Zbody = node.CreateComponent("RigidBody");
  622.         CollisionShape@ ColShape = node.CreateComponent("CollisionShape");
  623.         ColShape.SetCapsule(1,2);
  624.         Zbody.mass = 10;
  625.         Zbody.linearDamping = 0.6;
  626.         Zbody.angularFactor = Vector3(1,0.3,1);
  627.        
  628.         dir = Vector2(1-2*Random(),1-2*Random());
  629.         dir.Normalize();
  630.         powanode = node.CreateChild();
  631.        
  632.         StaticModel@ powaModel = powanode.CreateComponent("StaticModel");
  633.         powaModel.model = cache.GetResource("Model", "Models/pow.mdl");
  634.         powaModel.material = powmat;
  635.         powanode.enabled = false;
  636.        
  637.     }
  638.    
  639.     void Update()
  640.     {
  641.        
  642.     }
  643.    
  644.     void FixedUpdate(float timeStep)
  645.     {
  646.         if (node.position.y<-10) Remove();
  647.        
  648.         if (!dead){
  649.            
  650.            
  651.             Zbody.rotation = Quaternion(Vector3(0,0,-1), Vector3(dir.x,0,dir.y));
  652.            
  653.            
  654.            
  655.             Zbody.angularVelocity = Vector3(0,0,0);
  656.            
  657.            
  658.            
  659.             if(Zbody.collidingBodies.length>0 && jumpTimer>jumpTime)
  660.             {
  661.                 jumpTimer = 0;
  662.                 Zbody.ApplyImpulse(Vector3(speed * dir.x , jump , speed * dir.y));
  663.                
  664.                     Sound@ sound = cache.GetResource("Sound", "Sounds/zombStep.wav");
  665.                     SoundSource3D @ soundSource = node.CreateComponent("SoundSource3D");
  666.                     soundSource.gain = 0.2f;
  667.                     soundSource.SetDistanceAttenuation(2, 15, 1);
  668.                     soundSource.Play(sound);
  669.                     soundSource.autoRemove = true;
  670.                
  671.                 Vector3 playerVec = node.position - playerNode.position;
  672.                
  673.                 if (playerVec.length < zombie_agrDist)
  674.                 {
  675.                     dir = Vector2(-1 * playerVec.x, -1 * playerVec.z);
  676.                     dir.Normalize();
  677.                     speed = 40;
  678.                     jump = 35;
  679.                     jumpTime = 20;
  680.                     roam = 0.3;
  681.                     charge = charge + 0.01;
  682.                 } else {
  683.                    
  684.                     speed = 20;
  685.                     jump = 50;
  686.                     roam = 0.6;
  687.                     jumpTime = 50;
  688.                 }
  689.                
  690.                 dir = Vector2(dir.x + Random(-1*roam,roam),dir.y + Random(-1*roam,roam));
  691.                 dir.Normalize();
  692.                
  693.             }
  694.        
  695.         } else {
  696.             DeadTimer++;
  697.             if (DeadTimer > DeadTime) Remove();
  698.            
  699.             PowaTimer++;
  700.             if (PowaTimer < PowaTime)
  701.             {
  702.                 powanode.enabled = true;
  703.                 powanode.rotation = Quaternion(Random(360),Vector3(0,1,0));
  704.                 Vector3 randomVec = Vector3(Random(-1,1),0,Random(-1,1));
  705.                 randomVec.Normalize();
  706.                 Zbody.ApplyImpulse(randomVec * Random(0,10), Vector3(0,Random(-2,2),0));
  707.                
  708.                 if(Zbody.collidingBodies.length>0)
  709.                 {
  710.                     for (uint i=0; i<Zbody.collidingBodies.length ; i++)
  711.                     {
  712.                     RigidBody@ body2 = Zbody.collidingBodies[i];
  713.                    
  714.                         if (body2.node.name == "Zombie")
  715.                         {
  716.                             Zombie@ obj = cast<Zombie>(body2.node.GetScriptObject("Zombie"));
  717.                             Vector3 vec = Zbody.position - body2.position;
  718.                             body2.ApplyImpulse(vec * -20, Vector3(0,Random(-2,2),0));
  719.                                                        
  720.                             Sound@ sound = cache.GetResource("Sound", "Sounds/pow.wav");
  721.                             SoundSource3D @ soundSource = node.CreateComponent("SoundSource3D");
  722.                             soundSource.gain = 0.2f;
  723.                             soundSource.SetDistanceAttenuation(2, 30, 1);
  724.                             soundSource.Play(sound);
  725.                             soundSource.autoRemove = true;
  726.                             if (!obj.dead)
  727.                             {
  728.                                 obj.chain = chain + 1;
  729.                                 score = score + chain * (DeadTimer +10);
  730.                                 if (health < 100) health = health + chain/4;
  731.                                 obj.dead = true;
  732.                                
  733.                                 if (chain > maxChain)
  734.                                 {
  735.                                     maxChain = chain;
  736.                                     chainTimer = 0;
  737.                                 }
  738.                             }
  739.                         }
  740.                     }
  741.                 }
  742.                
  743.             } else {
  744.                 powanode.enabled = false;
  745.             }
  746.         }
  747.         jumpTimer++;       
  748.  
  749.     }
  750.    
  751.  
  752.     void Remove()
  753.     {
  754.         numZombies--;
  755.         node.Remove();
  756.     }
  757.    
  758. }
  759.  
  760. class Spawner : ScriptObject
  761. {
  762.     int zombie_timer;
  763.     Array<Vector3> spots;
  764.    
  765.     void Init()
  766.     {
  767. //      for (uint i=0; i< scene_.children.length; i++)
  768. //      {
  769. //          Node@ tnode = scene_.children[i];
  770. //          if (tnode.name = "spawner")
  771. //          {
  772. //             
  773. //          }
  774. //      }
  775.     }
  776.    
  777.     void FixedUpdate(float timeStep)
  778.      {
  779.         zombie_timer++;
  780.         if (zombie_timer > zombie_spawn_time){
  781.             if (numZombies<maxZombies)
  782.             {
  783.                 SpawnZombie();
  784.             }
  785.             zombie_timer = 0;
  786.         }
  787.        
  788.         if (input.keyPress[KEY_F9])SpawnZombie();
  789.      }
  790.    
  791.     void SpawnZombie()
  792.     {
  793.        
  794.         Node@ zombieNode = scene_.CreateChild("Zombie");
  795.         zombieNode.position = node.position;
  796.         Zombie@ zombie = cast<Zombie>(zombieNode.CreateScriptObject(scriptFile, "Zombie"));
  797.         zombie.Init();
  798.         numZombies++;
  799.        
  800.     }
  801. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement