forked from jacksondunstan/UnityNativeScripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
116 lines (102 loc) · 2.52 KB
/
Copy pathGame.cpp
File metadata and controls
116 lines (102 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/// <summary>
/// Game-specific code for the native plugin
/// </summary>
/// <author>
/// Jackson Dunstan, 2017, http://JacksonDunstan.com
/// </author>
/// <license>
/// MIT
/// </license>
#include "Bindings.h"
using namespace System;
using namespace UnityEngine;
namespace
{
struct GameState
{
int32_t NumCreated;
float Dir;
};
GameState* gameState;
}
// Called when the plugin is initialized
// This is mostly full of test code. Feel free to remove it all.
void PluginMain(
void* memory,
int32_t memorySize,
bool isFirstBoot)
{
gameState = (GameState*)memory;
if (isFirstBoot)
{
String message("Game booted up");
Debug::Log(message);
gameState->NumCreated = 0;
gameState->Dir = 1.0f;
String name("GameObject with a TestScript");
GameObject go(name);
go.AddComponent<MyGame::MonoBehaviours::TestScript>();
}
}
void MyGame::MonoBehaviours::TestScript::Awake()
{
String message("C++ TestScript Awake");
Debug::Log(message);
}
void MyGame::MonoBehaviours::TestScript::OnAnimatorIK(int32_t param0)
{
String message("C++ TestScript OnAnimatorIK");
Debug::Log(message);
}
void MyGame::MonoBehaviours::TestScript::OnCollisionEnter(UnityEngine::Collision& param0)
{
String message("C++ TestScript OnCollisionEnter");
Debug::Log(message);
}
void MyGame::MonoBehaviours::TestScript::Update()
{
if (gameState->NumCreated < 10)
{
GameObject go;
Transform transform = go.GetTransform();
float comp = (float)gameState->NumCreated;
Vector3 position(comp, comp*10.0f, comp*100.0f);
transform.SetPosition(position);
gameState->NumCreated++;
if (gameState->NumCreated == 10)
{
String message("Done spawning game objects");
Debug::Log(message);
GameObject go = GameObject::CreatePrimitive(PrimitiveType::Sphere);
String name("GameObject with an AnotherScript");
go.SetName(name);
go.AddComponent<MyGame::MonoBehaviours::AnotherScript>();
}
}
}
void MyGame::MonoBehaviours::AnotherScript::Awake()
{
String message("C++ AnotherScript Awake");
Debug::Log(message);
}
void MyGame::MonoBehaviours::AnotherScript::Update()
{
Transform transform = GetTransform();
Vector3 pos = transform.GetPosition();
const float speed = 1.2f;
const float min = -1.5f;
const float max = 1.5f;
Vector3 offset(Time::GetDeltaTime() * speed * gameState->Dir, 0, 0);
Vector3 newPos = pos + offset;
if (newPos.x > max)
{
gameState->Dir *= -1.0f;
newPos.x = max - (newPos.x - max);
}
else if (newPos.x < min)
{
gameState->Dir *= -1.0f;
newPos.x = min + (min - newPos.x);
}
transform.SetPosition(newPos);
}