mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
tweaked scripts folder
This commit is contained in:
@@ -1,125 +0,0 @@
|
|||||||
import node;
|
|
||||||
|
|
||||||
//constants
|
|
||||||
var SPEED: int const = 10;
|
|
||||||
|
|
||||||
//variables
|
|
||||||
var parent: opaque = null;
|
|
||||||
var posX: int = 50;
|
|
||||||
var posY: int = 50;
|
|
||||||
var WIDTH: int const = 100;
|
|
||||||
var HEIGHT: int const = 100;
|
|
||||||
|
|
||||||
var xspeed: int = 0;
|
|
||||||
var yspeed: int = 0;
|
|
||||||
|
|
||||||
//accessors - variables are private, functions are public
|
|
||||||
fn getX(node: opaque) {
|
|
||||||
return posX;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn getY(node: opaque) {
|
|
||||||
return posY;
|
|
||||||
}
|
|
||||||
|
|
||||||
//lifecycle functions
|
|
||||||
fn onInit(node: opaque) {
|
|
||||||
print "render.toy:onInit() called\n";
|
|
||||||
|
|
||||||
node.loadTexture("sprites:/character.png");
|
|
||||||
parent = node.getNodeParent();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onStep(node: opaque) {
|
|
||||||
posX += xspeed;
|
|
||||||
posY += yspeed;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onFree(node: opaque) {
|
|
||||||
print "render.toy:onFree() called\n";
|
|
||||||
|
|
||||||
node.freeTexture();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
|
||||||
// print "render.toy:onDraw() called\n";
|
|
||||||
|
|
||||||
var px = parent.callNode("getX");
|
|
||||||
var py = parent.callNode("getY");
|
|
||||||
|
|
||||||
if (px == null) {
|
|
||||||
px = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (py == null) {
|
|
||||||
py = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
node.drawNode(posX + px, posY + py, WIDTH, HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
//event functions
|
|
||||||
fn onKeyDown(node: opaque, event: string) {
|
|
||||||
if (event == "character_up") {
|
|
||||||
yspeed -= SPEED;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event == "character_down") {
|
|
||||||
yspeed += SPEED;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event == "character_left") {
|
|
||||||
xspeed -= SPEED;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event == "character_right") {
|
|
||||||
xspeed += SPEED;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onKeyUp(node: opaque, event: string) {
|
|
||||||
if (event == "character_up" && yspeed < 0) {
|
|
||||||
yspeed = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event == "character_down" && yspeed > 0) {
|
|
||||||
yspeed = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event == "character_left" && xspeed < 0) {
|
|
||||||
xspeed = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event == "character_right" && xspeed > 0) {
|
|
||||||
xspeed = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) {
|
|
||||||
// print "entity.toy:onMouseMotion(" + string x + ", " + string y + ", " + string xrel + ", " + string yrel + ")\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
|
|
||||||
// print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")\n";
|
|
||||||
|
|
||||||
//jump to pos
|
|
||||||
posX = x - WIDTH / 2;
|
|
||||||
posY = y - HEIGHT / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
|
|
||||||
// print "entity.toy:onMouseButtonUp(" + string x + ", " + string y + ", " + button + ")\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onMouseWheel(node: opaque, xrel: int, yrel: int) {
|
|
||||||
// print "entity.toy:onMouseWheel(" + string xrel + ", " + string yrel + ")\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
//single line comment
|
|
||||||
|
|
||||||
/*
|
|
||||||
multi line comment
|
|
||||||
*/
|
|
||||||
|
|
||||||
//test primitive literals
|
|
||||||
print "hello world";
|
|
||||||
print null;
|
|
||||||
print true;
|
|
||||||
print false;
|
|
||||||
print 42;
|
|
||||||
print 3.14;
|
|
||||||
print -69;
|
|
||||||
print -4.20;
|
|
||||||
print 2 + (3 * 3);
|
|
||||||
|
|
||||||
//test operators (integers)
|
|
||||||
print 1 + 1;
|
|
||||||
print 1 - 1;
|
|
||||||
print 2 * 2;
|
|
||||||
print 1 / 2;
|
|
||||||
print 4 % 2;
|
|
||||||
|
|
||||||
//test operators (floats)
|
|
||||||
print 1.0 + 1.0;
|
|
||||||
print 1.0 - 1.0;
|
|
||||||
print 2.0 * 2.0;
|
|
||||||
print 1.0 / 2.0;
|
|
||||||
|
|
||||||
//test scopes
|
|
||||||
{
|
|
||||||
print "This statement is within a scope.";
|
|
||||||
{
|
|
||||||
print "This is a deeper scope.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
print "Back to the outer scope.";
|
|
||||||
|
|
||||||
//test scope will delegate to higher scope
|
|
||||||
var a = 1;
|
|
||||||
{
|
|
||||||
a = 2;
|
|
||||||
print a;
|
|
||||||
}
|
|
||||||
print a;
|
|
||||||
|
|
||||||
//test scope will shadow higher scope on redefine
|
|
||||||
var b: int = 3;
|
|
||||||
{
|
|
||||||
var b = 4;
|
|
||||||
print b;
|
|
||||||
}
|
|
||||||
print b;
|
|
||||||
|
|
||||||
//test compounds, repeatedly
|
|
||||||
print [1, 2, 3];
|
|
||||||
print [4, 5];
|
|
||||||
print ["key":"value"];
|
|
||||||
print [1, 2, 3];
|
|
||||||
print [4, 5];
|
|
||||||
print ["key":"value"];
|
|
||||||
|
|
||||||
//test empties
|
|
||||||
print [];
|
|
||||||
print [:];
|
|
||||||
|
|
||||||
//test nested compounds
|
|
||||||
print [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
|
|
||||||
|
|
||||||
//var declarations
|
|
||||||
var x = 31;
|
|
||||||
var y : int = 42;
|
|
||||||
var arr : [int] = [1, 2, 3, 42];
|
|
||||||
var dict : [string:int] = ["hello": 1, "world":2];
|
|
||||||
|
|
||||||
//printing expressions
|
|
||||||
print x;
|
|
||||||
print x + y;
|
|
||||||
print arr;
|
|
||||||
print dict;
|
|
||||||
|
|
||||||
//test asserts at the end of the file
|
|
||||||
assert x, "This won't be seen";
|
|
||||||
assert true, "This won't be seen";
|
|
||||||
assert false, "This is a failed assert, and will end execution";
|
|
||||||
|
|
||||||
print "This will not be printed because of the above assert";
|
|
||||||
|
|
||||||
@@ -34,8 +34,8 @@ var tiles: [[int]] const = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
var tileset: [int: string] const = [
|
var tileset: [int: string] const = [
|
||||||
0: " ",
|
0: " ",
|
||||||
1: " X "
|
1: "X "
|
||||||
];
|
];
|
||||||
|
|
||||||
//variables
|
//variables
|
||||||
@@ -48,7 +48,7 @@ fn draw() {
|
|||||||
for (var i: int = 0; i < WIDTH; i++) {
|
for (var i: int = 0; i < WIDTH; i++) {
|
||||||
//draw the player pos
|
//draw the player pos
|
||||||
if (i == posX && j == posY) {
|
if (i == posX && j == posY) {
|
||||||
print " O ";
|
print "O ";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
scripts/roguelike-seeds.toy
Normal file
36
scripts/roguelike-seeds.toy
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Since this is a pseudo-random generator, and there's no internal state to the algorithm other
|
||||||
|
than the generator opaque, there needs to be a "call counter" (current depth) to shuffle the
|
||||||
|
initial seeds, otherwise generators created from other generators will resemble their parents,
|
||||||
|
but one call greater.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
import standard;
|
||||||
|
import random;
|
||||||
|
|
||||||
|
var DEPTH: int const = 20;
|
||||||
|
var levels = [];
|
||||||
|
|
||||||
|
//generate the level seeds
|
||||||
|
var generator: opaque = createRandomGenerator(clock().hash());
|
||||||
|
|
||||||
|
for (var i: int = 0; i < DEPTH; i++) {
|
||||||
|
levels.push(generator.generateRandomNumber());
|
||||||
|
}
|
||||||
|
|
||||||
|
generator.freeRandomGenerator();
|
||||||
|
|
||||||
|
//generate "levels" of a roguelike
|
||||||
|
for (var i = 0; i < DEPTH; i++) {
|
||||||
|
var rng: opaque = createRandomGenerator(levels[i] + i);
|
||||||
|
|
||||||
|
print "---";
|
||||||
|
print levels[i];
|
||||||
|
print rng.generateRandomNumber();
|
||||||
|
print rng.generateRandomNumber();
|
||||||
|
print rng.generateRandomNumber();
|
||||||
|
|
||||||
|
rng.freeRandomGenerator();
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import standard;
|
|
||||||
import random;
|
|
||||||
|
|
||||||
for (var i: int = 0; i < 1_000_000; i++) {
|
|
||||||
var generator: opaque = createRandomGenerator(clock().hash());
|
|
||||||
|
|
||||||
print generator.generateRandomNumber();
|
|
||||||
print generator.generateRandomNumber();
|
|
||||||
print generator.generateRandomNumber();
|
|
||||||
|
|
||||||
generator.freeRandomGenerator();
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user