Skip to content

Commit

Permalink
Merge pull request #5 from dojoengine/sub-dojo.c
Browse files Browse the repository at this point in the history
chore: include dojo.c submodule in bindings
  • Loading branch information
Larkooo authored Dec 19, 2023
2 parents ce8d27c + 74aef15 commit 4746fd7
Show file tree
Hide file tree
Showing 48 changed files with 23,595 additions and 23,956 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "c2cs"]
path = c2cs
url = https://github.com/Larkooo/c2cs
[submodule "Bindings/dojo.c"]
path = Bindings/dojo.c
url = https://github.com/dojoengine/dojo.c
47 changes: 47 additions & 0 deletions Assets/Dojo/CameraFollow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Dojo;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
public WorldManager worldManager;
public float height = 10.0f; // height of the camera above the center point
public float damping = 1.0f; // damping factor to smooth camera movement
public float speed = 10.0f; // speed of camera movement


void Update()
{
if (worldManager.Entities().Length > 0)
{
Vector3 centerPoint = GetCenterPoint();
Vector3 offset = new Vector3(0, height, -height);
Vector3 desiredPosition = centerPoint + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, damping * Time.deltaTime);
transform.position = smoothedPosition;

transform.LookAt(centerPoint);
}

// Camera control
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(movement * speed * Time.deltaTime, Space.World);
}

Vector3 GetCenterPoint()
{
if (worldManager.Entities().Length == 1)
{
return worldManager.Entities()[0].transform.position;
}

var bounds = new Bounds(worldManager.Entities()[0].transform.position, Vector3.zero);
for (int i = 0; i < worldManager.Entities().Length; i++)
{
bounds.Encapsulate(worldManager.Entities()[i].transform.position);
}
return bounds.center;
}
}
11 changes: 11 additions & 0 deletions Assets/Dojo/CameraFollow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion Assets/Dojo/InitEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dojo;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class InitEntities : MonoBehaviour
{
Expand All @@ -20,7 +21,7 @@ void Start()
// Update is called once per frame
void Update()
{

}

private void InitEntity(GameObject entity)
Expand All @@ -29,5 +30,22 @@ private void InitEntity(GameObject entity)
// change color of capsule to a random color
capsule.GetComponent<Renderer>().material.color = Random.ColorHSV();
capsule.transform.parent = entity.transform;

// create a new GameObject for the text
GameObject textObject = new GameObject("TextTag");
textObject.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
textObject.transform.parent = capsule.transform;
textObject.transform.localPosition = new Vector3(-1, 2, 0);

// add a Text component to the new GameObject
var textTag = textObject.AddComponent<TextMesh>();

// set the properties of the Text component
textTag.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
textTag.color = Color.black;

// add text to the position component
var position = entity.GetComponent<Position>();
position.textTag = textTag;
}
}
24 changes: 21 additions & 3 deletions Assets/Dojo/Models/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
using Dojo.Torii;
using dojo_bindings;
using UnityEngine;
using UnityEngine.UI;

public class Position : ModelInstance
{
// model fields
public dojo.FieldElement player;
// public Vector2Int position => new Vector2Int((int)x, (int)y);
public UInt32 x;
public UInt32 y;


// component fields
public TextMesh textTag;
public string shortPlayerAddress;

public override void Initialize(Model model)
{
player = model.members["player"].ty.ty_primitive.contract_address;
Expand All @@ -21,19 +28,30 @@ public override void Initialize(Model model)

void Start()
{
var target = new Vector3(x, 0, y);
var target = new Vector3(x, 1, y);
gameObject.transform.position = target;

// convert bytes array to hex string
var hexString = BitConverter.ToString(player.data.ToArray()).Replace("-", "").ToLower();
var shortString = hexString.Substring(0, 8).TrimStart('0');

shortPlayerAddress = $"0x{shortString}";
}

void Update()
{
// our curent position is gameObject.transform.position
// move towards the target position
var step = 3.0f * Time.deltaTime;
// scale down our positions
var target = new Vector3(x, 0, y);
Vector3 oldPosition = gameObject.transform.position;
var target = new Vector3(x, oldPosition.y, y);
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, target, step);

// calculate and display velocity
Vector3 velocity = (gameObject.transform.position - oldPosition) / Time.deltaTime;
textTag.text = $"{shortPlayerAddress}\nVelocity: {velocity.magnitude}";

// if we are close enough to the target position, snap to it
if (Vector3.Distance(gameObject.transform.position, target) < 0.001f)
{
Expand Down
41 changes: 39 additions & 2 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ GameObject:
- component: {fileID: 963194228}
- component: {fileID: 963194227}
- component: {fileID: 963194226}
- component: {fileID: 963194229}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
Expand Down Expand Up @@ -406,6 +407,22 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 69.465, y: 0, z: 0}
--- !u!114 &963194229
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9601f4ab467894a87a34f22f6b453eb8, type: 3}
m_Name:
m_EditorClassIdentifier:
worldManager: {fileID: 2019443015}
height: 10
damping: 1
speed: 10
--- !u!1 &998234499
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -463,6 +480,7 @@ GameObject:
- component: {fileID: 1603019350}
- component: {fileID: 1603019349}
- component: {fileID: 1603019348}
- component: {fileID: 1603019352}
m_Layer: 0
m_Name: Plane
m_TagString: Untagged
Expand Down Expand Up @@ -551,12 +569,29 @@ Transform:
m_GameObject: {fileID: 1603019347}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.059999, y: -0.05, z: -0.089867}
m_LocalScale: {x: 4.482111, y: 1, z: 3.7630765}
m_LocalPosition: {x: 0.059959, y: -0.05, z: -0.089834}
m_LocalScale: {x: 579.44196, y: 1, z: 204.42252}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1603019352
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1603019347}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ab13a05e740f84a6d9c745e33f5bd407, type: 3}
m_Name:
m_EditorClassIdentifier:
width: 256
height: 256
scale: 20
offsetX: 100
offsetY: 100
--- !u!1 &2019443014
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -639,6 +674,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
x: 0
y: 0
textTag: {fileID: 0}
shortPlayerAddress:
--- !u!114 &2019443019
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
Loading

0 comments on commit 4746fd7

Please sign in to comment.