Skip to content

Commit

Permalink
Added Image Loading and first character sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
vmckeown committed Feb 4, 2025
1 parent 933ea1c commit 23df29f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions vince_mckeown/Dungeon Crawler/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ window.onload = function() {
canvasContext.fill();

canvasContext.restore();

}

canvas.addEventListener('mousemove', function(evt) {
Expand Down
47 changes: 47 additions & 0 deletions vince_mckeown/topdownRPG/imageLoading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var spriteSheet1Pic = document.createElement("img");
var spriteCharacterPic = document.createElement("img");
var spriteEnemyPic = document.createElement("img");

//var titlepagePic = document.createElement("img");
var tilePics = [];


var picsToLoad = 0;

function countLoadedImagesAndLaunchIfReady(){
picsToLoad--;
console.log(picsToLoad);
if(picsToLoad == 0) {
imageLoadingDoneSoStartGame();
}
}

function beginLoadingImage(imgVar, fileName) {
imgVar.onload = countLoadedImagesAndLaunchIfReady;
imgVar.src = "images/" + fileName;
}

function loadImageForRoomCode(tileCode, fileName) {
tilePics[tileCode] = document.createElement("img");
beginLoadingImage(tilePics[tileCode], fileName);
}

function loadImages() {

var imageList = [
{varName: warriorPic, theFile: "spriteSheet1.png"},
{varName: spriteCharacterPic, theFile: "spritePlayer.png"},
{varName: spriteEnemyPic, theFile: "spriteEnemy.png"},
];

picsToLoad = imageList.length;

for(var i=0; i<imageList.length; i++) {
if(imageList[i].trackType != undefined){
loadImageForRoomCode(imageList[i].trackType, imageList[i].theFile);
}
else {
beginLoadingImage(imageList[i].varName, imageList[i].theFile);
}
}
}
10 changes: 8 additions & 2 deletions vince_mckeown/topdownRPG/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ player.levelUp();

const goblin = new Monster("Goblin", 300, 200, 30, 5, 20);
enemies.push(goblin);

console.log(`${goblin.name} is lurking in the woods...`);
goblin.attack(player);

console.log(`${player.name} now has ${player.health} HP.`);

const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');

// Game state
const gameState = {
Expand All @@ -27,6 +26,13 @@ const keys = {
right: false,
};

window.onload = function() {
canvas = document.getElementById('gameCanvas');
ctx = canvas.getContext('2d');

loadImages();
}

document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowUp') keys.up = true;
if (event.key === 'ArrowDown') keys.down = true;
Expand Down
1 change: 1 addition & 0 deletions vince_mckeown/topdownRPG/towndown.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script src="imageLoading.js"></script>
<script src="entity.js"></script>
<script src="player.js"></script>
<script src="enemy.js"></script>
Expand Down
Binary file added vince_mckeown/wizard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 23df29f

Please sign in to comment.