Repository navigation
Expand file tree
/
Copy pathJavaScript
More file actions
205 lines (183 loc) · 6.17 KB
/
Copy pathJavaScript
File metadata and controls
205 lines (183 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const blockSize = 40;
const rows = canvas.height / blockSize;
const cols = canvas.width / blockSize;
// Game state
let grid = Array.from({ length: rows }, () => Array(cols).fill(null));
let currentBlockType = 'grass';
let player = {
x: 5,
y: 5,
health: 100,
hunger: 100,
experience: 0,
inventory: {},
maxInventory: 20,
tools: {
pickaxe: { durability: 100, type: 'mining' },
axe: { durability: 100, type: 'woodcutting' }
}
};
let inventory = { grass: 0, dirt: 0, stone: 0, wood: 0, water: 0, sand: 0, food: 0, craftingTable: 0, chest: 0 };
let mobs = [];
// Block images
const blockImages = {
grass: 'https://i.imgur.com/2QlhwGo.png',
dirt: 'https://i.imgur.com/VAnlYmM.png',
stone: 'https://i.imgur.com/6R9L9ML.png',
wood: 'https://i.imgur.com/0D6zPiE.png',
water: 'https://i.imgur.com/NiMHnHp.png',
sand: 'https://i.imgur.com/PZhl9g1.png',
food: 'https://i.imgur.com/f1b7Wjo.png',
craftingTable: 'https://i.imgur.com/1w4IfvG.png',
chest: 'https://i.imgur.com/10Ko0AQ.png' // Example chest image
};
// Load block images
const images = {};
for (const [key, value] of Object.entries(blockImages)) {
const img = new Image();
img.src = value;
images[key] = img;
}
// Create mobs
function spawnMob() {
const x = Math.floor(Math.random() * cols);
const y = Math.floor(Math.random() * rows);
mobs.push({ x, y, type: 'cow' });
}
// Draw grid, player, and mobs
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const block = grid[row][col];
if (block) {
ctx.drawImage(images[block], col * blockSize, row * blockSize, blockSize, blockSize);
} else {
ctx.fillStyle = '#A9A9A9'; // Default color for empty blocks
ctx.fillRect(col * blockSize, row * blockSize, blockSize, blockSize);
}
ctx.strokeRect(col * blockSize, row * blockSize, blockSize, blockSize);
}
}
drawPlayer();
drawMobs();
}
// Draw the player
function drawPlayer() {
ctx.fillStyle = 'blue'; // Player color
ctx.fillRect(player.x * blockSize, player.y * blockSize, blockSize, blockSize);
}
// Draw mobs
function drawMobs() {
mobs.forEach(mob => {
ctx.fillStyle = 'green'; // Placeholder color for mobs
ctx.fillRect(mob.x * blockSize, mob.y * blockSize, blockSize, blockSize);
});
}
// Handle click events for placing/removing blocks
canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const col = Math.floor(x / blockSize);
const row = Math.floor(y / blockSize);
if (event.shiftKey) {
// Remove block
const removedBlock = grid[row][col];
if (removedBlock) {
grid[row][col] = null;
// Increment inventory if a block is removed
if (removedBlock in inventory) {
inventory[removedBlock]++;
player.experience += 1; // Gain experience from mining
}
}
} else {
// Place block if possible
if (inventory[currentBlockType] > 0 && countInventory() < player.maxInventory) {
grid[row][col] = currentBlockType;
inventory[currentBlockType]--;
}
}
drawGrid();
updateInventoryUI();
});
// Update inventory UI
function updateInventoryUI() {
const inventoryItemsDiv = document.getElementById('inventoryItems');
inventoryItemsDiv.innerHTML = '';
for (const [key, value] of Object.entries(inventory)) {
if (value > 0) {
const itemDiv = document.createElement('div');
itemDiv.className = 'inventory-item';
itemDiv.style.backgroundImage = `url(${blockImages[key]})`;
itemDiv.title = `${key}: ${value}`;
inventoryItemsDiv.appendChild(itemDiv);
}
}
}
// Count the items in inventory
function countInventory() {
return Object.values(inventory).reduce((sum, value) => sum + value, 0);
}
// Crafting system
const craftingRecipes = {
"woodenPlank": { wood: 2 },
"pickaxe": { woodenPlank: 3, stick: 2 },
"axe": { woodenPlank: 3, stick: 2 },
};
function craftItem(item) {
const recipe = craftingRecipes[item];
if (recipe) {
let canCraft = true;
for (const [key, value] of Object.entries(recipe)) {
if (inventory[key] < value) {
canCraft = false;
break;
}
}
if (canCraft) {
for (const [key, value] of Object.entries(recipe)) {
inventory[key] -= value;
}
inventory[item] = (inventory[item] || 0) + 1; // Add crafted item to inventory
updateInventoryUI();
} else {
alert("Not enough materials!");
}
}
}
// Time and Day/Night Cycle
let timeOfDay = 0; // 0 to 24, where 0 is night and 12 is day
let dayLength = 10000; // 10 seconds for a full cycle
function updateTime() {
timeOfDay += 0.1; // Increment time
if (timeOfDay >= 24) {
timeOfDay = 0; // Reset time
}
updateTimeDisplay();
drawGrid();
}
function updateTimeDisplay() {
const timeDisplay = document.getElementById('timeDisplay');
timeDisplay.innerText = timeOfDay < 12 ? "Night" : "Day";
}
setInterval(updateTime, dayLength / 240); // Adjust time increment based on day length
// Update health and hunger
function updateHealthAndHunger() {
player.hunger -= 0.01; // Decrease hunger over time
if (player.hunger <= 0) {
player.health -= 0.1; // Decrease health if hunger is 0
}
document.getElementById('health').innerText = player.health.toFixed(1);
document.getElementById('hunger').innerText = player.hunger.toFixed(1);
}
setInterval(updateHealthAndHunger, 100); // Update health and hunger every second
// Spawn mobs every few seconds
setInterval(spawnMob, 5000); // Spawn a new mob every 5 seconds
// Initial draw
drawGrid();
updateInventoryUI();
updateTimeDisplay();