Advertisement
Guest User

VeryBasicUnityGameSrc

a guest
Aug 3rd, 2015
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.29 KB | None | 0 0
  1. //=============================================================================================================
  2. // Source for https://www.fgl.com/view_game.php?from=dev&game_id=39196
  3. // you can use it if you want, don't share on the asset store, credits appreciated if you find it useful, xoxo
  4. //-------------------------------------------------------------------------------------------------------------
  5.  
  6. // #has "lolcode.h"
  7. // #has "unity.h"
  8.  
  9. //=============================================================================================================
  10. // PlayerControl.cs
  11. //=============================================================================================================
  12.  
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. using System.Collections;
  16.  
  17. public class PlayerControl : MonoBehaviour {
  18.  
  19.     public int fuel=10000;
  20.     public int baseFuelBonus=10;
  21.  
  22.     public  enum GameMode {Demo,StartGame,Game};
  23.     public static GameMode _gameMode;
  24.     public static PlayerControl instance;
  25.  
  26.     public GameMode gameMode;
  27.  
  28.  
  29.     public Camera cam;
  30.     public Level level;
  31.     public ScrollableGround scrollableGround;
  32.     public GameObject bullet;
  33.     public Transform plane;
  34.     public KeyCode kLeft,kRight,kFire,kDebug1;
  35.     public KeyCode kLeft2,kRight2;
  36.     public float turnSpeed;
  37.     public float Speed;
  38.    
  39.  
  40.     public float bulletSpeed;
  41.  
  42.     private float currentFireDelay;
  43.     public float fireDelay;
  44.     public float maxDistance;
  45.  
  46.  
  47.     public Vector3 camOffset;
  48.     public Vector3 lookatOffset;
  49.     public Vector3 fireOffset;
  50.     public Vector3 startPosition;
  51.  
  52.     public Vector3 planeOrientation= new Vector3(0,0,0);
  53.     public Vector3 nextPlaneOrientation= new Vector3(0,0,0);
  54.  
  55.     private float totalDistance;
  56.     private float currentDistance;
  57.     private float lastPos;
  58.     private float distPerFrame;
  59.     private float turnRatio=0.0f;
  60.     private int posInt;
  61.  
  62.     //-----------------------------------------------------------------------------------------------------------
  63.     void Start () {
  64.         resetCamera();
  65.         level.placeObjects(transform.position.z,maxDistance);
  66.         scrollableGround.placeGroundObjects(transform.position.z);
  67.         instance=this;
  68.     }
  69.    
  70.     //-----------------------------------------------------------------------------------------------------------
  71.     void FixedUpdate () {
  72.    
  73.         _gameMode=gameMode;
  74.  
  75.         if ( (Input.GetKey(kFire) || (Input.GetMouseButtonDown(0) || (Input.GetMouseButtonDown(1)) ) ) ) {  
  76.             if (gameMode!=GameMode.Game) { gameMode=GameMode.StartGame; }
  77.         }
  78.  
  79.  
  80.         //if (Input.GetKey(kDebug1)) {  LevelGeometry.instance.placeObjects(); }
  81.  
  82.         switch (gameMode) {
  83.             case GameMode.StartGame: {startGame();};break;
  84.             case GameMode.Game: {game ();}  break;
  85.             case GameMode.Demo: {demo ();} break;
  86.         }
  87.  
  88.     }
  89.  
  90.  
  91.  
  92.     //-----------------------------------------------------------------------------------------------------------
  93.     void demo() {
  94.         scrollableGround.placeGroundObjects(transform.position.z);
  95.         cam.transform.position=cam.transform.position+new Vector3(0,0,1);
  96.         if (cam.transform.position.z>maxDistance) {
  97.             resetCamera();
  98.             //levelGeometry.placeObjects(transform.position.z,maxDistance);
  99.         }
  100.  
  101.     }
  102.     //-----------------------------------------------------------------------------------------------------------
  103.     void resetCamera() {
  104.         //cam.transform.position = new Vector3(31,16,-6);
  105.         cam.transform.position = startPosition;
  106.         cam.transform.rotation.eulerAngles.Set(14,-50,-4);
  107.         transform.position = new Vector3(0,0,0);
  108.     }
  109.     //-----------------------------------------------------------------------------------------------------------
  110.     void startGame() {
  111.  
  112.         scrollableGround.placeGroundObjects(transform.position.z);
  113.         gameMode=GameMode.Game;
  114.     }
  115.     //-----------------------------------------------------------------------------------------------------------
  116.     void resetGame() {
  117.         transform.position = new Vector3(0,0,30);
  118.         level.placeObjects(transform.position.z,maxDistance);
  119.         scrollableGround.placeGroundObjects(transform.position.z);
  120.         gameMode=GameMode.Demo;
  121.     }
  122.     //-----------------------------------------------------------------------------------------------------------
  123.     void game() {
  124.         level.placeObjectsDuringGame(transform.position.z,maxDistance);
  125.         scrollableGround.placeGroundObjectsDuringGame(transform.position.z);
  126.         //levelGeometry.pl(0,maxDistance); 
  127.         //levelGeometry.placeObjectsDuringGame(
  128.         Hud.instance.TXT_distance.text="totalDistance:"+totalDistance;
  129.         Hud.instance.TXT_fuel.text="fuel:"+fuel;
  130.         //totalDistance+=GetComponent<Rigidbody>().velocity.magnitude;
  131.  
  132.         if ((Input.GetKey(kLeft) || (Input.GetKey(kLeft2)))) {move(true);}
  133.         if ((Input.GetKey(kRight) || (Input.GetKey(kRight2)))) {move(false);}
  134.        
  135.         GetComponent<Rigidbody>().velocity*=0.9f;
  136.        
  137.         cam.transform.position=transform.position+camOffset;
  138.         cam.transform.LookAt(transform.position+lookatOffset);
  139.         GetComponent<Rigidbody>().AddForce(0,0,Speed);
  140.        
  141.         fuel--;
  142.         if (fuel<=0) { resetGame(); };
  143.         currentFireDelay-=1;
  144.         //if (Input.GetKey(kFire)) { fire();}
  145.    
  146.         posInt++;
  147.         if (posInt>=3) {
  148.             posInt=0;
  149.             distPerFrame=(transform.position.z-lastPos);
  150.             lastPos =transform.position.z;
  151.             totalDistance+=distPerFrame;
  152.             //if (totalDistance>=maxDistance) {loop (); }
  153.         }
  154.  
  155.         turnRatio*=0.9f;
  156.         //plane.transform.Rotate(new Vector3(1,0,0),planeOrientation.x);
  157.  
  158.         planeOrientation = Vector3.Lerp(planeOrientation,nextPlaneOrientation,0.1f);
  159.         nextPlaneOrientation = new Vector3(0,0,nextPlaneOrientation.z*0.5f);
  160.         plane.transform.eulerAngles  =new Vector3(planeOrientation.x*20,90,planeOrientation.x*5);
  161.         //planeOrientation = new Vector3(planeOrientation.x*0.9f,planeOrientation.y,planeOrientation.z);
  162.  
  163.     }
  164. /*
  165.     //-----------------------------------------------------------------------------------------------------------
  166.     void loop() {
  167.         //totalDistance=0;
  168.         //Debug.Log ("loop:"+maxDistance);
  169.         //levelGeometry.placeObjects(transform.position.z,maxDistance);
  170.  
  171.     }
  172. */
  173.     /*
  174.     //-----------------------------------------------------------------------------------------------------------
  175.     void fire() {
  176.         if (currentFireDelay<=0) {
  177.             currentFireDelay=fireDelay;
  178.             GameObject newBullet = GameObject.Instantiate(bullet);
  179.             newBullet.transform.position=transform.position+fireOffset;
  180.             newBullet.GetComponent<Rigidbody>().AddForce((transform.forward*bulletSpeed));
  181.         }
  182.     }
  183.     */
  184.     //-----------------------------------------------------------------------------------------------------------
  185.     void move(bool right) {
  186.  
  187.         float orient = 1.5f;
  188.         float rotationScale=1000.0f;
  189.  
  190.         if (right) {
  191.             turnRatio+=0.5f;
  192.             nextPlaneOrientation = new Vector3(-orient,0,0);
  193.             lookatOffset=Vector3.Lerp(lookatOffset,new Vector3(-orient,1.2f,0),0.1f);
  194.             GetComponent<Rigidbody>().AddForce(-turnSpeed,0,0);
  195.         }
  196.         else {
  197.             turnRatio+=0.5f;
  198.             nextPlaneOrientation = new Vector3(orient,0,0);
  199.             lookatOffset=Vector3.Lerp(lookatOffset,new Vector3(orient,1.2f,0),0.1f);
  200.             GetComponent<Rigidbody>().AddForce(turnSpeed,0,0);
  201.         }
  202.  
  203.         //plane.transform.Rotate(new Vector3(1,0,0),planeOrientation.x);
  204.         //Debug.Log ("planeOrientation:"+planeOrientation);
  205.  
  206.      }
  207.        
  208.    
  209.  
  210.     /*
  211.     //-----------------------------------------------------------------------------------------------------------
  212.     void OnCollisionEnter(Collision collision) {
  213.         Debug.Log ("OnCollisionEnter:collide with:"+collision.collider.gameObject.tag);
  214.         collectObject(collision.collider.gameObject);
  215.     }
  216.     */
  217.     //-----------------------------------------------------------------------------------------------------------
  218.     void OnTriggerEnter(Collider collider) {
  219. //      Debug.Log ("OntriggerEnter::::collide with:"+collider.gameObject.tag);
  220.         collectObject(collider.gameObject);
  221.     }
  222.     //-----------------------------------------------------------------------------------------------------------
  223.     void collectObject(GameObject gameobject){
  224.         //gameobject.transform.position = new Vector3(999,999,9999);
  225.        
  226.         if (gameobject.tag=="Bonus_Fuel") {
  227.             fuel+=baseFuelBonus;
  228.             gameobject.GetComponent<Collectible>().collect();
  229.         };
  230.         if (gameobject.tag=="Bonus_Speed") {
  231.             fuel+=baseFuelBonus;
  232.             gameobject.GetComponent<Collectible>().collect();
  233.         };
  234.         if (gameobject.tag=="Bonus_Mine") {
  235.             gameobject.GetComponent<Collectible>().collect();
  236.             resetGame();
  237.         };
  238.  
  239.  
  240.     }
  241.  
  242. }
  243.  
  244.  
  245. //=============================================================================================================
  246. //Collectible.cs
  247. //=============================================================================================================
  248. using UnityEngine;
  249. using System.Collections;
  250.  
  251. public class Collectible : MonoBehaviour {
  252.  
  253.     public int hp=10;
  254.     public float textOffset=-20;
  255.     private bool collected=false;
  256.  
  257.     public GameObject text;
  258.     public GameObject mesh;
  259.  
  260.     private Vector3 basePos;
  261.  
  262.     //-----------------------------------------------------------------------------------------------------------
  263.     void Start () {
  264.         //text.transform.position =new Vector3(999999999999,999999999,9999);
  265.         basePos = mesh.transform.position;
  266.     }
  267.    
  268.     //-----------------------------------------------------------------------------------------------------------
  269.     void Update () {
  270.  
  271.  
  272.         if (collected) {
  273.             text.transform.position+=new Vector3(0,0.1f,0);
  274.             mesh.transform.position = new Vector3(9999,99999,9999);
  275.         } else {
  276.             //text.transform.position =new Vector3(mesh.transform.position.x,mesh.transform.position.y,mesh.transform.position.z);
  277.             text.transform.position = new Vector3(9999,99999,9999);
  278.             mesh.transform.position = basePos;
  279.         }
  280.         //text.transform.position =new Vector3(transform.position.x,transform.position.y,transform.position.z);
  281.         //text.transform.position =new Vector3(mesh.transform.position.x,mesh.transform.position.y,mesh.transform.position.z);
  282.     }
  283.  
  284.  
  285.     //-----------------------------------------------------------------------------------------------------------
  286.     void OnCollisionEnter(Collision collision) {
  287.         //Debug.Log ("collide with:"+collision.collider.gameObject.tag);
  288.  
  289.         if (collision.collider.gameObject.tag=="Bullet") {
  290.             hp--;
  291.             //Destroy (collision.collider.gameObject);
  292.             if (hp<=0) { destroyObject();}
  293.         };
  294.  
  295.     }
  296.     /*
  297.     void OnTriggerEnter(Collider collider) {
  298.         Debug.Log ("OntriggerEnter::::collide with:"+collider.gameObject.tag);
  299.         if (collider.gameObject.tag=="Player") {destroyObject();};
  300.     }
  301.     */
  302.     //-----------------------------------------------------------------------------------------------------------
  303.     public void reset()  {
  304.         //Debug.Log ("reset!");
  305.         collected=false;
  306.         basePos = mesh.transform.position;
  307.     }
  308.     //-----------------------------------------------------------------------------------------------------------
  309.     public void collect()  {
  310.     //  Debug.Log ("collect:"+tag);
  311.         collected=true;
  312.         text.transform.position =basePos-new Vector3(0,0,textOffset);
  313.         //mesh.transform.position = new Vector3(9999,99999,9999);
  314.     }
  315.     //-----------------------------------------------------------------------------------------------------------
  316.  
  317.     void destroyObject() {
  318.  
  319.         //transform.position = new Vector3(9999,99999,9999);
  320.     }
  321. }
  322.  
  323.  
  324. //=============================================================================================================
  325. //Hud.cs
  326. //=============================================================================================================
  327. using UnityEngine;
  328. using UnityEngine.UI;
  329. using System.Collections;
  330.  
  331. public class Hud : MonoBehaviour {
  332.  
  333.     public static Hud instance;
  334.  
  335.     public Image title;
  336.     public Image pressStart;
  337.  
  338.     public Image GUI_bar;
  339.     public Text TXT_distance;
  340.     public Text TXT_fuel;
  341.  
  342.     // Use this for initialization
  343.     void Start () {
  344.    
  345.         instance=this;
  346.     }
  347.  
  348.  
  349.     // Update is called once per frame
  350.     void FixedUpdate () {
  351.         switch (PlayerControl._gameMode) {
  352.         case PlayerControl.GameMode.Game: { UI_Game(); }  break;
  353.         case PlayerControl.GameMode.Demo: { UI_Demo(); }  break;
  354.         }
  355.     }
  356.  
  357.     void UI_Game() {
  358.         //title.GetComponent<Image>().IsActive.Set (10,112,0);
  359.         title.GetComponent<RectTransform>().anchoredPosition = new Vector3(10000,112,0);
  360.         pressStart.GetComponent<RectTransform>().anchoredPosition = new Vector3(10000,-126,0);
  361.  
  362.         GUI_bar.GetComponent<RectTransform>().anchoredPosition = new Vector3(-15,239,0);
  363.         TXT_distance.GetComponent<RectTransform>().anchoredPosition = new Vector3(-270,226,0);
  364.         TXT_fuel.GetComponent<RectTransform>().anchoredPosition = new Vector3(400,226,0);
  365.     }
  366.  
  367.     void UI_Demo() {
  368.         title.GetComponent<RectTransform>().anchoredPosition = VeryBasicTween( new Vector3(10,112,0),title.GetComponent<RectTransform>().anchoredPosition);
  369.         pressStart.GetComponent<RectTransform>().anchoredPosition = new Vector3(11,-126,0);
  370.         GUI_bar.GetComponent<RectTransform>().anchoredPosition = new Vector3(-15000,239,0);
  371.         TXT_distance.GetComponent<RectTransform>().anchoredPosition = new Vector3(-1150,226,0);
  372.         TXT_fuel.GetComponent<RectTransform>().anchoredPosition = new Vector3(-110005,226,0);
  373.     }
  374.  
  375.     Vector3 VeryBasicTween(Vector3 newPos,Vector3 currentPos) {
  376.         return newPos;
  377.     }
  378. }
  379.  
  380.  
  381. //=============================================================================================================
  382. //LevelObjects.cs
  383. //=============================================================================================================
  384. using UnityEngine;
  385. using System.Collections;
  386. using System.Collections.Generic;
  387.  
  388.  
  389. public class Level : MonoBehaviour {
  390.  
  391.     public static Level instance;
  392.  
  393.  
  394.     public  int rowSize=4;
  395.     public  float rowSpacing=10;
  396.     public  float rowStart=-30;
  397.     public float objectScale=3;
  398.     public List<float> rows;
  399.     public int nextRow=1;
  400.     public float objectSpacingZ=5;
  401.  
  402.     public float yObjectsPos=-4.4f;
  403.     public float levelSize=40;
  404.  
  405.     public GameObject groundElement;
  406.     public List<Transform> levelObjects;
  407.     // Use this for initialization
  408.  
  409.     void Start () {
  410.         instance=this;
  411.     }
  412.  
  413.     //-----------------------------------------------------------------------------------------------------------
  414.     void Update () {
  415.         /*
  416.         for (int i=0; i<levelObjects.Count; i++) {
  417.             levelObjects[i].position = new Vector3(levelObjects[i].position.x,yObjectsPos,levelObjects[i].position.z);
  418.         }*/
  419.  
  420.     }
  421.     //-----------------------------------------------------------------------------------------------------------
  422.     void initRows() {
  423.         rows = new List<float>();
  424.         float previousRowValue=rowStart;
  425.         for (int i=0;i<rowSize;i++) {
  426.             rows.Add(  previousRowValue+rowSpacing);
  427.             previousRowValue=rows[i];
  428.         }
  429.     }
  430.  
  431.     //-----------------------------------------------------------------------------------------------------------
  432.     public void placeObjects(float startPos,float maxDistance) {
  433.         levelObjects = new List<Transform>();
  434.         initRows();
  435.  
  436.         foreach (Transform child in transform) { levelObjects.Add (child);  }
  437.  
  438.         float currentSpacing=maxDistance/levelObjects.Count;
  439.     //  Debug.Log ("currentSpacing:"+currentSpacing+":"+maxDistance+"/"+levelObjects.Count);
  440.  
  441.         for (int i=0; i<levelObjects.Count; i++) {
  442.             placeObject(startPos,levelObjects[i],i,currentSpacing);
  443.         }
  444.     }
  445.     //-----------------------------------------------------------------------------------------------------------
  446.     public void placeObjectsDuringGame(float startPos,float maxDistance) {
  447.         levelObjects = new List<Transform>();
  448.         initRows();
  449.        
  450.         foreach (Transform child in transform) { levelObjects.Add (child);  }
  451.        
  452.         float currentSpacing=(maxDistance/levelObjects.Count)+objectSpacingZ;
  453.         //Debug.Log ("currentSpacing:"+currentSpacing+":"+maxDistance+"/"+levelObjects.Count);
  454.  
  455.         for (int i=0; i<levelObjects.Count; i++) {
  456.             if (levelObjects[i].transform.position.z<startPos-50) {  
  457.                     placeObject(startPos+maxDistance,levelObjects[i],i,currentSpacing);
  458.                 };
  459.         }
  460.     }
  461.  
  462.  
  463.  
  464.     //-----------------------------------------------------------------------------------------------------------
  465.     void placeObject(float startPos,Transform g,int objectID,float currentSpacing) {
  466.         nextRow =Mathf.RoundToInt(Random.Range(0,rows.Count));
  467.         if (nextRow>rows.Count) {nextRow=rows.Count;};
  468.         if (nextRow<0) {nextRow=0;};
  469.         g.position =new Vector3(rows[nextRow],yObjectsPos,startPos+(objectID*currentSpacing));
  470.         g.localScale  = new Vector3(objectScale,objectScale,objectScale);
  471.         g.GetComponent<Collectible>().reset ();
  472.  
  473.     }
  474.  
  475. }
  476.  
  477.  
  478.  
  479. //=============================================================================================================
  480. //ScrollableGround.cs
  481. //=============================================================================================================
  482. using UnityEngine;
  483. using System.Collections;
  484. using System.Collections;
  485. using System.Collections.Generic;
  486.  
  487. public class ScrollableGround : MonoBehaviour {
  488.  
  489.     public static ScrollableGround instance;
  490.     public float GroundSpacing=50;
  491.     public float nextObjectPosition;
  492.     public List<Transform> groundObjects;
  493.  
  494.  
  495.     //-----------------------------------------------------------------------------------------------------------
  496.     void Start () {
  497.         instance=this;
  498.         groundObjects = new List<Transform>();
  499.         foreach (Transform child in transform) { groundObjects.Add (child); }
  500.     }
  501.    
  502.     //-----------------------------------------------------------------------------------------------------------
  503.     void Update () {
  504.    
  505.     }
  506.     //-----------------------------------------------------------------------------------------------------------
  507.     public void placeGroundObjects(float startPos) {
  508.  
  509.         for (int i=0; i<groundObjects.Count; i++) {
  510.             //ebug.Log ("placeGroundObjects:"+groundObjects[i].transform.position.z);
  511.  
  512.             placeGroundObject(startPos,groundObjects[i],(i*GroundSpacing));
  513.                 nextObjectPosition=groundObjects[i].transform.position.z;
  514.         }
  515.     }
  516.  
  517.     //-----------------------------------------------------------------------------------------------------------
  518.     public void placeGroundObjectsDuringGame(float startPos) {
  519.         for (int i=0; i<groundObjects.Count; i++) {
  520.             //Debug.Log ("placeGroundObjectsDuringGame:"+groundObjects[i].transform.position.z);
  521.             if (groundObjects[i].transform.position.z<startPos-GroundSpacing) {  
  522.  
  523.                 placeGroundObject(startPos,groundObjects[i],GroundSpacing*(groundObjects.Count-1));
  524.                 //placeGroundObject(startPos,groundObjects[i],nextObjectPosition);
  525.                 //nextObjectPosition
  526.             };
  527.         }
  528.     }
  529.  
  530.     //-----------------------------------------------------------------------------------------------------------
  531.     void placeGroundObject(float startPos,Transform g,float currentSpacing) {
  532.         g.position =new Vector3(g.position.x,g.position.y,startPos+currentSpacing);
  533.         //nextObjectPosition=g.position.z;
  534.     }
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement