Merged the StoreScene.hx with the rest of the game, all purchases will be made.

Also introduced savegames.
This commit is contained in:
Bram van der Veen 2014-07-15 22:17:09 +02:00
parent f0375c798b
commit e2f94102d4
13 changed files with 108 additions and 70 deletions

View file

View file

@ -15,6 +15,7 @@
<haxelib name="openfl" version="1.4.0" />
<assets path="assets/graphics" rename="graphics" include="*.png|*.jpg" />
<assets path="assets/save" rename="save" include="*.save" />
<assets path="assets/audio" rename="audio" include="*.mp3" if="flash" />
<assets path="assets/audio" rename="audio" include="*.wav|*.ogg" unless="flash" />
<assets path="assets/font" rename="font" include="*.ttf" />

View file

@ -51,8 +51,6 @@ class Boss extends Entity {
var enemies:Array<Enemy> = [];
this.scene.getClass(Enemy, enemies);
trace(enemies.length + ", " + maxEnemies);
if (spawnTimer < 0 && enemies.length != maxEnemies && canSpawn) {
this.scene.add(new Enemy(this.width / 2, 20, color, Math.floor(Math.random() * 4) + 1));
spawnTimer = .75;

View file

@ -13,6 +13,9 @@ class Bullet extends Entity {
graphic = laser1;
setHitboxTo(laser1);
laser1.centerOrigin();
laser2.centerOrigin();
this.centerOrigin();
type = "bullet";

View file

@ -3,18 +3,21 @@ import com.haxepunk.graphics.Image;
import com.haxepunk.HXP;
import Player;
import Save;
class Lives extends Entity {
public function new () {
baseSprite = new Image("graphics/playerShip1_green.png");
baseSprite = new Image("graphics/playerShip" + Save.load().ship_type + "_green.png");
sprite = [
baseSprite,
new Image("graphics/playerShip1_damage1.png"),
new Image("graphics/playerShip1_damage2.png"),
new Image("graphics/playerShip1_damage3.png")
new Image("graphics/playerShip" + Save.load().ship_type + "_damage1.png"),
new Image("graphics/playerShip" + Save.load().ship_type + "_damage2.png"),
new Image("graphics/playerShip" + Save.load().ship_type + "_damage3.png")
];
maxDamage = Save.load().ship_type + Save.load().ship_color;
liveBar = Image.createRect(Math.floor(baseSprite.width * .75), 10, 0x00FF00);
liveBar.y += 65;
@ -42,13 +45,13 @@ class Lives extends Entity {
public override function update() {
graphic = baseSprite;
this.addGraphic(sprite[damage]);
this.addGraphic(sprite[Math.floor((damage / maxDamage) * 4)]);
liveBar.scaledWidth = (liveBar.width / 4) * (4 - damage);
liveBar.scaledWidth = (liveBar.width / maxDamage) * (maxDamage - damage);
this.addGraphic(liveBar);
if (damage > 3) {
if (damage == maxDamage) {
var player:Array<Player> = [];
this.scene.getClass(Player, player);
player[0].die();
@ -61,6 +64,8 @@ class Lives extends Entity {
private var sprite:Array<Image> = [];
private var baseSprite:Image;
private var liveBar:Image;
private var damage:Int;
private var maxDamage:Int;
}

View file

@ -15,7 +15,7 @@ class Main extends Engine
HXP.resize(360, 640);
#end
HXP.scene = new StoreScene();
HXP.scene = new MenuScene();
}
public static function main() { new Main(); }

View file

@ -39,15 +39,19 @@ class MenuButton extends Entity {
}
public override function update() {
Input.touchPoints(onTouch);
if (Input.mouseReleased) {
if ((Input.mouseX > this.left && Input.mouseX < this.right) && (Input.mouseY > this.top && Input.mouseY < this.bottom)) {
if (this.text.text != "Menu") {
if ((Input.mouseX > this.left && Input.mouseX < this.right) && (Input.mouseY > this.top && Input.mouseY < this.bottom) || Input.pressed("enter")) {
if (this.text.text == "Retry" || this.text.text == "Play!") {
HXP.scene = null;
Assets.cache.clear();
HXP.scene = new MainScene();
}
else if (this.text.text == "Store") {
HXP.scene = null;
Assets.cache.clear();
HXP.scene = new StoreScene();
}
else {
HXP.scene = null;
Assets.cache.clear();
@ -55,34 +59,6 @@ class MenuButton extends Entity {
}
}
}
if (Input.check("enter")) {
if (this.text.text != "Menu") {
HXP.scene = null;
Assets.cache.clear();
HXP.scene = new MainScene();
}
else {
HXP.scene = null;
Assets.cache.clear();
HXP.scene = new MenuScene();
}
}
}
private function onTouch(touch:Touch) {
if ((touch.x > this.left && touch.x < this.right) && (touch.y > this.top && touch.y < this.bottom)) {
if (this.text.text != "Menu") {
HXP.scene = null;
Assets.cache.clear();
HXP.scene = new MainScene();
}
else {
HXP.scene = null;
Assets.cache.clear();
HXP.scene = new MenuScene();
}
}
}
private var txt:String;

View file

@ -12,11 +12,23 @@ import Bullet;
import Lives;
import Score;
import Explosion;
import StoreScene;
import Save;
class Player extends Entity {
public function new() {
super(HXP.halfWidth - 16, HXP.height - 200);
baseSprite = new Image("graphics/playerShip1_green.png");
baseSprite = new Image("graphics/" + Save.load().ship);
if (Save.load().ship_type == 1)
moveSpeed = 10;
else if (Save.load().ship_type == 2)
moveSpeed = 7;
else
moveSpeed = 5;
trace(moveSpeed + ", " + Save.load().ship_type);
shield = new Image("graphics/shield1.png");
#if flash
@ -39,18 +51,22 @@ class Player extends Entity {
new Image("graphics/fire17.png"),
];
fireEffectLeft = fireEffectsLeft[currentAnim];
fireEffectLeft.x = -30;
fireEffectLeft.y = 24;
for (i in 0...fireEffectsLeft.length) {
fireEffectsLeft[i].originX = fireEffectsLeft[i].width / 2;
fireEffectsLeft[i].x = -30;
fireEffectsLeft[i].y = 24;
fireEffectsRight[i].originX = fireEffectsLeft[i].width / 2;
fireEffectsRight[i].x = 30;
fireEffectsRight[i].y = 24;
}
fireEffectLeft = fireEffectsLeft[currentAnim];
fireEffectRight = fireEffectsRight[currentAnim];
fireEffectRight.x = 19;
fireEffectRight.y = 24;
this.addGraphic(fireEffectLeft);
this.addGraphic(fireEffectRight);
setHitbox(99, 75);
setHitboxTo(baseSprite);
Input.define("left", [Key.LEFT, Key.A]);
Input.define("right", [Key.RIGHT, Key.D]);
@ -96,17 +112,32 @@ class Player extends Entity {
public function shoot() {
if (this.y > 0) {
var score:Array<Score> = [];
this.scene.getClass(Score, score);
score[0].rem(500);
this.scene.add(new Bullet(this.x, this.y - this.height / 2));
score[0].rem(250);
var ship:Int = Save.load().ship_type;
if (ship == 3){
this.scene.add(new Bullet(this.x, this.y - this.height / 2));
}
else if (ship == 1) {
this.scene.add(new Bullet(this.x + 40, this.y - 20));
this.scene.add(new Bullet(this.x - 40, this.y - 20));
}
else if (ship == 2) {
this.scene.add(new Bullet(this.x, this.y - this.height / 2));
this.scene.add(new Bullet(this.x - 40, this.y - 20));
this.scene.add(new Bullet(this.x + 40, this.y - 20));
}
laser.play();
}
}
public function die() {
this.visible = false;
var score:Array<Score> = [];
this.scene.getClass(Score, score);
this.scene.add(new Explosion(this.x, this.y, this));
@ -115,8 +146,10 @@ class Player extends Entity {
var txt:Text = new Text("You died!", HXP.halfWidth - 225, HXP.halfHeight - 250, 500, 50, {size: 100});
txt.font = Assets.getFont("font/kenpixel_mini_square.ttf").fontName;
Save.save("money", score[0].score);
this.scene.addGraphic(txt);
this.scene.add(new MenuButton(HXP.halfWidth, HXP.halfHeight - 50, "Retry?"));
this.scene.add(new MenuButton(HXP.halfWidth, HXP.halfHeight - 50, "Store"));
this.scene.add(new MenuButton(HXP.halfWidth, HXP.halfHeight + 50, "Menu"));
}
@ -135,12 +168,8 @@ class Player extends Entity {
currentAnim = 0;
fireEffectLeft = fireEffectsLeft[currentAnim];
fireEffectLeft.x = -30;
fireEffectLeft.y = 24;
fireEffectRight = fireEffectsRight[currentAnim];
fireEffectRight.x = 19;
fireEffectRight.y = 24;
graphic = baseSprite;
this.addGraphic(fireEffectLeft);
@ -185,13 +214,14 @@ class Player extends Entity {
private var fireEffectsLeft:Array<Image> = [];
private var fireEffectsRight:Array<Image> = [];
private var moveSpeed:Float;
public var health:Int;
private var fireEffectLeft:Image;
private var fireEffectRight:Image;
private var moveSpeed:Int = 7;
private var hitPause:Float = 0;
private var hitPause:Float = 0;
private var animWait:Float = .75;
private var currentAnim:Int = 0;

View file

@ -5,19 +5,22 @@ class Save extends Entity {
public function new() {
super();
Data.load("savegame.save");
Data.load("save/savegame.save");
}
public static function save(what:String, data:Dynamic) {
Data.write(what, data);
Data.save("savegame.save", true);
Data.save("save/savegame.save", true);
}
public static function load() {
var data = {
"current_ship" : Data.readString("ship" , "playerShip3_green"),
"current_laser" : Data.readString("laser", "laserGreen04.png"),
"has_heavy_laser" : Data.readBool("heavy_laser", false)
"ship" : Data.readString("ship" , "playerShip3_green.png"),
"ship_type" : Data.readInt("ship_type", 3),
"ship_color" : Data.readInt("ship_color", 1),
"laser" : Data.readString("laser", "laserGreen04.png"),
"has_heavy_laser" : Data.readBool("heavy_laser", false),
"money" : Data.readInt("money", 1000)
};
return data;

View file

@ -4,14 +4,16 @@ import com.haxepunk.graphics.Text;
import openfl.Assets;
import Save;
class Score extends Entity {
public function new() {
super(HXP.halfWidth, 30);
name = "score";
score = 1000;
scoreText = new Text("$1000", 0, 0, 0, 0, {size : 50, /*align : "center",*/ color : 0xFFF000});
score = Save.load().money;
scoreText = new Text("$" + score, 0, 0, 0, 0, {size : 50, align : "center", color : 0xFFF000});
scoreText.font = Assets.getFont("font/kenpixel_mini_square.ttf").fontName;
layer = -3;

View file

@ -62,8 +62,6 @@ class StoreArrows extends Entity {
}
}
Input.touchPoints(onTouch);
}
private function onTouch(touch:Touch) {

View file

@ -2,6 +2,8 @@ import com.haxepunk.Entity;
import com.haxepunk.graphics.Image;
import com.haxepunk.graphics.Text;
import com.haxepunk.utils.Input;
import com.haxepunk.utils.Touch;
import openfl.Assets;
class StoreItem extends Entity {
@ -64,6 +66,23 @@ class StoreItem extends Entity {
this.addGraphic(button);
this.addGraphic(text);
this.addGraphic(description);
if (Input.mouseReleased) {
if ((Input.mouseX > this.left && Input.mouseX < this.right) && (Input.mouseY > this.top && Input.mouseY < this.bottom)) {
buy();
}
}
}
private function buy() {
var save = Save.load();
if (save.money >= prices[which][currentSprite]) {
Save.save("ship", items[which][currentSprite]);
Save.save("ship_type", which);
Save.save("ship_color", currentSprite);
Save.save("money", save.money - prices[which][currentSprite]);
trace("Bought something");
}
}
public function goLeft() {

View file

@ -14,7 +14,7 @@ class StoreScene extends Scene {
public override function begin() {
backdrop = new Backdrop("graphics/darkPurple.png", true, true);
addGraphic(backdrop);
var buyText:Text = new Text("Buy :", HXP.halfWidth - 100, 50, 0, 0, {size: 40});
buyText = new Text("$" + Save.load().money + ", Buy :", HXP.halfWidth - 100, 50, 0, 0, {size: 40});
buyText.font = Assets.getFont("font/kenpixel_mini_square.ttf").fontName;
addGraphic(buyText);
@ -23,13 +23,16 @@ class StoreScene extends Scene {
add(new StoreArrows(HXP.halfWidth, 200 + (i * 200), i));
}
trace(Save.load());
this.add(new MenuButton(HXP.halfWidth - 220, HXP.height - 50, "Retry"));
this.add(new MenuButton(HXP.halfWidth + 220, HXP.height - 50, "Menu"));
}
public override function update() {
super.update();
buyText.text = "$" + Save.load().money + ", Buy :";
backdrop.y += 1;
}
private var backdrop:Backdrop;
private var buyText:Text;
}