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
85 lines (76 loc) · 1.7 KB
/
Game.cpp
File metadata and controls
85 lines (76 loc) · 1.7 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
/// <summary>
/// Game-specific code for the native plugin
/// </summary>
/// <author>
/// Jackson Dunstan, 2017, http://JacksonDunstan.com
/// </author>
/// <license>
/// MIT
/// </license>
#include "Bindings.h"
#include "Game.h"
using namespace System;
using namespace UnityEngine;
namespace
{
struct GameState
{
float BallDir;
};
GameState* gameState;
}
namespace MyGame
{
void BallScript::Update()
{
Transform transform = GetTransform();
Vector3 pos = transform.GetPosition();
const float speed = 1.2f;
const float min = -1.5f;
const float max = 1.5f;
float distance = Time::GetDeltaTime() * speed * gameState->BallDir;
Vector3 offset(distance, 0, 0);
Vector3 newPos = pos + offset;
if (newPos.x > max)
{
gameState->BallDir *= -1.0f;
newPos.x = max - (newPos.x - max);
if (newPos.x < min)
{
newPos.x = min;
}
}
else if (newPos.x < min)
{
gameState->BallDir *= -1.0f;
newPos.x = min + (min - newPos.x);
if (newPos.x > max)
{
newPos.x = max;
}
}
transform.SetPosition(newPos);
}
}
// 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);
// The ball initially goes right
gameState->BallDir = 1.0f;
// Create the ball game object out of a sphere primitive
GameObject go = GameObject::CreatePrimitive(PrimitiveType::Sphere);
String name("GameObject with a BallScript");
go.SetName(name);
// Attach the ball script to make it bounce back and forth
go.AddComponent<MyGame::BaseBallScript>();
}
}