Newer
Older
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
modeltype Unity uses 'http://www.example.org/unityGameDSL_modded';
modeltype Prog uses 'http://www.example.org/examples/example.ecore';
transformation UgameTransform(in unity: Unity, out Prog);
property monoBehaviour : Prog::ExternalType;
property vector2 : Prog::ExternalType;
property rigidbody : Prog::ExternalType;
property text : Prog::ExternalType;
property collider : Prog::ExternalType;
property collision : Prog::ExternalType;
property gameObject : Prog::ExternalType;
property image : Prog::ExternalType;
property FIXED_NAMESPACE : String = 'DSL';
main() {
monoBehaviour := object Prog::ExternalType {
importedFrom := 'UnityEngine';
identifier := 'MonoBehaviour';
type := EType::_class;
};
vector2 := object Prog::ExternalType {
importedFrom := 'UnityEngine';
identifier := 'Vector2';
type := EType::struct;
};
rigidbody := object Prog::ExternalType {
importedFrom := 'UnityEngine';
identifier := 'Rigidbody2D';
type := EType::_class;
};
text := object Prog::ExternalType {
importedFrom := 'UnityEngine.UI';
identifier := 'Text';
type := EType::_class;
};
image := object Prog::ExternalType {
importedFrom := 'UnityEngine.UI';
identifier := 'Image';
type := EType::_class;
};
collider := object Prog::ExternalType {
importedFrom := 'UnityEngine';
identifier := 'Collider2D';
type := EType::_class;
};
collision := object Prog::ExternalType {
importedFrom := 'UnityEngine';
identifier := 'Collision2D';
type := EType::_class;
};
gameObject := object Prog::ExternalType {
importedFrom := 'UnityEngine';
identifier := 'GameObject';
type := EType::_class;
};
unity.rootObjects()[Unity::Game]->asOrderedSet()->first()-> map Unity2Prog()
}
mapping Unity::Game::Unity2Prog () : Prog::Program {
externalTypes += monoBehaviour;
externalTypes += vector2;
externalTypes += rigidbody;
externalTypes += text;
externalTypes += image;
externalTypes += collider;
externalTypes += collision;
externalTypes += gameObject;
var types : Set(InternalType);
types += self.dataManager->map DataManager2Class();
types += self.guiElements->map GUIElement2Class();
// convert all types into separate files!
files += types-> map Internal2File();
}
identifier := self.name.firstToUpper();
log('GameObject to class: ' + identifier);
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var startMethod := object Prog::MethodDef {
accessability := AccessModifier::none;
returnType := toType(EPrimitiveType::void);
identifier := 'Start'
};
var updateMethod := object Prog::MethodDef {
accessability := AccessModifier::none;
returnType := toType(EPrimitiveType::void);
identifier := 'Update'
};
var triggerMethod := object MethodDef {
identifier := "OnTriggerEnter2D";
parameters += object Parameter {
identifier := 'collider';
type := toType(collider);
};
returnType := toType(EPrimitiveType::void);
};
methods += updateMethod;
methods += startMethod;
methods += triggerMethod;
baseType := monoBehaviour;
var speed : MemberDef;
var horizontal : MemberDef;
if (self.movement->size() > 0) {
speed := object MemberDef {
identifier := 'speed';
type := toType(EPrimitiveType::float);
};
horizontal:= object MemberDef {
identifier := 'horizontal';
type := toType(EPrimitiveType::float);
accessability := AccessModifier::private;
};
members += speed;
members += horizontal;
};
var rigidbody : MemberDef;
if (self.colision->select(c | c.oclIsTypeOf(RigidBody))->size() > 0) {
// you can ignore this warning
rigidbody := object MemberDef {
identifier := 'rigidbody';
type := toType(this.rigidbody);
};
members += rigidbody;
};
// sideways only: isGrounded, jumpIntensity, collisionmethod, triggermethod
// topdown only: vertical
// both: start, update, rigidbody, horizontal, speed
if (self.movement->size() = 1) {
if (self.movement->asOrderedSet()->first().oclIsTypeOf(Sideways)) {
var isGrounded := object MemberDef {
identifier := 'isGrounded';
type := toType(EPrimitiveType::bool);
accessability := AccessModifier::private;
};
var jumpIntensity:= object MemberDef {
identifier := 'jumpIntensity';
type := toType(EPrimitiveType::float);
};
var collisionMethod := object MethodDef {
identifier := "OnCollisionEnter2D";
parameters += object Parameter {
identifier := 'collision';
type := toType(collision);
};
returnType := toType(EPrimitiveType::void);
};
members += isGrounded;
members += jumpIntensity;
methods += collisionMethod;
updateMethod.body += horizontal.identifier + ' = Input.GetAxisRaw("Horizontal");';
updateMethod.body +=
'if (Input.GetButtonDown("Jump") && ' + isGrounded.identifier + ') {
' + rigidbody.identifier + '.AddForce(new Vector2(0,1) * ' + jumpIntensity.identifier + ');
updateMethod.body += rigidbody.identifier + '.AddForce(new Vector2(' + horizontal.identifier + '* ' + speed.identifier + ', 0));';
collisionMethod.body += 'if (collision.collider.gameObject.tag == "Floor"){
} else {
var vertical := object MemberDef {
identifier := 'vertical';
type := toType(EPrimitiveType::float);
accessability := AccessModifier::private;
};
members += vertical;
updateMethod.body += horizontal.identifier + ' = Input.GetAxisRaw("Horizontal");';
updateMethod.body += vertical.identifier + ' = Input.GetAxisRaw("Vertical");';
updateMethod.body += rigidbody.identifier + '.velocity = new Vector2(' + horizontal.identifier + ' * ' + speed.identifier + ', ' + vertical.identifier + ' * ' + speed.identifier + ');';
};
};
var createCounter : Integer = 0;
--childrens with object creation.
self.start->forEach(e){
if (e.oclIsKindOf(Unity::ObjectCreation)){
var prefab: MemberDef;
object prefab: MemberDef{
accessability := AccessModifier::public;
type := toType(gameObject);
identifier := "createdObject"+createCounter.toString();
createCounter := createCounter + 1;
};
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
};
};
self.update->forEach(e){
if (e.oclIsKindOf(Unity::ObjectCreation)){
var prefab: MemberDef;
object prefab: MemberDef{
accessability := AccessModifier::public;
type := toType(gameObject);
identifier := "createdObject"+createCounter.toString();
createCounter := createCounter + 1;
};
members+=prefab;
};
};
self.interaction.execute->forEach(e){
if (e.oclIsKindOf(Unity::ObjectCreation)){
var prefab: MemberDef;
object prefab: MemberDef{
accessability := AccessModifier::public;
type := toType(gameObject);
identifier := "createdObject"+createCounter.toString();
createCounter := createCounter + 1;
};
members+=prefab;
};
};
self.colision->forEach(c | c.oclIsKindOf(Unity::Trigger)){
c.oclAsType(Unity::Trigger).execute->forEach(e){
if (e.oclIsKindOf(Unity::ObjectCreation)){
var prefab: MemberDef;
object prefab: MemberDef{
accessability := AccessModifier::public;
type := toType(gameObject);
identifier := "createdObject"+createCounter.toString();
createCounter := createCounter + 1;
};
members+=prefab;
};
};
};
createCounter := 0;
-- triggers
self.colision->forEach(e | e.oclIsTypeOf(Unity::Trigger)){
e.oclAsType(Unity::Trigger)->SetTriggerBody(triggerMethod, createCounter);
};
-- start
self.start->forEach(action){
startMethod.body += action.Action2Body(createCounter);
};
-- update
self.update->forEach(action){
updateMethod.body += action.Action2Body(createCounter);
};
-- KeyPress
self.interaction->forEach(keypress | keypress.oclIsTypeOf(Unity::Actionkey)){
keypress.oclAsType(Unity::Actionkey)->SetActionKeyBody(updateMethod, createCounter);
};
}
helper Unity::Actionkey::SetActionKeyBody(inout updatemethod:Prog::MethodDef,inout creationCounter:Integer){
if (self.pressType = KeyType::Instant) then {
updatemethod.body +='if (Input.GetKeyDown(KeyCode.'+self.key+')) {';
}
else if (self.pressType = KeyType::Repeated) then {
updatemethod.body +='if (Input.GetKey(KeyCode.'+self.key+')) {'
}
endif endif;
self.execute->forEach(action){
updatemethod.body += action.Action2Body(creationCounter);
};
updatemethod.body+='}'
}
helper Unity::Trigger::SetTriggerBody(inout triggermethod:Prog::MethodDef,inout creationCounter: Integer) {
log('INTERMEDIATE RESULT ' + self.collisionTAg);
triggermethod.body->forEach(s) {
log(s);
};
log('');
triggermethod.body += 'if (collider.gameObject.tag == "'+self.collisionTAg+'") {';
self.execute->forEach(action){
var res := action.Action2Body(creationCounter);
log(res);
triggermethod.body += res;
};
triggermethod.body += '}';
log('FINAL RESULT ' + self.collisionTAg);
triggermethod.body->forEach(s) {
log(s);
};
log('');
}
helper Unity::GameAction::Action2Body(inout creationCounter: Integer) : String {
//log('action to body type: ' + self.toString());
if (self.oclIsTypeOf(Unity::Add)){
var value =self.oclAsType(Unity::Add).value;
return 'DataManager.instance.Set'+self.oclAsType(Unity::DataChange).data.name.firstToUpper()+'(DataManager.instance.Get'+self.oclAsType(Unity::DataChange).data.name.firstToUpper()+'()+'+value+');';
};
if (self.oclIsTypeOf(Unity::Subtract)){
var value =self.oclAsType(Unity::Subtract).value;
return 'DataManager.instance.Set'+self.oclAsType(Unity::DataChange).data.name.firstToUpper()+'(DataManager.instance.Get'+self.oclAsType(Unity::DataChange).data.name.firstToUpper()+'()-'+value+');';
};
if (self.oclIsTypeOf(Unity::Define)){
var value =self.oclAsType(Unity::Define).value;
return 'DataManager.instance.Set'+self.oclAsType(Unity::DataChange).data.name.firstToUpper()+'('+value+');';
};
if(self.oclIsTypeOf(Unity::SelfDestruction)){
return 'Destroy(gameObject);'
};
if(self.oclIsTypeOf(Unity::ApplyForce)){
if (self.oclAsType(Unity::ApplyForce).isInstant){
return 'rigidbody.AddForce(new Vector2('+self.oclAsType(Unity::ApplyForce).direction->at(1).toString()+','+self.oclAsType(Unity::ApplyForce).direction->at(2).toString()+')*'+self.oclAsType(Unity::ApplyForce).intensity.toString()+'f, ForceMode2D.Impulse);'
return 'rigidbody.AddForce(new Vector2('+self.oclAsType(Unity::ApplyForce).direction->at(1).toString()+','+self.oclAsType(Unity::ApplyForce).direction->at(2).toString()+')*'+self.oclAsType(Unity::ApplyForce).intensity.toString()+'f);'
};
};
if(self.oclIsTypeOf(Unity::ObjectCreation)){
var currentCounter: Integer = creationCounter;
creationCounter := creationCounter + 1;
return 'Instantiate(createdObject'+currentCounter.toString()+', transform.position + new Vector3('+self.oclAsType(Unity::ObjectCreation).position->at(1).toString()+','+self.oclAsType(Unity::ObjectCreation).position->at(2).toString()+',0),createdObject'+currentCounter.toString()+'.transform.rotation);'
log('failed to convert action ' + self.toString());
}
// creates a class from GUIElement, possibly TextDisplay or BarDisplay
mapping Unity::GUIElement::GUIElement2Class() : Prog::Class {
var isTextual := self.oclIsTypeOf(Unity::TextDisplay);
baseType := monoBehaviour;
var update := object MethodDef {
accessability := AccessModifier::none;
returnType := toType(EPrimitiveType::void);
identifier := "Update"
};
if (isTextual) {
var value := object MemberDef {
accessability := AccessModifier::public;
identifier := 'text';
};
var prefix := object MemberDef {
accessability := AccessModifier::public;
type := toType(EPrimitiveType::string);
identifier := 'prefix';
};
var postfix := object MemberDef {
accessability := AccessModifier::public;
type := toType(EPrimitiveType::string);
identifier := 'postfix';
};
members += value;
members += prefix;
members += postfix;
update.body += value.identifier + '.text = ' + prefix.identifier + ' + DataManager.instance.Get'+self.data.name.firstToUpper()+ '() +' + postfix.identifier + ';';
var max := object MemberDef {
accessability := AccessModifier::public;
type := toType(EPrimitiveType::float);
identifier := 'maxValue';
};
var bar := object MemberDef {
accessability := AccessModifier::public;
type := toType(image);
identifier := 'barImage';
};
update.body += 'barImage.fillAmount = DataManager.instance.Get'+self.data.name.firstToUpper()+"()/maxValue;";
members += max;
members += bar;
};
methods += update;
}
mapping Unity::DataManager::DataManager2Class () : Prog::Class {
identifier := 'DataManager';
var instance := object MemberDef {
specifier := Specifier::_static;
identifier := 'instance';
type := toType(result);
};
members += instance;
members += self.controls -> map Data2Member();
var awake := object MethodDef {
identifier := 'Awake';
accessability := AccessModifier::private;
body += instance.identifier + '??= this;';
returnType := toType(EPrimitiveType::void);
};
methods += awake;
methods += self.controls-> map Data2Get();
methods += self.controls-> map Data2Set();
}
mapping Unity::Data::Data2Member(): Prog::MemberDef{
type := toType(self.type);
identifier := self.name;
}
mapping Unity::Data::Data2Get(): Prog::MethodDef{
accessability := AccessModifier::public;
returnType := toType(self.type);
identifier := "Get"+self.name.firstToUpper();
body += "return this."+self.name + ';';
}
mapping Unity::Data::Data2Set(): Prog::MethodDef{
init{
var parameter := object Prog::Parameter{
type:= toType(self.type);
identifier := "value"
}
}
accessability := AccessModifier::public;
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
identifier := "Set"+self.name.firstToUpper();
parameters += parameter;
body += 'this.' + self.name + ' = value;';
}
// converts unity data type to primitive types
query toType(in type : DataTypes) : Prog::PrimitiveType {
return if type = DataTypes::Integer then toType(EPrimitiveType::int)
else if type = DataTypes::Decimal then toType(EPrimitiveType::float)
else toType(EPrimitiveType::string) endif endif;
}
// converts InternalType to file
mapping InternalType::Internal2File() : Prog::File {
filename:= self.identifier.firstToUpper();
namespaces += object Namespace {
namespace := FIXED_NAMESPACE;
using += 'UnityEngine';
using += 'System';
typedefs += self;
};
}
// converts a primitive type to a Type
query toType(in _type: EPrimitiveType) : Prog::PrimitiveType {
return object Prog::PrimitiveType {
type := _type;
};
}
// converts a TypeDef to a Type through reference
query toType(in _type: TypeDef) : Prog::CompoundType {
return object Prog::CompoundType {
referencedType := _type;
}
}