Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active September 28, 2021 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/e26fc9de3c20e7c395f6809a9946d1b9 to your computer and use it in GitHub Desktop.
Save amirrajan/e26fc9de3c20e7c395f6809a9946d1b9 to your computer and use it in GitHub Desktop.
class Star
def initialize grid
@grid = grid
@x = (rand @grid.w) * -1
@y = (rand @grid.h) * -1
@w = 4
@h = 4
@s = 1 + (4.randomize :ratio)
@path = 'sprites/misc/tiny-star.png'
end
def move
@x += @s
@y += @s
@x = (rand @grid.w) * -1 if @x > @grid.right
@y = (rand @grid.h) * -1 if @y > @grid.top
end
def draw_override ffi_draw
move
ffi_draw.draw_sprite @x, @y, @w, @h, @path
end
end
def tick args
if args.state.tick_count == 0
args.state.stars = 80000.map { |i| Star.new args.grid }
args.outputs.static_sprites << args.state.stars
end
args.outputs.background_color = [0, 0, 0]
args.outputs.primitives << args.gtk.current_framerate_primitives
end
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
public class DisplayFramerate : MonoBehaviour
{
public TMPro.TMP_Text Text;
private void Update()
{
if (Text != null)
{
string fps = ((int)(1f / Time.smoothDeltaTime)).ToString();
string ms = (Time.smoothDeltaTime * 1000f).ToString("00.0");
string display = string.Concat("FPS: ", fps, "\nMS: ", ms);
Text.SetText(display);
}
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
public class Star : MonoBehaviour
{
private float Speed;
private void Start()
{
Speed = Random.Range(Game.Instance.MinSpeed, Game.Instance.MaxSpeed);
}
private void Update()
{
Vector3 nextPos = this.transform.position;
nextPos += Speed * new Vector3(1f, 1f, 0f) * Time.deltaTime;
if (nextPos.x > Game.Instance.BoundsMaxX) nextPos.x = Game.Instance.BoundsMinX;
if (nextPos.y > Game.Instance.BoundsMaxY) nextPos.y = Game.Instance.BoundsMinY;
this.transform.position = nextPos;
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
public static class CameraUtility
{
public static void GetCameraBounds(out float minX, out float minY, out float maxX, out float maxY, float padding)
{
Camera cam = Camera.main;
Vector3 camPos = cam.transform.position;
minX = camPos.x - (cam.orthographicSize * cam.aspect) - padding;
maxX = camPos.x + (cam.orthographicSize * cam.aspect) + padding;
minY = camPos.y - cam.orthographicSize - padding;
maxY = camPos.y + cam.orthographicSize + padding;
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
public class Game : MonoBehaviour
{
public int StarCount;
public GameObject StarPrefab;
public static Game Instance;
public float MinSpeed = 1f;
public float MaxSpeed = 4f;
public float BoundsPadding = 2f;
public float BoundsMinX, BoundsMaxX, BoundsMinY, BoundsMaxY;
private void Awake()
{
if (Application.isPlaying)
{
if (Game.Instance != null) Destroy(this);
Game.Instance = this;
}
CameraUtility.GetCameraBounds(out BoundsMinX, out BoundsMinY, out BoundsMaxX, out BoundsMaxY, BoundsPadding);
for (int i = 0; i < StarCount; i++)
{
Instantiate(StarPrefab,
new Vector3(Random.Range(BoundsMinX, BoundsMaxX),
Random.Range(BoundsMinY, BoundsMaxY),
10f),
Quaternion.identity);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment