Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jalbam committed Jan 23, 2024
1 parent 472d2c4 commit 52a5024
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ Game.onLoopStart = function(graphicSpritesSceneObject, CB_REM_dataObject, expect
//If the time to wait for the next wave has been reached, starts a new wave:
if (Game.data.levelEnemyWaveEndedTs !== null && CB_Device.getTiming() >= Game.data.levelEnemyWaveEndedTs + Game.Levels.data[Game.data.level].enemyWaves[Game.data.levelEnemyWave].timeFromLastEnemyToNextWave)
{
logMessage("The time for next wave has been reached. Starting new wave #" + (Game.data.levelEnemyWave) + "...");
Game.data.levelEnemyWaveEndedTs = null;
Game.data.enemies = [];

Expand All @@ -95,6 +94,8 @@ Game.onLoopStart = function(graphicSpritesSceneObject, CB_REM_dataObject, expect
Game.data.levelEnemyWave++;

createNewEnemy = true;

logMessage("The time for next wave has been reached. Starting new wave #" + (Game.data.levelEnemyWave) + "...");
}
//...otherwise, the current wave has ended so we store the time (if not done yet):
else if (Game.data.levelEnemyWaveEndedTs === null)
Expand Down Expand Up @@ -132,7 +133,8 @@ Game.onLoopStart = function(graphicSpritesSceneObject, CB_REM_dataObject, expect
else if (levelPassed)
{
Game.data.levelSucceeded = true;
Game.end("Congratulations! You survived successfully");
Game.data.score += Game.data.vitality >= 100 ? 1000 : 500;
Game.end("Congratulations! You survived successfully" + (Game.data.vitality >= 100 ? " (undamaged!!!)" : ""));
}

//Perform steps for each enemy:
Expand Down Expand Up @@ -217,7 +219,9 @@ Game.end = function(message)

message = CB_trim(message);
Game.data.gameStarted = false;
CB_Elements.insertContentById("start_button", (message !== "" ? message + "<br />" : "") + (Game.data.levelSucceeded ? "Continue playing" : "Start game!"))
var buttonText = "Start game!";
if (Game.data.levelSucceeded) { buttonText = (Game.data.level >= Game.Levels.data.length - 1) ? "All levels passed!<br />Restart game" : "Continue playing"; }
CB_Elements.insertContentById("start_button", (message !== "" ? message + "<br />" : "") + buttonText)
CB_Elements.showById("start_button"); //Shows the start button again.
}

Expand Down Expand Up @@ -391,8 +395,8 @@ Game.Levels._loadData = function(level, avoidCache)
(
function (subSprite)
{
subSprite.width = Visual._ELEMENTS_WIDTH;
subSprite.height = Visual._ELEMENTS_HEIGHT;
subSprite.width = subSprite.id.indexOf("vitality") !== -1 && subSprite.data._enemyObject ? subSprite.data._enemyObject.vitality / subSprite.data._enemyObject.levelVitality[subSprite.data._enemyObject.level] * Visual._ELEMENTS_WIDTH : Visual._ELEMENTS_WIDTH;
subSprite.height = subSprite.id.indexOf("vitality") !== -1 ? Visual._ELEMENTS_HEIGHT / 15 : Visual._ELEMENTS_HEIGHT;
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ Visual.getSpritesGroupsData = function()
};

//Sprites groups data:
Visual._spritesGroupsDataBeforeDrawingSprite =
function(element, canvasContext, canvasBufferContext, useBuffer, CB_GraphicSpritesSceneObject, drawingMap, x, y, mapElement) //Called before drawing the element.
{
if (!element.data._spriteDuration) { element.data._spriteDuration = 500 / (element.data._enemyObject.level + 1); }
if (element.data._beforeDrawingLastTS === null || CB_Device.getTiming() - element.data._beforeDrawingLastTS >= element.data._spriteDuration)
{
element.srcLeft += element.data._spriteWidth;
element.srcLeft %= element.data._spriteWidthTotal;
element.data._beforeDrawingLastTS = CB_Device.getTiming();
}

canvasContext.filter = "hue-rotate(" + (element.data._enemyObject.level * 60) + "deg)";

return element; //Same as 'element'. Must return the element to draw. Return null to skip drawing it.
};
Visual._spritesGroupsDataBeforeDrawingSpriteVitality =
function(element)
{
element.width = element.data._enemyObject.vitality / element.data._enemyObject.levelVitality[element.data._enemyObject.level] * Visual._ELEMENTS_WIDTH;
return element;
};

Visual._spritesGroupsData =
{
//'towers_defense_game_sprites_groups' ('CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT' object). Some missing or non-valid properties will get a default value:
Expand All @@ -40,13 +62,14 @@ Visual.getSpritesGroupsData = function()
id: "info",
srcType: CB_GraphicSprites.SRC_TYPES.TEXT,
top: 15,
zIndex: 3,
zIndex: 4,
data:
{
fontSize: "16px",
fontFamily: "courier",
style: "#ffaa00",
fontWeight: "bold"
fontWeight: "bold",
filter: "none"
},
sprites: [ { id: "info_sprite" } ]
},
Expand Down Expand Up @@ -95,7 +118,7 @@ Visual.getSpritesGroupsData = function()
{
id: "path_0",
src: "img/path_0.gif"
}
}
]
},

Expand Down Expand Up @@ -172,7 +195,31 @@ Visual.getSpritesGroupsData = function()
{
id: "enemy_0_subsprites",
src: "img/enemy_0_sprites.gif",
disabled: true
disabled: true,
data:
{
rotationUseDegrees: true,
rotation: 0,
_beforeDrawingLastTS: null,
_spriteWidth: 38,
_spriteWidthTotal: 152,
_spriteDuration: null, //It will change according to enemy level.
_enemyObject: null,
beforeDrawing: Visual._spritesGroupsDataBeforeDrawingSprite
}
},
{
id: "enemy_0_subsprites_vitality",
srcType: CB_GraphicSprites.SRC_TYPES.RECTANGLE,
disabled: true,
zIndex: 3,
data:
{
style: "#00aa00",
opacity: 0.6,
_enemyObject: null,
beforeDrawing: Visual._spritesGroupsDataBeforeDrawingSpriteVitality
}
}
]
}
Expand All @@ -194,7 +241,30 @@ Visual.getSpritesGroupsData = function()
{
id: "enemy_1_subsprites",
src: "img/enemy_1_sprites.gif",
disabled: true
disabled: true,
data:
{
rotationUseDegrees: true,
rotation: 0,
_beforeDrawingLastTS: null,
_spriteWidth: 38,
_spriteWidthTotal: 152,
_spriteDuration: null, //It will change according to enemy level.
beforeDrawing: Visual._spritesGroupsDataBeforeDrawingSprite
}
},
{
id: "enemy_0_subsprites_vitality",
srcType: CB_GraphicSprites.SRC_TYPES.RECTANGLE,
disabled: true,
zIndex: 3,
data:
{
style: "#00aa00",
opacity: 0.6,
_enemyObject: null,
beforeDrawing: Visual._spritesGroupsDataBeforeDrawingSpriteVitality
}
}
]
}
Expand Down Expand Up @@ -374,8 +444,8 @@ Visual.updateInfo = function(graphicSpritesSceneObject)
graphicSpritesSceneObject.getById("info").get(0).src =
"Level: " + (Game.data.level + 1) + "\n" +
"Coins: " + Game.data.coins + "\n" +
"Vitality: " + Game.data.vitality + "\n" +
"Score: " + Game.data.score + "\n" +
"Vitality: " + Game.data.vitality + "\n" +
"Wave: " + (Game.data.levelEnemyWave + 1) + "/" + Game.Levels.data[Game.data.level].enemyWaves.length + "\n" +
"* Enemies of this wave: " + Game.data.enemies.length + "/" + Game.Levels.data[Game.data.level].enemyWaves[Game.data.levelEnemyWave].enemies.length + " (" + Game.getEnemiesAlive().length + " alive)\n" +
(!CB_Screen.isLandscape() ? "\n\nLandscape screen recommended!" : "") +
Expand Down Expand Up @@ -493,17 +563,17 @@ Visual.resizeElements = function(graphicSpritesSceneObject, avoidFillingMap)
//NOTE: some browsers will fail to enable full screen mode if it is not requested through a user-driven event (as "onClick", "onTouchStart", etc.).
Visual.fullScreenToggle = function()
{
showMessage("Toggling full screen mode...");
logMessage("Toggling full screen mode...");
//If it is using full screen mode already, disables it:
if (CB_Screen.isFullScreen())
{
showMessage("Full screen mode detected. Trying to restore normal mode...");
logMessage("Full screen mode detected. Trying to restore normal mode...");
CB_Screen.setFullScreen(false); //Uses the Fullscreen API and fallbacks to other methods internally, including NW.js, Electron ones, when not available.
}
//...otherwise, requests full screen mode:
else
{
showMessage("Normal mode detected. Trying to enable full screen mode...");
logMessage("Normal mode detected. Trying to enable full screen mode...");
CB_Screen.setFullScreen(true, undefined, true); //Allows reloading into another (bigger) window (for legacy clients).
}
}
Loading

0 comments on commit 52a5024

Please sign in to comment.