浏览代码

Working on new space game example

Josh Engebretson 10 年之前
父节点
当前提交
8f0910d4bf

+ 0 - 7
NewSpaceGame/Resources/Components/Bullet.js

@@ -79,13 +79,6 @@ function updatePlayerBullet() {
             Math.abs(epos[1] - bpos[1]) < 0.25) {
 
             enemy.onHit();
-
-            enemy = null;
-
-            Atomic.getVM().gC();
-
-            game.dumpMetrics();
-
             return true;
         }
 

+ 17 - 4
NewSpaceGame/Resources/Components/CapitalShip.js

@@ -1,6 +1,6 @@
 var game = Atomic.game;
 var node = self.node;
-var scene = SpaceGame.scene;
+var scene = SpaceGame.myscene;
 
 self.allowShoot = true;
 self.shootDelta = 0;
@@ -52,14 +52,13 @@ function start() {
     var ai = node.createJSComponent("AI");
     ai.canMove = true;
 
-
-
     var spaceSheet = game.getSpriteSheet("Sprites/spacegame_sheet.xml");
 
     // add a sprite component to our node
-    var sprite2D = node.createComponent("StaticSprite2D");
+    var sprite2D = self.sprite2D = node.createComponent("StaticSprite2D");
     sprite2D.blendMode = Atomic.BLEND_ALPHA;
     sprite2D.sprite = spaceSheet.getSprite("spaceship_locust");
+    sprite2D.alpha = 0.0;
 
     node.position2D = [-4, SpaceGame.halfHeight - 1];
     node.roll(180);
@@ -69,5 +68,19 @@ function start() {
 // update function called per frame with delta time
 function update(timeStep) {
 
+  // fade in
+  var alpha = self.sprite2D.alpha;
+
+  if (alpha < 1)
+  {
+    alpha += timeStep * 1.5;
+    if (alpha > 1)
+      alpha = 1;
+
+    self.sprite2D.alpha = alpha;
+
+  }
+
+
 
 }

+ 16 - 1
NewSpaceGame/Resources/Components/Enemy.js

@@ -25,7 +25,7 @@ function start() {
     var spaceSheet = game.getSpriteSheet("Sprites/spacegame_sheet.xml");
 
     // add a sprite component to our node
-    var sprite2D = node.createComponent("StaticSprite2D");
+    var sprite2D = self.sprite2D = node.createComponent("StaticSprite2D");
     sprite2D.blendMode = Atomic.BLEND_ALPHA;
     sprite2D.sprite = spaceSheet.getSprite(self.spriteName);
 
@@ -33,6 +33,8 @@ function start() {
     node.roll(180);
     node.scale2D = [0.65, 0.65];
 
+    sprite2D.alpha = 0.0;
+
     self.dir = (Math.random() > .5);
 
 
@@ -41,6 +43,19 @@ function start() {
 // update function called per frame with delta time
 function update(timeStep) {
 
+    // fade in
+    var alpha = self.sprite2D.alpha;
+
+    if (alpha < 1)
+    {
+      alpha += timeStep * 1.5;
+      if (alpha > 1)
+        alpha = 1;
+
+      self.sprite2D.alpha = alpha;
+
+    }
+
     var pos = node.position2D;
     var ppos = SpaceGame.playerNode.position2D;
 

+ 1 - 1
NewSpaceGame/Resources/Components/HUD.js

@@ -8,7 +8,7 @@ var UILayout = Atomic.UILayout;
 
 var layout = new Atomic.UIWidget();
 layout.load("UI/Hud.ui.txt");
-layout.setSize(1280, 720);
+layout.setSize(game.graphics.width, game.graphics.height);
 view.addChild(layout);
 
 SpaceGame.viewport.rect = layout.getWidget("viewport").rect;

+ 8 - 17
NewSpaceGame/Resources/Components/SpaceGame.js

@@ -2,7 +2,8 @@ var UI = require("UI/ui");
 
 var game = Atomic.game;
 
-// expose ourselves as a global
+// expose ourselves as a global, this is invalid in "use strict"; which perhaps we should be using
+// to enforce better form
 SpaceGame = self;
 
 createScene();
@@ -109,19 +110,9 @@ function updateEnemies(timeStep) {
 self.cleanup = function() {
 
   game.renderer.setViewport(1, null);
+  Atomic.destroy(self.myscene);
 
-  Atomic.destroy(self.playerNode);
-  self.player = self.playerNode = null;
-
-  Atomic.destroy(self.hud.node);
-  self.hud = null;
-
-  for (var i = 0; i < self.enemies.length; i++) {
-    Atomic.destroy(self.enemies[i].node);
-  }
-
-  self.enemies = [];
-
+  // our node is in the main scene
   Atomic.destroy(self.node);
 
   SpaceGame = null;
@@ -133,8 +124,8 @@ self.win = function() {
 
     self.hud.updateGameText("YOU WIN!!!!");
     self.gameOver = true;
-    UI.showMainMenu();
-    self.cleanup();
+    UI.showGameOver();
+    //self.cleanup();
 
 }
 
@@ -142,8 +133,8 @@ self.lose = function() {
 
     self.hud.updateGameText("YOU LOSE!!!!");
     self.gameOver = true;
-    UI.showMainMenu();
-    self.cleanup();
+    UI.showGameOver();
+    //self.cleanup();
 
 }
 

+ 6 - 2
NewSpaceGame/Resources/Scripts/precache.js

@@ -2,10 +2,14 @@
 
 var resources =  {
   "Sprites/space_background.png" : "Texture2D",
-  "Music/battle.ogg" : "Sound"
+  "Music/battle.ogg" : "Sound",
+  "Sounds/boom0.wav" : "Sound",
+  "Sounds/boom1.wav" : "Sound",
+  "Sounds/laser01.wav" : "Sound",
+  "Sounds/laser02.wav" : "Sound",
+  "Sprites/explosions_sheet.xml" : "SpriteSheet2D"
 }
 
-
 // precache resources so they are ready to go
 exports.precache = function(verbose) {
 

+ 43 - 0
NewSpaceGame/Resources/UI/about.js

@@ -0,0 +1,43 @@
+'use strict';
+
+var game = Atomic.game;
+var view = game.uiView;
+var UI = Atomic.UI;
+var UIWindow = Atomic.UIWindow;
+
+var window;
+
+function closeWindow() {
+
+  if (window)
+    window.die();
+  window = null;
+
+}
+
+exports.init = function(onClose) {
+
+  window = new UIWindow();
+
+  window.settings = UI.WINDOW_SETTINGS_TITLEBAR;
+  window.text = "About Atomic Space Game";
+
+  window.load("UI/about.ui.txt");
+  window.resizeToFitContent();
+  view.addChild(window);
+  window.center();
+
+  window.getWidget("ok").onClick = function () {
+
+    closeWindow();
+    onClose();
+
+  }
+
+}
+
+exports.shutdown = function() {
+
+  closeWindow();
+
+}

+ 7 - 0
NewSpaceGame/Resources/UI/about.ui.txt

@@ -0,0 +1,7 @@
+TBLayout: axis: y
+	TBEditField: multiline: 1, styling: 1, gravity: all, id: about_text, readonly: 1, adapt-to-content: 0
+		font: size: 12
+		lp: max-width: 800, min-width: 800, min-height: 512, max-height: 512
+		text: "..."
+	TBButton: text: OK, id: ok
+		lp: min-width: 128, min-height: 48

+ 45 - 0
NewSpaceGame/Resources/UI/gameOver.js

@@ -0,0 +1,45 @@
+'use strict';
+
+var game = Atomic.game;
+var view = game.uiView;
+var UI = Atomic.UI;
+var UIWindow = Atomic.UIWindow;
+
+var window;
+
+function closeWindow() {
+
+  if (window)
+    window.die();
+  window = null;
+
+}
+
+exports.init = function() {
+
+  window = new UIWindow();
+
+  window.settings = Atomic.UI.WINDOW_SETTINGS_TITLEBAR;
+  window.text = "Game Over";
+
+  window.load("UI/gameOver.ui.txt");
+  window.resizeToFitContent();
+  view.addChild(window);
+  window.center();
+
+  window.getWidget("ok").onClick = function () {
+
+    SpaceGame.cleanup();
+    closeWindow();
+    var ui = require("./ui");
+    ui.showMainMenu();
+  }
+
+
+}
+
+exports.shutdown = function() {
+
+  closeWindow();
+
+}

+ 8 - 0
NewSpaceGame/Resources/UI/gameOver.ui.txt

@@ -0,0 +1,8 @@
+definitions
+	menubutton
+		font: size: 24
+
+TBLayout: axis: y, size: available
+
+	TBButton: text: "Ok!", id: "ok"
+		@include definitions>menubutton

+ 12 - 2
NewSpaceGame/Resources/UI/mainMenu.js

@@ -22,7 +22,7 @@ exports.init = function() {
   window.settings = Atomic.UI.WINDOW_SETTINGS_TITLEBAR;
   window.text = "Main Menu";
 
-  window.load("UI/MainMenu.ui.txt");
+  window.load("UI/mainMenu.ui.txt");
   window.resizeToFitContent();
   view.addChild(window);
   window.center();
@@ -38,7 +38,17 @@ exports.init = function() {
 
   window.getWidget("about").onClick = function () {
 
-    game.dumpMetrics();
+    // disable ourselves until ok is clicked on about
+    window.setState(UI.WIDGET_STATE_DISABLED, true);
+
+    var ui = require("./ui");
+    ui.showAbout(function() {window.setState(UI.WIDGET_STATE_DISABLED, false);});
+
+  }
+
+  window.getWidget("quit").onClick = function () {
+
+    game.engine.exit();
 
   }
 

+ 1 - 1
NewSpaceGame/Resources/UI/mainMenu.ui.txt

@@ -10,5 +10,5 @@ TBLayout: axis: y, size: available
 		@include definitions>menubutton
 	TBButton: text: "About", id: "about"
 		@include definitions>menubutton
-	TBButton: text: "Quit"
+	TBButton: text: "Quit", id: "quit"
 		@include definitions>menubutton

+ 15 - 3
NewSpaceGame/Resources/UI/ui.js

@@ -1,13 +1,25 @@
 'use strict';
 
-
-
+var mainMenu = require("./mainMenu");
+var gameOver = require("./gameOver");
+var about = require("./about");
 
 exports.showMainMenu = function() {
-    var mainMenu = require("./mainMenu");
+
     mainMenu.init();
 }
 
+exports.showGameOver = function() {
+
+    gameOver.init();
+}
+
+exports.showAbout = function(onClose) {
+
+    about.init(onClose);
+}
+
+
 exports.update = function(timeStep) {