Created
July 29, 2023 00:32
-
-
Save amirrajan/08d3971613a29b6f322353cc9dc33e89 to your computer and use it in GitHub Desktop.
DragonRuby Game Toolkit vs Unity Performance - Collisions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Square | |
attr_sprite | |
def initialize | |
@x = rand 1280 | |
@y = rand 720 | |
@w = 15 | |
@h = 15 | |
@path = 'sprites/square/blue.png' | |
@dir = 1 | |
end | |
def mark_collisions all | |
@path = if all[self] | |
'sprites/square/red.png' | |
else | |
'sprites/square/blue.png' | |
end | |
end | |
def move | |
@dir = -1 if (@x + @w >= 1280) && @dir == 1 | |
@dir = 1 if (@x <= 0) && @dir == -1 | |
@x += @dir | |
end | |
end | |
def reset_if_needed args | |
if args.state.tick_count == 0 || args.inputs.mouse.click | |
args.state.star_count = 10000 | |
args.state.stars = args.state.star_count.map { |i| Square.new }.to_a | |
args.outputs.static_sprites.clear | |
args.outputs.static_sprites << args.state.stars | |
end | |
end | |
def tick args | |
reset_if_needed args | |
Fn.each args.state.stars do |s| s.move end | |
all = GTK::Geometry.find_collisions args.state.stars | |
Fn.each args.state.stars do |s| s.mark_collisions all end | |
args.outputs.background_color = [0, 0, 0] | |
args.outputs.primitives << args.gtk.current_framerate_primitives | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
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; | |
} | |
} | |
public class Game : MonoBehaviour | |
{ | |
public int SquareCount; | |
public GameObject SquarePrefab; | |
public static Game Instance; | |
public float BoundsMinX, BoundsMaxX, BoundsMinY, BoundsMaxY; | |
public float MinSpeed = 1f; | |
public float MaxSpeed = 4f; | |
public float BoundsPadding = 2f; | |
public TMPro.TMP_Text Text; | |
void Awake() | |
{ | |
SquareCount = 10000; | |
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 < SquareCount; i++) | |
{ | |
var s = Instantiate(SquarePrefab, | |
new Vector3(Random.Range(BoundsMinX, BoundsMaxX), | |
Random.Range(BoundsMinY, BoundsMaxY), | |
10f), | |
Quaternion.identity); | |
s.transform.localScale = new Vector3(0.25f, 0.25f, 1f); | |
} | |
Camera.main.orthographicSize = 8f; | |
} | |
void Update() | |
{ | |
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 System; | |
using UnityEngine; | |
public class SquarePrefab : MonoBehaviour | |
{ | |
public Rigidbody2D rigidbody2d; | |
public BoxCollider2D boxCollider2d; | |
public Sprite blueSprite; | |
public Sprite redSprite; | |
public SpriteRenderer spriteRenderer; | |
float moveSpeed = 2f; | |
int direction = 1; | |
float minPosition = 0f; | |
float maxPosition = 0f; | |
// Start is called before the first frame update | |
void Awake() | |
{ | |
rigidbody2d = GetComponent<Rigidbody2D>(); | |
minPosition = Camera.main.ScreenToWorldPoint(Vector2.zero).x; | |
maxPosition = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x; | |
boxCollider2d = gameObject.AddComponent<BoxCollider2D>(); | |
boxCollider2d.size = new Vector2(0.9f, 0.9f); | |
boxCollider2d.isTrigger = true; | |
rigidbody2d = gameObject.AddComponent<Rigidbody2D>(); | |
rigidbody2d.bodyType = RigidbodyType2D.Kinematic; | |
spriteRenderer.sprite = blueSprite; | |
} | |
void OnTriggerEnter2D(Collider2D other) | |
{ | |
spriteRenderer.sprite = redSprite; | |
} | |
void OnTriggerStay2D(Collider2D other) | |
{ | |
spriteRenderer.sprite = redSprite; | |
} | |
void OnTriggerExit2D(Collider2D other) | |
{ | |
spriteRenderer.sprite = blueSprite; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
float xPosition = transform.position.x; | |
if (xPosition <= minPosition) | |
{ | |
direction = 1; | |
} | |
if (xPosition >= maxPosition) | |
{ | |
direction = -1; | |
} | |
rigidbody2d.velocity = new Vector2(direction * moveSpeed, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment