Skip to content

Instantly share code, notes, and snippets.

@markdekuijer
Created November 20, 2018 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markdekuijer/7512ba7b23a5b597d0e7f4dd6b28adee to your computer and use it in GitHub Desktop.
Save markdekuijer/7512ba7b23a5b597d0e7f4dd6b28adee to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MapReader : MonoBehaviour
{
[SerializeField] private AudioSource music;
[SerializeField] private AudioSource debugMusic;
[SerializeField] private bool debug;
[SerializeField] private PlayerInput input;
[SerializeField] private GameObject sliceObj;
//different types of ingredient prefabs
[SerializeField] private GameObject fish;
[SerializeField] private GameObject deeg;
[SerializeField] private GameObject komkomer;
[SerializeField] private GameObject prij;
[SerializeField] private GameObject longkomer;
Dictionary<string, GameObject> objects = new Dictionary<string, GameObject>();
[SerializeField] private LevelData mapdata;
public float mapSpeed;
public float spawnDelayMultiplier;
public Text debugText;
public Text scoreText;
public float timeOffset;
int objSpawnCount;
int counter;
//these lists below are set in editor with an editor script instead of being spawn
//this is to save optimalization at the start of a scene (otherwise it would load around 300 vectors/gameobjects)
public List<GameObject> sliceObjs = new List<GameObject>();
public List<Vector3> positions = new List<Vector3>();
[SerializeField] private RubberBand band;
[SerializeField] private PlayerInput player;
private Vector3 startPos;
private float difference;
private bool started;
private void Awake()
{
//add objects to dictionary
objects.Add("Fish", fish);
objects.Add("Deeg", deeg);
objects.Add("Komkomer", komkomer);
objects.Add("Prij", prij);
objects.Add("Longkomer", longkomer);
}
private void Start()
{
startPos = transform.position;
//get first 20 slice times and vectors
for (int i = 0; i < 20; i++)
{
PositionNext();
}
//spawn the basic ingredients(without clickpoints) this will be around 20obj's for a 1-2min map
for (int i = 0; i < mapdata.LevelItems.Count; i++)
{
startPos = transform.position;
GameObject g = Instantiate(objects[mapdata.LevelItems[i].ItemID], Spawn(mapdata.LevelItems[i].SpawnSecondinGame, 3f), Quaternion.identity);
g.transform.SetParent(this.transform);
}
}
public void GameStart()
{
if (!debug)
music.Play();
else
debugMusic.Play();
started = true;
band.started = true;
player.started = true;
}
private void Update()
{
if (Input.GetMouseButtonDown(0) && !started)
GameStart();
difference = startPos.x - transform.position.x;
if (started == false)
return;
//all objects get parented to the root/mapreader so only 1 object has to move in update
float x = transform.position.x;
x += Time.deltaTime * spawnDelayMultiplier;
transform.position = new Vector3(x, 0, 0);
text.text = Time.timeSinceLevelLoad.ToString();
}
//returns the needed vector which will be stored for in edit mode for optimalization
//this works by sending a time and an offset if needed
public Vector3 Spawn(double spawnTime, float yOffset)
{
Vector3 returnVec = Vector3.zero;
returnVec.x = -(float)(spawnTime);
returnVec.x *= spawnDelayMultiplier;
returnVec.y -= yOffset;
return returnVec;
}
//gets the next slice which gets reused from other slices that went past the screen boundary
public void PositionNext()
{
if (counter >= input.clickTimes.Count)
return;
Vector3 pos = positions[counter];
pos.x -= difference;
sliceObjs[objSpawnCount].transform.position = pos;
if(counter < input.clickTimes.Count)
counter++;
objSpawnCount++;
if (objSpawnCount >= 20)
objSpawnCount = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment