We have the technology!
hartnell 07 Sep 2007 06:29
I was working on the lib and realized that I needed an engine to do most of the stuff I wanted to do. So, I created a standard game object in the lib and a way to create one. createGameObject() creates an game object in the gameObjects[] array, BUT returns a reference to the created object.
So, in this test, I create a game object, draw it's position with the reference newObj and then move it every loopin directly. Holy crap!
Below is the lib and the test. No external files needed.
The lib :
var vk_windowx = 0; var vk_escape = 1; var vk_1 = 2; var vk_2 = 3; var vk_3 = 4; var vk_4 = 5; var vk_5 = 6; var vk_6 = 7; var vk_7 = 8; var vk_8 = 9; var vk_9 = 10; var vk_0 = 11; var vk_minus = 12; var vk_equals = 13; var vk_backspace = 14; var vk_tab = 15; var vk_q = 16; var vk_w = 17; var vk_e = 18; var vk_r = 19; var vk_t = 20; var vk_y = 21; var vk_u = 22; var vk_i = 23; var vk_o = 24; var vk_p = 25; var vk_rbracket = 26; var vk_lbracket = 27; var vk_enter = 28; var vk_control = 29; var vk_a = 30; var vk_s = 31; var vk_d = 32; var vk_f = 33; var vk_g = 34; var vk_h = 35; var vk_j = 36; var vk_k = 37; var vk_l = 38; var vk_semicolon = 39; var vk_quote = 40; var vk_tilde = 41; var vk_lshift = 42; var vk_backslash = 43; var vk_z = 44; var vk_x = 45; var vk_c = 46; var vk_v = 47; var vk_b = 48; var vk_n = 49; var vk_m = 50; var vk_comma = 51; var vk_period = 52; var vk_slash = 53; var vk_rshift = 54; var vk_multiply = 55; var vk_alt = 56; var vk_space = 57; var vk_capslock = 58; var vk_f1 = 59; var vk_f2 = 60; var vk_f3 = 61; var vk_f4 = 62; var vk_f5 = 63; var vk_f6 = 64; var vk_f7 = 65; var vk_f8 = 66; var vk_f9 = 67; var vk_f10 = 68; var vk_numlock = 69; var vk_scrolllock = 70; var vk_home = 71; var vk_up = 72; var vk_pageup = 73; var vk_left = 75; var vk_right = 77; var vk_plus = 78; var vk_end = 79; var vk_down = 80; var vk_pagedown = 81; var vk_insert = 82; var vk_delete = 83; var vk_f11 = 87; var vk_f12 = 88; var vk_lwin = 125; var vk_rwin = 126; var vk_menu = 127; var c_aliceblue = Argb(0xFF,0xF0,0xF8,0xFF); var c_antiquewhite = Argb(0xFF,0xFA,0xEB,0xE7); var c_aqua = Argb(0xFF,0x00,0xFF,0xFF); var c_aquamarine = Argb(0xFF,0x7F,0xFF,0xD4); var c_azure = Argb(0xFF,0xF0,0xFF,0xFF); var c_black = Argb(0xFF,0x0,0x0,0x0); var c_brown = Argb(0xFF,0xA5,0x2A,0x2A); var c_blue = Argb(0xFF,0x0,0x0,0xFF); var c_cyan = Argb(0xFF,0x0,0xFF,0xFF); var c_gray = Argb(0xFF,0x80,0x80,0x80); var c_green = Argb(0xFF,0x0,0xFF,0x0); var c_lightblue = Argb(0xFF,0xAD,0xD8,0xE6); var c_orange = Argb(0xFF,0xFF,0xA5,0x00); var c_red = Argb(0xFF,0xFF,0x0,0x0); var c_yellow = Argb(0xFF,0xFF,0xFF,0x0); var pi = 3.141592654; var gameObjects = new Array(); function createGameObject() { var i; //check if array empty var arrLength = gameObjects.length; if (arrLength <= 0) { gameObjects[1] = new gameObject(); return gameObjects[1]; } //check for an empty spot for (i;i<=arrLength;i++) { if (gameObjects[i] == 0) { gameObjects[i] = new gameObject(); return gameObjects[i]; } } //lastly, just throw one at the end of array gameObjects[arrLength] = new gameObject(); return gameObjects[arrLength]; } function gameObject() { this.x = 0; // x-position of object this.y = 0; // y-position of object this.xStart = 0; // starting x location this.yStart = 0; // starting y location this.xPrevious = 0; // x-position last loopin this.yPrevious = 0; // y-position last loopin this.hSpeed = 0; // horizontal speed of object this.vSpeed = 0; // vertical speed of object this.direction = 0; // direction of motion this.spriteIndex = 0; // index of the sprite this.imageIndex = 0; // current frame this.imageSpeed = 0; // frame speed this.gravity = 0; // pull of gravity on object this.gravityDir = 0; // direction of pull of gravity this.friction = 0; // the force of friction per loopin this.id = 0; // the unique id of the object this.type = 0; // the type of object }
The test :
include("gdn_stdlib.bjs"); Graphics.Initialize(); Key.Initialize(); var gameOver = false; var newObj = createGameObject(); newObj.x = 100; newObj.y = 100; while (gameOver != true) { if (Key.Pressed(vk_escape) || Key.Pressed(vk_windowx)) { gameOver = true; } Graphics.Clear(); Graphics.SetPoint(newObj.x, newObj.y,c_azure); Graphics.Display(); Graphics.SetFPS(30); gameObjects[1].x++; } Graphics.Terminate(); Key.Initialize();