Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================================================
- // Source for https://www.fgl.com/view_game.php?from=dev&game_id=39196
- // you can use it if you want, don't share on the asset store, credits appreciated if you find it useful, xoxo
- //-------------------------------------------------------------------------------------------------------------
- // #has "lolcode.h"
- // #has "unity.h"
- //=============================================================================================================
- // PlayerControl.cs
- //=============================================================================================================
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class PlayerControl : MonoBehaviour {
- public int fuel=10000;
- public int baseFuelBonus=10;
- public enum GameMode {Demo,StartGame,Game};
- public static GameMode _gameMode;
- public static PlayerControl instance;
- public GameMode gameMode;
- public Camera cam;
- public Level level;
- public ScrollableGround scrollableGround;
- public GameObject bullet;
- public Transform plane;
- public KeyCode kLeft,kRight,kFire,kDebug1;
- public KeyCode kLeft2,kRight2;
- public float turnSpeed;
- public float Speed;
- public float bulletSpeed;
- private float currentFireDelay;
- public float fireDelay;
- public float maxDistance;
- public Vector3 camOffset;
- public Vector3 lookatOffset;
- public Vector3 fireOffset;
- public Vector3 startPosition;
- public Vector3 planeOrientation= new Vector3(0,0,0);
- public Vector3 nextPlaneOrientation= new Vector3(0,0,0);
- private float totalDistance;
- private float currentDistance;
- private float lastPos;
- private float distPerFrame;
- private float turnRatio=0.0f;
- private int posInt;
- //-----------------------------------------------------------------------------------------------------------
- void Start () {
- resetCamera();
- level.placeObjects(transform.position.z,maxDistance);
- scrollableGround.placeGroundObjects(transform.position.z);
- instance=this;
- }
- //-----------------------------------------------------------------------------------------------------------
- void FixedUpdate () {
- _gameMode=gameMode;
- if ( (Input.GetKey(kFire) || (Input.GetMouseButtonDown(0) || (Input.GetMouseButtonDown(1)) ) ) ) {
- if (gameMode!=GameMode.Game) { gameMode=GameMode.StartGame; }
- }
- //if (Input.GetKey(kDebug1)) { LevelGeometry.instance.placeObjects(); }
- switch (gameMode) {
- case GameMode.StartGame: {startGame();};break;
- case GameMode.Game: {game ();} break;
- case GameMode.Demo: {demo ();} break;
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- void demo() {
- scrollableGround.placeGroundObjects(transform.position.z);
- cam.transform.position=cam.transform.position+new Vector3(0,0,1);
- if (cam.transform.position.z>maxDistance) {
- resetCamera();
- //levelGeometry.placeObjects(transform.position.z,maxDistance);
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- void resetCamera() {
- //cam.transform.position = new Vector3(31,16,-6);
- cam.transform.position = startPosition;
- cam.transform.rotation.eulerAngles.Set(14,-50,-4);
- transform.position = new Vector3(0,0,0);
- }
- //-----------------------------------------------------------------------------------------------------------
- void startGame() {
- scrollableGround.placeGroundObjects(transform.position.z);
- gameMode=GameMode.Game;
- }
- //-----------------------------------------------------------------------------------------------------------
- void resetGame() {
- transform.position = new Vector3(0,0,30);
- level.placeObjects(transform.position.z,maxDistance);
- scrollableGround.placeGroundObjects(transform.position.z);
- gameMode=GameMode.Demo;
- }
- //-----------------------------------------------------------------------------------------------------------
- void game() {
- level.placeObjectsDuringGame(transform.position.z,maxDistance);
- scrollableGround.placeGroundObjectsDuringGame(transform.position.z);
- //levelGeometry.pl(0,maxDistance);
- //levelGeometry.placeObjectsDuringGame(
- Hud.instance.TXT_distance.text="totalDistance:"+totalDistance;
- Hud.instance.TXT_fuel.text="fuel:"+fuel;
- //totalDistance+=GetComponent<Rigidbody>().velocity.magnitude;
- if ((Input.GetKey(kLeft) || (Input.GetKey(kLeft2)))) {move(true);}
- if ((Input.GetKey(kRight) || (Input.GetKey(kRight2)))) {move(false);}
- GetComponent<Rigidbody>().velocity*=0.9f;
- cam.transform.position=transform.position+camOffset;
- cam.transform.LookAt(transform.position+lookatOffset);
- GetComponent<Rigidbody>().AddForce(0,0,Speed);
- fuel--;
- if (fuel<=0) { resetGame(); };
- currentFireDelay-=1;
- //if (Input.GetKey(kFire)) { fire();}
- posInt++;
- if (posInt>=3) {
- posInt=0;
- distPerFrame=(transform.position.z-lastPos);
- lastPos =transform.position.z;
- totalDistance+=distPerFrame;
- //if (totalDistance>=maxDistance) {loop (); }
- }
- turnRatio*=0.9f;
- //plane.transform.Rotate(new Vector3(1,0,0),planeOrientation.x);
- planeOrientation = Vector3.Lerp(planeOrientation,nextPlaneOrientation,0.1f);
- nextPlaneOrientation = new Vector3(0,0,nextPlaneOrientation.z*0.5f);
- plane.transform.eulerAngles =new Vector3(planeOrientation.x*20,90,planeOrientation.x*5);
- //planeOrientation = new Vector3(planeOrientation.x*0.9f,planeOrientation.y,planeOrientation.z);
- }
- /*
- //-----------------------------------------------------------------------------------------------------------
- void loop() {
- //totalDistance=0;
- //Debug.Log ("loop:"+maxDistance);
- //levelGeometry.placeObjects(transform.position.z,maxDistance);
- }
- */
- /*
- //-----------------------------------------------------------------------------------------------------------
- void fire() {
- if (currentFireDelay<=0) {
- currentFireDelay=fireDelay;
- GameObject newBullet = GameObject.Instantiate(bullet);
- newBullet.transform.position=transform.position+fireOffset;
- newBullet.GetComponent<Rigidbody>().AddForce((transform.forward*bulletSpeed));
- }
- }
- */
- //-----------------------------------------------------------------------------------------------------------
- void move(bool right) {
- float orient = 1.5f;
- float rotationScale=1000.0f;
- if (right) {
- turnRatio+=0.5f;
- nextPlaneOrientation = new Vector3(-orient,0,0);
- lookatOffset=Vector3.Lerp(lookatOffset,new Vector3(-orient,1.2f,0),0.1f);
- GetComponent<Rigidbody>().AddForce(-turnSpeed,0,0);
- }
- else {
- turnRatio+=0.5f;
- nextPlaneOrientation = new Vector3(orient,0,0);
- lookatOffset=Vector3.Lerp(lookatOffset,new Vector3(orient,1.2f,0),0.1f);
- GetComponent<Rigidbody>().AddForce(turnSpeed,0,0);
- }
- //plane.transform.Rotate(new Vector3(1,0,0),planeOrientation.x);
- //Debug.Log ("planeOrientation:"+planeOrientation);
- }
- /*
- //-----------------------------------------------------------------------------------------------------------
- void OnCollisionEnter(Collision collision) {
- Debug.Log ("OnCollisionEnter:collide with:"+collision.collider.gameObject.tag);
- collectObject(collision.collider.gameObject);
- }
- */
- //-----------------------------------------------------------------------------------------------------------
- void OnTriggerEnter(Collider collider) {
- // Debug.Log ("OntriggerEnter::::collide with:"+collider.gameObject.tag);
- collectObject(collider.gameObject);
- }
- //-----------------------------------------------------------------------------------------------------------
- void collectObject(GameObject gameobject){
- //gameobject.transform.position = new Vector3(999,999,9999);
- if (gameobject.tag=="Bonus_Fuel") {
- fuel+=baseFuelBonus;
- gameobject.GetComponent<Collectible>().collect();
- };
- if (gameobject.tag=="Bonus_Speed") {
- fuel+=baseFuelBonus;
- gameobject.GetComponent<Collectible>().collect();
- };
- if (gameobject.tag=="Bonus_Mine") {
- gameobject.GetComponent<Collectible>().collect();
- resetGame();
- };
- }
- }
- //=============================================================================================================
- //Collectible.cs
- //=============================================================================================================
- using UnityEngine;
- using System.Collections;
- public class Collectible : MonoBehaviour {
- public int hp=10;
- public float textOffset=-20;
- private bool collected=false;
- public GameObject text;
- public GameObject mesh;
- private Vector3 basePos;
- //-----------------------------------------------------------------------------------------------------------
- void Start () {
- //text.transform.position =new Vector3(999999999999,999999999,9999);
- basePos = mesh.transform.position;
- }
- //-----------------------------------------------------------------------------------------------------------
- void Update () {
- if (collected) {
- text.transform.position+=new Vector3(0,0.1f,0);
- mesh.transform.position = new Vector3(9999,99999,9999);
- } else {
- //text.transform.position =new Vector3(mesh.transform.position.x,mesh.transform.position.y,mesh.transform.position.z);
- text.transform.position = new Vector3(9999,99999,9999);
- mesh.transform.position = basePos;
- }
- //text.transform.position =new Vector3(transform.position.x,transform.position.y,transform.position.z);
- //text.transform.position =new Vector3(mesh.transform.position.x,mesh.transform.position.y,mesh.transform.position.z);
- }
- //-----------------------------------------------------------------------------------------------------------
- void OnCollisionEnter(Collision collision) {
- //Debug.Log ("collide with:"+collision.collider.gameObject.tag);
- if (collision.collider.gameObject.tag=="Bullet") {
- hp--;
- //Destroy (collision.collider.gameObject);
- if (hp<=0) { destroyObject();}
- };
- }
- /*
- void OnTriggerEnter(Collider collider) {
- Debug.Log ("OntriggerEnter::::collide with:"+collider.gameObject.tag);
- if (collider.gameObject.tag=="Player") {destroyObject();};
- }
- */
- //-----------------------------------------------------------------------------------------------------------
- public void reset() {
- //Debug.Log ("reset!");
- collected=false;
- basePos = mesh.transform.position;
- }
- //-----------------------------------------------------------------------------------------------------------
- public void collect() {
- // Debug.Log ("collect:"+tag);
- collected=true;
- text.transform.position =basePos-new Vector3(0,0,textOffset);
- //mesh.transform.position = new Vector3(9999,99999,9999);
- }
- //-----------------------------------------------------------------------------------------------------------
- void destroyObject() {
- //transform.position = new Vector3(9999,99999,9999);
- }
- }
- //=============================================================================================================
- //Hud.cs
- //=============================================================================================================
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class Hud : MonoBehaviour {
- public static Hud instance;
- public Image title;
- public Image pressStart;
- public Image GUI_bar;
- public Text TXT_distance;
- public Text TXT_fuel;
- // Use this for initialization
- void Start () {
- instance=this;
- }
- // Update is called once per frame
- void FixedUpdate () {
- switch (PlayerControl._gameMode) {
- case PlayerControl.GameMode.Game: { UI_Game(); } break;
- case PlayerControl.GameMode.Demo: { UI_Demo(); } break;
- }
- }
- void UI_Game() {
- //title.GetComponent<Image>().IsActive.Set (10,112,0);
- title.GetComponent<RectTransform>().anchoredPosition = new Vector3(10000,112,0);
- pressStart.GetComponent<RectTransform>().anchoredPosition = new Vector3(10000,-126,0);
- GUI_bar.GetComponent<RectTransform>().anchoredPosition = new Vector3(-15,239,0);
- TXT_distance.GetComponent<RectTransform>().anchoredPosition = new Vector3(-270,226,0);
- TXT_fuel.GetComponent<RectTransform>().anchoredPosition = new Vector3(400,226,0);
- }
- void UI_Demo() {
- title.GetComponent<RectTransform>().anchoredPosition = VeryBasicTween( new Vector3(10,112,0),title.GetComponent<RectTransform>().anchoredPosition);
- pressStart.GetComponent<RectTransform>().anchoredPosition = new Vector3(11,-126,0);
- GUI_bar.GetComponent<RectTransform>().anchoredPosition = new Vector3(-15000,239,0);
- TXT_distance.GetComponent<RectTransform>().anchoredPosition = new Vector3(-1150,226,0);
- TXT_fuel.GetComponent<RectTransform>().anchoredPosition = new Vector3(-110005,226,0);
- }
- Vector3 VeryBasicTween(Vector3 newPos,Vector3 currentPos) {
- return newPos;
- }
- }
- //=============================================================================================================
- //LevelObjects.cs
- //=============================================================================================================
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Level : MonoBehaviour {
- public static Level instance;
- public int rowSize=4;
- public float rowSpacing=10;
- public float rowStart=-30;
- public float objectScale=3;
- public List<float> rows;
- public int nextRow=1;
- public float objectSpacingZ=5;
- public float yObjectsPos=-4.4f;
- public float levelSize=40;
- public GameObject groundElement;
- public List<Transform> levelObjects;
- // Use this for initialization
- void Start () {
- instance=this;
- }
- //-----------------------------------------------------------------------------------------------------------
- void Update () {
- /*
- for (int i=0; i<levelObjects.Count; i++) {
- levelObjects[i].position = new Vector3(levelObjects[i].position.x,yObjectsPos,levelObjects[i].position.z);
- }*/
- }
- //-----------------------------------------------------------------------------------------------------------
- void initRows() {
- rows = new List<float>();
- float previousRowValue=rowStart;
- for (int i=0;i<rowSize;i++) {
- rows.Add( previousRowValue+rowSpacing);
- previousRowValue=rows[i];
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- public void placeObjects(float startPos,float maxDistance) {
- levelObjects = new List<Transform>();
- initRows();
- foreach (Transform child in transform) { levelObjects.Add (child); }
- float currentSpacing=maxDistance/levelObjects.Count;
- // Debug.Log ("currentSpacing:"+currentSpacing+":"+maxDistance+"/"+levelObjects.Count);
- for (int i=0; i<levelObjects.Count; i++) {
- placeObject(startPos,levelObjects[i],i,currentSpacing);
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- public void placeObjectsDuringGame(float startPos,float maxDistance) {
- levelObjects = new List<Transform>();
- initRows();
- foreach (Transform child in transform) { levelObjects.Add (child); }
- float currentSpacing=(maxDistance/levelObjects.Count)+objectSpacingZ;
- //Debug.Log ("currentSpacing:"+currentSpacing+":"+maxDistance+"/"+levelObjects.Count);
- for (int i=0; i<levelObjects.Count; i++) {
- if (levelObjects[i].transform.position.z<startPos-50) {
- placeObject(startPos+maxDistance,levelObjects[i],i,currentSpacing);
- };
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- void placeObject(float startPos,Transform g,int objectID,float currentSpacing) {
- nextRow =Mathf.RoundToInt(Random.Range(0,rows.Count));
- if (nextRow>rows.Count) {nextRow=rows.Count;};
- if (nextRow<0) {nextRow=0;};
- g.position =new Vector3(rows[nextRow],yObjectsPos,startPos+(objectID*currentSpacing));
- g.localScale = new Vector3(objectScale,objectScale,objectScale);
- g.GetComponent<Collectible>().reset ();
- }
- }
- //=============================================================================================================
- //ScrollableGround.cs
- //=============================================================================================================
- using UnityEngine;
- using System.Collections;
- using System.Collections;
- using System.Collections.Generic;
- public class ScrollableGround : MonoBehaviour {
- public static ScrollableGround instance;
- public float GroundSpacing=50;
- public float nextObjectPosition;
- public List<Transform> groundObjects;
- //-----------------------------------------------------------------------------------------------------------
- void Start () {
- instance=this;
- groundObjects = new List<Transform>();
- foreach (Transform child in transform) { groundObjects.Add (child); }
- }
- //-----------------------------------------------------------------------------------------------------------
- void Update () {
- }
- //-----------------------------------------------------------------------------------------------------------
- public void placeGroundObjects(float startPos) {
- for (int i=0; i<groundObjects.Count; i++) {
- //ebug.Log ("placeGroundObjects:"+groundObjects[i].transform.position.z);
- placeGroundObject(startPos,groundObjects[i],(i*GroundSpacing));
- nextObjectPosition=groundObjects[i].transform.position.z;
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- public void placeGroundObjectsDuringGame(float startPos) {
- for (int i=0; i<groundObjects.Count; i++) {
- //Debug.Log ("placeGroundObjectsDuringGame:"+groundObjects[i].transform.position.z);
- if (groundObjects[i].transform.position.z<startPos-GroundSpacing) {
- placeGroundObject(startPos,groundObjects[i],GroundSpacing*(groundObjects.Count-1));
- //placeGroundObject(startPos,groundObjects[i],nextObjectPosition);
- //nextObjectPosition
- };
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- void placeGroundObject(float startPos,Transform g,float currentSpacing) {
- g.position =new Vector3(g.position.x,g.position.y,startPos+currentSpacing);
- //nextObjectPosition=g.position.z;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement