use strict Parameters defined here will be available on the args objec

  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
"use strict";
/**
* Parameters defined here will be available on the 'args' object as 'args.key'
* and customizable using the script panel. Parameters are defined like below.
* 'key', 'type', and 'default' are required properties.
*/
var parameters = [{
name: "Entity",
key: "hiddenEntity",
type: "entity"
}];
/**
* Implement this method to initialize your script.
* Called when pressing play and when running exported projects.
*/
var setup = function(args, ctx, goo) {
ctx.entityData.isKeyPressed = {
a: false,
space: false,
left: false,
right: false
};
ctx.entityData.wasKeyPressed = {
left: false,
right: false
};
ctx.onKeyDown = function(event) {
switch (event.keyCode) {
case 65:
ctx.entityData.isKeyPressed.a = true;
break;
case 32:
ctx.entityData.isKeyPressed.space = true;
break;
case 37:
ctx.entityData.isKeyPressed.left = true;
break;
case 39:
ctx.entityData.isKeyPressed.right = true;
break;
}
if (ctx.entityData.isKeyPressed.a) {
ctx.entity.meshRendererComponent.materials[0].uniforms = ctx.entityData.redMaterial;
}
event.preventDefault();
};
ctx.onKeyUp = function(event) {
switch (event.keyCode) {
case 65:
ctx.entityData.isKeyPressed.a = false;
break;
case 32:
ctx.entityData.isKeyPressed.space = false;
break;
case 37:
ctx.entityData.isKeyPressed.left = false;
ctx.entityData.wasKeyPressed.left = false;
break;
case 39:
ctx.entityData.isKeyPressed.right = false;
ctx.entityData.wasKeyPressed.right = false;
break;
}
if (!ctx.entityData.isKeyPressed.a) {
ctx.entity.meshRendererComponent.materials[0].uniforms = ctx.entityData.whiteMaterial;
}
};
window.onkeydown = ctx.onKeyDown;
window.onkeyup = ctx.onKeyUp;
ctx.entityData.whiteMaterial = ctx.entity.meshRendererComponent.materials[0].uniforms;
ctx.entityData.redMaterial = args.hiddenEntity.meshRendererComponent.materials[0].uniforms;
};
/**
* Implement this method to do cleanup.
* Called when the script is stopped or deleted.
*/
var cleanup = function(args, ctx, goo) {
window.onkeydown = null;
window.onkeyup = null;
};
var K = 0.1;
/**
* This function will be called every frame.
*/
var update = function(args, ctx, goo) {
var angle = 0;
if (ctx.entityData.isKeyPressed.left && !ctx.entityData.wasKeyPressed.left) {
angle = -K;
ctx.entityData.wasKeyPressed.left = true;
} else if (ctx.entityData.isKeyPressed.right && !ctx.entityData.wasKeyPressed.right) {
angle = K;
ctx.entityData.wasKeyPressed.right = true;
}
if (ctx.entityData.isKeyPressed.space) {
angle = K/5;
}
ctx.entity.addRotation(0, angle, 0);
};