BlueGit

timfa frameworkv3

Files

frameworkv3 / examples / space / main.js

main.js

Initial Commit
timfa Tim Falken · pushed by timfa · 10 years ago
d075adff History
main.js 7.0 KiB Download Raw
1$(init);
2
3//load in images
4var s_bullet = new image("images/bullet.gif");
5var s_ship = new image("images/ship.png");
6var s_star = new image("images/star.png");
7var s_timer = new image("images/timerPlaceholder.png", 10, 10); //10 subimages, with 10 per row. (So just one row in this example.)
8
9var soundsFolder = "sounds";
10var sounds =
11[
12 "shoot"
13]; //List of all sound files as strings (do not include file extension)
14
15function init()
16{
17 Game.initialize(20, 20, 500, 500, 60, gameStart); //Start game engine. Create a window at 20,20, width 500 and height 500, 60 frames per second. As soon as loading is done, it'll call gameStart().
18}
19
20function gameStart()
21{
22 GameWindow.backgroundColor = {r:0, g:0, b:0};
23
24 Game.setUpdateFunction(update); //Let the game engine know we want to link our 'update()' to the game's update step.
25
26 GameAudio.init(sounds, soundsFolder);
27
28 GameObject.create(backgroundTemplate, 250, 250); //create the background object! (Big black block)
29
30 player = GameObject.create(player, 250, 250); //create the player ship at the center of the screen
31 GameObject.create(block, 300, 300); //create an empty block which does nothing
32
33 for(var i = 0; i < 20; i++) //create a bunch of stars!
34 GameObject.create(star, Math.random() * GameWindow.width, Math.random() * GameWindow.height);
35}
36
37function update()
38{
39 for(var i = 0; i < Game.activeObjects.length; i++)//loop through all existing objects
40 {
41 if(Game.activeObjects[i].name != "player") //if the current object is not the player...
42 {
43 var obj = Game.activeObjects[i]; //put object reference into a variable
44 obj.transform.position = obj.transform.position.add(player.velocity.stretch(-1)); //move object according to player's velocity vector.
45
46 /*
47 * We're doing this to create an illusion of movement. By moving everything but the player,
48 * it will appear as if the player is moving, and the camera follows.
49 * Objects can still manipulate their position.
50 */
51
52 if(obj.transform.position.distanceTo(player.transform.position) > 1000) //If the object is really far away...
53 obj.destroy(); //just delete it. (If you're expanding this, make sure not to delete important objects!)
54 }
55 }
56 //You can add code here which will be executed every frame. (recommended to do this in individual objects whenever possible)
57}
58
59var block =
60{
61 start: function()
62 {
63 this.renderer.image = s_timer;
64 },
65
66 update: function(deltaTime)
67 {
68 this.renderer.subImage += deltaTime;
69 }
70}
71
72var bullet =
73{
74 start: function()
75 {
76 this.additionalSpeed = player.velocity.getCopy(); //Bullets copy the initial speed of the shooter. Like throwing an apple out of a moving car; it'll still be moving along with the car until it hits the ground and explodes. Waste of an apple, really.
77 this.renderer.image = s_bullet;
78 this.transform.width = 10;
79 this.transform.height = 30; //it's a bit stretched, but it'll look fun that way. Collision detection will be off, but there are no enemies here anyway so whatever.
80 },
81
82 update: function()
83 {
84 this.transform.position = this.transform.position.add( this.transform.forward().stretch(25) ).add(this.additionalSpeed); //Make bullet move forwards!
85 },
86
87 onCollision: function(other)
88 {
89 if(other.name != "player") //If i'm colliding with something that's not the player, delete myself.
90 this.destroy();
91 }
92}
93
94var star =
95{
96 start: function()
97 {
98 this.ignoreCollisions = true; //Stars are background objects, so we don't want to check for collisions.
99 this.name = "star"; //It's a star, what else is there to say?
100 this.transform.width = 2; //tiny star.
101 this.transform.height = 2;
102 this.color = "white"; //You can change this, but most stars are white so I made these white too.
103 this.renderer.image = s_star;
104 },
105
106 update: function()
107 {
108 if(this.transform.position.x < 0 || this.transform.position.x > GameWindow.width || this.transform.position.y < 0 || this.transform.position.y > GameWindow.height)
109 this.reposition(); //If I'm at the border of the screen, or past it, reposition myself!
110 },
111
112 reposition: function()
113 {
114 //Put myself at a random spot at a screen border
115
116 if(Math.random() > 0.5) //Horizontal of vertical? Coin flip it is!
117 {
118 this.transform.position.x = (Math.random() > 0.5? 0 : GameWindow.width); //Another coinflip to decide which side to go to.
119 this.transform.position.y = Math.random() * GameWindow.width; //random point along the border.
120 }
121 else
122 {
123 this.transform.position.y = (Math.random() > 0.5? 0 : GameWindow.height); //Same as above, but on another axis.
124 this.transform.position.x = Math.random() * GameWindow.height;
125 }
126 },
127
128 onDestroy: function()
129 {
130 GameObject.create(star, -100, -100); //Stars should never be destroyed. If it is anyway, make a new one.
131 }
132}
133
134var player = //Player ship that flies around and shoots and stuff.
135{
136 start: function()
137 {
138 this.cooldown = 30; //gun cooldown variable!
139 this.name = "player";
140 this.renderer.image = s_ship;
141 this.rotSpeed = 2; //how fast can we turn?
142 this.accelleration = 0.2; //Engine power!
143
144 this.gunOffset = 13; //This will help shoot the bullets from the wings instead of the center.
145
146 this.velocity = new vector(0, 0); //We start out stationary.
147 },
148
149 update: function()
150 {
151 if(GameInput.isHeld(37)) //Left key pressed? Turn left!
152 this.transform.rotation -= this.rotSpeed;
153
154 if(GameInput.isHeld(39)) //right key pressed? Turn right!
155 this.transform.rotation += this.rotSpeed;
156
157 if(GameInput.isHeld(38)) //Forward key held? Geronimo!
158 this.velocity = this.velocity.add(this.transform.forward().stretch(this.accelleration));
159
160 if(GameInput.isHeld(40)) //Backwards key held? Retreat!
161 this.velocity = this.velocity.add(this.transform.back().stretch(this.accelleration));
162
163 this.cooldown--; //let gun cooldown timer run
164
165 if(GameInput.isHeld(32)) //fire key held?
166 {
167 if(this.cooldown < 0) //cooldown timer expired?
168 {
169 GameAudio.play("shoot"); //Pew pew pew
170
171 this.gunOffset *= -1; //reverse gun offset to alternate the bullet origin
172 var creationPos = this.transform.position.add(this.transform.forward().stretch(1).add(this.transform.right().stretch(this.gunOffset))); //determine position at which the bullet will be created.
173 var newBullet = GameObject.create(bullet, creationPos.x, creationPos.y); //make a new bullet
174 newBullet.transform.rotation = this.transform.rotation; //rotate the bullet
175
176 newBullet.transform.rotation += -5 + (Math.random() * 10); //wiggle the bullet for a bit so we're not insanely accurate. Looks cooler.
177
178 this.cooldown = 5; //set the cooldown to some number to delay the next shot.
179
180 this.velocity = this.velocity.add(this.transform.back().stretch(this.accelleration * 0.1)); //Knockback in space!
181 }
182 }
183 }
184}
185
186var backgroundTemplate = //Big block which sticks to screen center!
187{
188 start: function()
189 {
190 this.ignoreCollisions = true;
191 this.name = "background";
192 this.color = "black";
193 this.transform.width = 1000;
194 this.transform.height = 1000;
195 },
196
197 update: function()
198 {
199 this.transform.position.x = 250;
200 this.transform.position.y = 250;
201 }
202}