From 5dcb18fe66ec6f46834580152c6875f1007d19ce Mon Sep 17 00:00:00 2001 From: vesta Date: Sat, 30 Aug 2025 00:18:53 +0200 Subject: [PATCH] premiere version --- html/main.html | 110 ++++++ js/script.js | 1004 ++++++++++++++++++++++++------------------------ 2 files changed, 619 insertions(+), 495 deletions(-) create mode 100644 html/main.html diff --git a/html/main.html b/html/main.html new file mode 100644 index 0000000..47c27a4 --- /dev/null +++ b/html/main.html @@ -0,0 +1,110 @@ + + + + + + defiance v0.12.1 + + + + + + + + + + + +
+ +
Le monde prend vie...
+ +
+
Jour 1, 06:00
+
Printemps
+
+ + + + + + + +
+ + +
+
+
+
+
+
+ + + +
+ + + + \ No newline at end of file diff --git a/js/script.js b/js/script.js index eec7965..2c3843c 100644 --- a/js/script.js +++ b/js/script.js @@ -1,542 +1,556 @@ -const Biome = { - WATER_DEEP: { acceptStructure:false, movements:['swim'], affinities:[('water',0.8),('dark',0.2)], name: 'Eau Profonde', winterColor: '#3D5A80', fallColor: '#3D5A80', summerColor: '#3D5A80', autumnColor: '#3D5A80',maxElevation:-1,minElevation:-4 }, - WATER_SHALLOW: { acceptStructure:false,movements:['swim','ride','navigate','fly'], affinities:[('water',0.8),('life',0.2)],name: 'Eau Peu Profonde', winterColor: '#97aabdff', fallColor: '#5A8AB8', summerColor: '#5A8AB8', autumnColor: '#5A8AB8',maxElevation:1,minElevation:-2 }, - BEACH: { acceptStructure:true, movements:['walk','ride','fly'],affinities:[('sand',0.8),('water',0.2)],name: 'Sable', winterColor: '#E9D9A1', fallColor: '#E9D9A1', summerColor: '#E9D9A1', autumnColor: '#E9D9A1',maxElevation:2 ,minElevation:0 }, - GRASSLAND: { acceptStructure:true, movements:['walk','ride','fly'],affinities:[('life',0.6),('earth',0.2)],name: 'Plaine', winterColor: '#ecf1e3ff', fallColor: '#98C159', summerColor: '#a5a450ff', autumnColor: '#455e21ff',maxElevation:3,minElevation:-1 }, - FOREST: { acceptStructure:true,movements:['walk','ride','fly'],affinities:[('wood',0.6),('earth',0.4)], name: 'Forêt', winterColor: '#92ac83ff', fallColor: '#21a32cff', summerColor: '#6A994E', autumnColor: '#b88a28ff',maxElevation:3,minElevation:0 }, - ENCHANTED_FOREST: { acceptStructure:true,movements:['walk','ride','fly'],affinities:[('wood',0.8),('dark',0.2)('life',0.2)], name: 'Forêt Enchantée', winterColor: '#7B6094', fallColor: '#7B6094', summerColor: '#7B6094', autumnColor: '#7B6094',maxElevation:3,minElevation:0 }, - MOUNTAIN: { acceptStructure:false, movements:['climb','fly'],affinities:[('rock',0.6),('wind',0.4)],name: 'Montagne', winterColor: '#F7F7F7', fallColor: '#A9A9A9', summerColor: '#A9A9A9', autumnColor: '#A9A9A9',maxElevation:4,minElevation:3 }, - SNOWLAND: { acceptStructure:true,movements:['walk','ride','fly'], affinities:[('ice',0.8),('earth',0.2)],name: 'Toundra', winterColor: '#F7F7F7', fallColor: '#F7F7F7', summerColor: '#F7F7F7', autumnColor: '#F7F7F7',maxElevation:2,minElevation:-1 }, - SNOWMOUNTAIN: { acceptStructure:false, movements:['climb','fly'],affinities:[('ice',0.4),('rock',0.4),('wind',0.2)],name: 'Mont enneigé', winterColor: '#F7F7F7', fallColor: '#F7F7F7', summerColor: '#F7F7F7', autumnColor: '#F7F7F7',maxElevation:4,minElevation:3 }, - DESERT: { acceptStructure:true,movements:['walk','ride','fly'],affinities:[('sand',0.8),('life',0.1),('fire',0.1)], name: 'Désert', winterColor: '#D4A373', fallColor: '#D4A373', summerColor: '#D4A373', autumnColor: '#D4A373',maxElevation:2,minElevation:0 }, - RIVER: { acceptStructure:false,movements:['navigate','swim','fly'],affinities:[('water',0.6),('earth',0.2),('life',0.2)],name: 'Rivière', winterColor: '#97aabdff', fallColor: '#5A8AB8', summerColor: '#5A8AB8', autumnColor: '#5A8AB8',maxElevation:2,minElevation:-2 }, - SWAMP: { acceptStructure:false, movements:['fly'],affinities:[('water',0.6),('earth',0.2),('dark',0.2)],name: 'Marais', winterColor: '#0b2e10ff', fallColor: '#0b2e10ff', summerColor: '#0b2e10ff', autumnColor: '#0b2e10ff',maxElevation:1,minElevation:-1 } - -} -const NAME_PREFIXES = ["Chêne", "Sombre", "Pierre", "Haut", "Val", "Mur", "Guet", "Clair"]; -const NAME_SUFFIXES = ["bourg", "fort", "ville", "mont", "port", "bois", "rivage", "gard"]; - -const Affinities = ['water','fire','sand','rock','dark','life','ice','wood','wind','metal','time','space','lava','light','spirit']; - -const RACE = { - HUMAN: {strength:1,vitality:1,dexterity:1,intelligence:1,wisdom:1,luck:1} -}; - - - -const Slot = ['head','body','leg','foot','right-hand','left-hand','two-hands','finger1','finger2','neck','purse','backpack','belt1','belt2']; - -const ItemType = {lightWeapon:{slot:('right-hand','left-hand','backpack'),soul:true,enchant:true}, -heavyWeapon:{slot:('two-hands','backpack'),soul:true,enchant:true}, -heavyArmor:{slot:('body','backpack'),soul:true,enchant:true}, -lightArmor:{slot:('body','backpack'),soul:true,enchant:true}, -shield:{slot:('right-hand','left-hand','backpack'),soul:true,enchant:true}, -shoes:{slot:('foot','backpack'),soul:true,enchant:true}, -pant:{slot:('leg','backpack'),soul:true,enchant:true}, -helmet:{slot:('leg','backpack'),soul:true,enchant:true}, -potion:{slot:('heal','backpack'),soul:false,enchant:false}, -throwable:{slot:('belt','backpack'),soul:true,enchant:true}, -craftMaterial:{slot:('backpack'),soul:false,enchant:false}, -ring:{slot:('finger1','finger2','backpack'),soul:true,enchant:true}, -amulet:{slot:('neck','backpack'),soul:true,enchant:true}, -currency:{slot:('purse'),soul:true,enchant:true}, -enchantMaterial:{slot:('backpack'),soul:false,enchant:false}, -mercantMaterial:{slot:('backpack'),soul:false,enchant:false} -} - -const Items = { - 'Épée en Bois': { name: 'Épée en Bois', itemType: 'lightWeapon', stats: { strength: 2 }, icon: '🗡️' ,enchantments:[] }, - 'Armure de Cuir': { name: 'Armure de Cuir', itemType: 'lightArmor', stats: { vitality: 2 }, icon: '🧥',enchantments:[] }, - 'Claymore': { name: 'Claymore', itemType: 'lightArmor', stats: { vitality: 2 }, icon: '🧥',enchantments:['primal'] ,soul:new soul('cleaver','test')}, - 'Bois': { name: 'Bois', itemType: 'craftMaterial', icon: '🌲',biome:['FOREST'],occurence:0.9}, - 'stone': { name: 'Pierre', itemType: 'craftMaterial', icon: '🗿',biome:['MOUNTAIN'],occurence:0.4}, - 'Herbes': { name: 'Herbes', itemType: 'craftMaterial', icon: '🌿',biome:['GRASSLAND'],occurence:0.4}, - 'Cristaux Magiques': { name: ' enchantMaterial',icon: '💎',biome:['MOUNTAIN','ENCHANTED_FOREST'],occurence:0.005}, - 'Fer': { name: 'Fer', itemType: 'craftMaterial', icon: '🔩',biome:['MOUNTAIN'],occurence:0.001}, - 'Mushroom': { name: 'Champignons', itemType: 'craftMaterial', icon: '🍄',biome:['FOREST'],occurence:0.01}, - 'gold': { name: 'Champignons', itemType: 'mercantMaterial', icon: '💰',biome:['MOUNTAIN','RIVER'],occurence:0.001} -}; - - -const StructureType = { - TOWN: { name: 'Ville', population:15, icon: '🗡️', occurence:0.001,design: ` `}, - VILLAGE: { name: 'Village', population:5, icon: '🗡️',occurence:0.005, design: ` `}, - FARM: { name: 'Ferme', population:5, icon: '🗡️',occurence:0.010 }, - CAMP:{ name: 'Campement', population:2, icon: '⛺',occurence:0.002 }, -}; - -const MONSTER_TYPES = { - GOBLIN: { name: 'Gobelin', icon: '👺', hp: 30, strength: 3, xp: 25, loot: { 'Or': 5 } }, - ORC: { name: 'Orc', icon: '👹', hp: 60, strength: 6, xp: 50, loot: { 'Or': 15, 'Fer': 1 } }, -}; - -const ANIMAL_TYPES = { - WOLF: { name: 'Loup', icon: '🐺', hp: 20, strength: 4, xp: 15, loot: { 'Cuir': 1, 'Os': 1 }, design: ``}, - BOAR: { name: 'Sanglier', icon: '🐗', hp: 25, strength: 5, xp: 20, loot: { 'Cuir': 2 } }, -}; - - -class position { - - constructor(x,y,z) { - this.x = x; - this.y = y; - this.h = h; - } - setPosition(x, y, z) { - this.x = x; - this.y = y; - this.h = h; - } - - getPosition() { - return { x: this.x, y: this.y, z: this.z }; - } - - cartToIso() { - return { - x: (this.x - this.y) * 32, - y: (this.x + this.y) * 16 - } - } - // Méthode de l'objet -} - -class tile { - - constructor(position,elevation) { - this.position = position; - this.biome = this.setBiome(); - this.elevation = this.setElevation(); - this.structure = this.setStructure(); - this.explored = false; - this.design=null; - } - - // Méthode de l'objet - setBiome(biome) { - if (!Object.values(Biome).includes(biome)) { - throw new Error('Biome invalide !'); - } - this.biome = biome; - } - setBiome() { - let scale = 0.05; - let eRaw = (simplex.noise2D(x * scale, y * scale) + 1) / 2; - let tRaw = (simplex.noise2D(x * scale * 0.8, y * scale * 0.8) + 1) / 2; - let mRaw = (simplex.noise2D(x * scale * 1.5, y * scale * 1.5) + 1) / 2; +document.addEventListener('DOMContentLoaded', () => { + const Biome = { + WATER_DEEP: { acceptStructure:false, movements:['swim'], affinities:[('water',0.8),('dark',0.2)], name: 'Eau Profonde', winterColor: '#3D5A80', fallColor: '#3D5A80', summerColor: '#3D5A80', autumnColor: '#3D5A80',maxElevation:0,minElevation:0 }, + WATER_SHALLOW: { acceptStructure:false,movements:['swim','ride','navigate','fly'], affinities:[('water',0.8),('life',0.2)],name: 'Eau Peu Profonde', winterColor: '#97aabdff', fallColor: '#5A8AB8', summerColor: '#5A8AB8', autumnColor: '#5A8AB8',maxElevation:1,minElevation:1 }, + BEACH: { acceptStructure:true, movements:['walk','ride','fly'],affinities:[('sand',0.8),('water',0.2)],name: 'Sable', winterColor: '#E9D9A1', fallColor: '#E9D9A1', summerColor: '#E9D9A1', autumnColor: '#E9D9A1',maxElevation:2 ,minElevation:2 }, + GRASSLAND: { acceptStructure:true, movements:['walk','ride','fly'],affinities:[('life',0.6),('earth',0.2)],name: 'Plaine', winterColor: '#ecf1e3ff', fallColor: '#98C159', summerColor: '#a5a450ff', autumnColor: '#455e21ff',maxElevation:3,minElevation:2 }, + FOREST: { acceptStructure:true,movements:['walk','ride','fly'],affinities:[('wood',0.6),('earth',0.4)], name: 'Forêt', winterColor: '#92ac83ff', fallColor: '#21a32cff', summerColor: '#6A994E', autumnColor: '#b88a28ff',maxElevation:3,minElevation:2 }, + ENCHANTED_FOREST: { acceptStructure:true,movements:['walk','ride','fly'],affinities:[('wood',0.8),('dark',0.2),('life',0.2)], name: 'Forêt Enchantée', winterColor: '#7B6094', fallColor: '#7B6094', summerColor: '#7B6094', autumnColor: '#7B6094',maxElevation:3,minElevation:2 }, + MOUNTAIN: { acceptStructure:false, movements:['climb','fly'],affinities:[('rock',0.6),('wind',0.4)],name: 'Montagne', winterColor: '#F7F7F7', fallColor: '#A9A9A9', summerColor: '#A9A9A9', autumnColor: '#A9A9A9',maxElevation:4,minElevation:3 }, + SNOWLAND: { acceptStructure:true,movements:['walk','ride','fly'], affinities:[('ice',0.8),('earth',0.2)],name: 'Toundra', winterColor: '#F7F7F7', fallColor: '#F7F7F7', summerColor: '#F7F7F7', autumnColor: '#F7F7F7',maxElevation:2,minElevation:2 }, + SNOWMOUNTAIN: { acceptStructure:false, movements:['climb','fly'],affinities:[('ice',0.4),('rock',0.4),('wind',0.2)],name: 'Mont enneigé', winterColor: '#F7F7F7', fallColor: '#F7F7F7', summerColor: '#F7F7F7', autumnColor: '#F7F7F7',maxElevation:4,minElevation:3 }, + DESERT: { acceptStructure:true,movements:['walk','ride','fly'],affinities:[('sand',0.8),('life',0.1),('fire',0.1)], name: 'Désert', winterColor: '#D4A373', fallColor: '#D4A373', summerColor: '#D4A373', autumnColor: '#D4A373',maxElevation:2,minElevation:2 }, + RIVER: { acceptStructure:false,movements:['navigate','swim','fly'],affinities:[('water',0.6),('earth',0.2),('life',0.2)],name: 'Rivière', winterColor: '#97aabdff', fallColor: '#5A8AB8', summerColor: '#5A8AB8', autumnColor: '#5A8AB8',maxElevation:2,minElevation:2 }, + SWAMP: { acceptStructure:false, movements:['fly'],affinities:[('water',0.6),('earth',0.2),('dark',0.2)],name: 'Marais', winterColor: '#0b2e10ff', fallColor: '#0b2e10ff', summerColor: '#0b2e10ff', autumnColor: '#0b2e10ff',maxElevation:1,minElevation:1 } - if (eRaw < 0.25) this.biome = BIOMES.WATER_DEEP; - else if (eRaw < 0.3) this.biome = BIOMES.WATER_SHALLOW; - else if (eRaw < 0.35) this.biome = BIOMES.SAND; - else if (eRaw > 0.85) this.biome = BIOMES.SNOW; - else if (eRaw > 0.75) this.biome = BIOMES.MOUNTAIN; - else { - if (tRaw < 0.3) this.biome = BIOMES.DESERT; - else if (tRaw > 0.6) this.biome = (mRaw > 0.7) ? BIOMES.ENCHANTED_FOREST : BIOMES.FOREST; - else this.biome = BIOMES.GRASSLAND; - } } - setDesign(ctx){ - if (!this.biome) return; - const screenPos = this.position.cartToIso(); - const elevationHeight = this.position.h * 8; + const NAME_PREFIXES = ["Chêne", "Sombre", "Pierre", "Haut", "Val", "Mur", "Guet", "Clair"]; + const NAME_SUFFIXES = ["bourg", "fort", "ville", "mont", "port", "bois", "rivage", "gard"]; + + const Affinities = ['water','fire','sand','rock','dark','life','ice','wood','wind','metal','time','space','lava','light','spirit']; + + const RACE = { + HUMAN: {strength:1,vitality:1,dexterity:1,intelligence:1,wisdom:1,luck:1} + }; + + + + const Slot = ['head','body','leg','foot','right-hand','left-hand','two-hands','finger1','finger2','neck','purse','backpack','belt1','belt2']; + + const ItemType = { + lightWeapon:{slot:['right-hand','left-hand','backpack'],soul:true,enchant:true}, + heavyWeapon:{slot:('two-hands','backpack'),soul:true,enchant:true}, + heavyArmor:{slot:('body','backpack'),soul:true,enchant:true}, + lightArmor:{slot:('body','backpack'),soul:true,enchant:true}, + shield:{slot:('right-hand','left-hand','backpack'),soul:true,enchant:true}, + shoes:{slot:('foot','backpack'),soul:true,enchant:true}, + pant:{slot:('leg','backpack'),soul:true,enchant:true}, + helmet:{slot:('leg','backpack'),soul:true,enchant:true}, + potion:{slot:('heal','backpack'),soul:false,enchant:false}, + throwable:{slot:('belt','backpack'),soul:true,enchant:true}, + craftMaterial:{slot:('backpack'),soul:false,enchant:false}, + ring:{slot:('finger1','finger2','backpack'),soul:true,enchant:true}, + amulet:{slot:('neck','backpack'),soul:true,enchant:true}, + currency:{slot:('purse'),soul:true,enchant:true}, + enchantMaterial:{slot:('backpack'),soul:false,enchant:false}, + mercantMaterial:{slot:('backpack'),soul:false,enchant:false} + } + + const Items = { + 'Épée en Bois': { name: 'Épée en Bois', itemType: 'lightWeapon', stats: { strength: 2 }, icon: '🗡️' ,enchantments:[] }, + 'Armure de Cuir': { name: 'Armure de Cuir', itemType: 'lightArmor', stats: { vitality: 2 }, icon: '🧥',enchantments:[] }, + 'Claymore': { name: 'Claymore', itemType: 'lightArmor', stats: { vitality: 2 }, icon: '🧥',enchantments:['primal'] }, + 'Bois': { name: 'Bois', itemType: 'craftMaterial', icon: '🌲',biome:['FOREST'],occurence:0.9}, + 'stone': { name: 'Pierre', itemType: 'craftMaterial', icon: '🗿',biome:['MOUNTAIN'],occurence:0.4}, + 'Herbes': { name: 'Herbes', itemType: 'craftMaterial', icon: '🌿',biome:['GRASSLAND'],occurence:0.4}, + 'Cristaux Magiques': { name: ' enchantMaterial',icon: '💎',biome:['MOUNTAIN','ENCHANTED_FOREST'],occurence:0.005}, + 'Fer': { name: 'Fer', itemType: 'craftMaterial', icon: '🔩',biome:['MOUNTAIN'],occurence:0.001}, + 'Mushroom': { name: 'Champignons', itemType: 'craftMaterial', icon: '🍄',biome:['FOREST'],occurence:0.01}, + 'gold': { name: 'Champignons', itemType: 'mercantMaterial', icon: '💰',biome:['MOUNTAIN','RIVER'],occurence:0.001} + }; + + + const StructureType = { + TOWN: { name: 'Ville', population:15, icon: '🗡️', occurence:0.001,design: ` `}, + VILLAGE: { name: 'Village', population:5, icon: '🗡️',occurence:0.005, design: ` `}, + FARM: { name: 'Ferme', population:5, icon: '🗡️',occurence:0.010 }, + CAMP:{ name: 'Campement', population:2, icon: '⛺',occurence:0.002 }, + }; + + const MONSTER_TYPES = { + GOBLIN: { name: 'Gobelin', icon: '👺', hp: 30, strength: 3, xp: 25, loot: { 'Or': 5 } }, + ORC: { name: 'Orc', icon: '👹', hp: 60, strength: 6, xp: 50, loot: { 'Or': 15, 'Fer': 1 } }, + }; + + const ANIMAL_TYPES = { + WOLF: { name: 'Loup', icon: '🐺', hp: 20, strength: 4, xp: 15, loot: { 'Cuir': 1, 'Os': 1 }, design: ``}, + BOAR: { name: 'Sanglier', icon: '🐗', hp: 25, strength: 5, xp: 20, loot: { 'Cuir': 2 } }, + }; + + + + + class Position{ + constructor(x,y,h=0) { + this.x = x; + this.y = y; + this.h = h; + } + setPosition(x, y, h=0) { + this.x = x; + this.y = y; + this.h = h; + } - ctx.save(); - ctx.translate(screenPos.x, screenPos.y - elevationHeight); - const baseColor = this.biome.color; - const shadowColor = shadeColor(baseColor, -30); - ctx.fillStyle = shadowColor; - ctx.beginPath(); - ctx.moveTo(0, 32); ctx.lineTo(32, 16); ctx.lineTo(32, 16 + elevationHeight); ctx.lineTo(0, 32 + elevationHeight); - ctx.closePath(); - ctx.fill(); - ctx.fillStyle = shadeColor(shadowColor, -10); - ctx.beginPath(); - ctx.moveTo(0, 32); ctx.lineTo(-32, 16); ctx.lineTo(-32, 16 + elevationHeight); ctx.lineTo(0, 32 + elevationHeight); - ctx.closePath(); - ctx.fill(); - ctx.fillStyle = baseColor; - ctx.beginPath(); - ctx.moveTo(0, 0); ctx.lineTo(32, 16); ctx.lineTo(0, 32); ctx.lineTo(-32, 16); - ctx.closePath(); - ctx.fill(); - if (this.structure.type === 'village') { - ctx.fillStyle = '#D4A373'; - ctx.fillRect(-8, -12, 16, 12); - ctx.fillStyle = '#8B4513'; + getPosition() { + return { x: this.x, y: this.y, h: this.h }; + } + + cartToIso() { + return { + x: (this.x - this.y) * 32, + y: (this.x + this.y) * 16 + } + } + // Méthode de l'objet + } + + class Tile { + + constructor(position) { + this.position = position; + this.biome = null; + this.structure = null; + this.explored = false; + this.design=null; + this.setBiome(); + this.setElevation(); + this.setStructure(); + this.setDesign(); + } + + // Méthode de l'objet + setBiome() { + let scale = 0.05; + let simplex = new SimplexNoise(); + let eRaw = (simplex.noise2D(this.position.x * scale, this.position.y * scale) + 1) / 2; + let tRaw = (simplex.noise2D(this.position.x * scale * 0.8, this.position.y * scale * 0.8) + 1) / 2; + let mRaw = (simplex.noise2D(this.position.x * scale * 1.5, this.position.y * scale * 1.5) + 1) / 2; + if (eRaw < 0.25) this.biome = Biome.WATER_DEEP; + else if (eRaw < 0.3) this.biome = Biome.WATER_SHALLOW; + else if (eRaw < 0.35) this.biome = Biome.BEACH; + else if (eRaw > 0.85) this.biome = Biome.SNOWLAND; + else if (eRaw > 0.75) this.biome = Biome.MOUNTAIN; + else { + if (tRaw < 0.3) this.biome = Biome.DESERT; + else if (tRaw > 0.6) this.biome = (mRaw > 0.7) ? Biome.ENCHANTED_FOREST : Biome.FOREST; + else this.biome = Biome.GRASSLAND; + } + } + setDesign(){ + if (!this.biome) return; + const screenPos = this.position.cartToIso(); + const elevationHeight = this.position.h * 8; + + ctx.save(); + ctx.translate(screenPos.x, screenPos.y - elevationHeight); + const baseColor = this.biome.summerColor; + const shadowColor = shadeColor(baseColor, -30); + ctx.fillStyle = shadowColor; ctx.beginPath(); - ctx.moveTo(0, -22); ctx.lineTo(-10, -12); ctx.lineTo(10, -12); + ctx.moveTo(0, 32); ctx.lineTo(32, 16); ctx.lineTo(32, 16 + elevationHeight); ctx.lineTo(0, 32 + elevationHeight); ctx.closePath(); ctx.fill(); - } else if (this.structure.type === 'city') { - ctx.fillStyle = '#C0C0C0'; - ctx.fillRect(-10, -20, 20, 20); - ctx.fillStyle = '#808080'; + ctx.fillStyle = shadeColor(shadowColor, -10); ctx.beginPath(); - ctx.moveTo(-10, -20); ctx.lineTo(-12, -22); ctx.lineTo(22, -22); ctx.lineTo(20, -20); + ctx.moveTo(0, 32); ctx.lineTo(-32, 16); ctx.lineTo(-32, 16 + elevationHeight); ctx.lineTo(0, 32 + elevationHeight); ctx.closePath(); ctx.fill(); + ctx.fillStyle = baseColor; + ctx.beginPath(); + ctx.moveTo(0, 0); ctx.lineTo(32, 16); ctx.lineTo(0, 32); ctx.lineTo(-32, 16); + ctx.closePath(); + ctx.fill(); + + if (this.structure && this.structure.type === 'village') { + ctx.fillStyle = '#D4A373'; + ctx.fillRect(-8, -12, 16, 12); + ctx.fillStyle = '#8B4513'; + ctx.beginPath(); + ctx.moveTo(0, -22); ctx.lineTo(-10, -12); ctx.lineTo(10, -12); + ctx.closePath(); + ctx.fill(); + } else if (this.structure && this.structure.type === 'city') { + ctx.fillStyle = '#C0C0C0'; + ctx.fillRect(-10, -20, 20, 20); + ctx.fillStyle = '#808080'; + ctx.beginPath(); + ctx.moveTo(-10, -20); ctx.lineTo(-12, -22); ctx.lineTo(22, -22); ctx.lineTo(20, -20); + ctx.closePath(); + ctx.fill(); + } + ctx.restore(); } - ctx.restore(); - } - - setExplored(explored) { - this.explored=explored; - } - setElevation(){ - this.elevation = Math.floor(Math.random() * (this.biome.maxElevation - this.biome.minElevation+ 1) + this.biome.minElevation); - } - setStructure(){ - var structureChance = rand(); - if (structureChance < 0.005 && this.tile.biome.acceptStructure) { - this.structure = { type: 'city', name: NAME_PREFIXES[Math.floor(rand() * NAME_PREFIXES.length)] + NAME_SUFFIXES[Math.floor(rand() * NAME_SUFFIXES.length)], population: TOWN.population + Math.floor(rand() * 150), buildings: ['Remparts', 'Place Forte (Château)', 'Grand Marché', 'Forge', 'Alchimiste', 'Enchanteur', 'Écurie', 'Ferme', 'Ferme', 'Ferme', 'Ferme', 'Ferme', 'Lieu de Culte', 'Nombreuses Maisons'] }; - } else if (structureChance < 0.02 && this.tile.biome.acceptStructure) { - this.structure = { type: 'village', name: NAME_PREFIXES[Math.floor(rand() * NAME_PREFIXES.length)] + NAME_SUFFIXES[Math.floor(rand() * NAME_SUFFIXES.length)], population: VILLAGE.population + Math.floor(rand() * 20), buildings: ['Maison du Chef', 'Marchand', 'Lieu de Culte', 'Ferme', 'Ferme', 'Plusieurs Maisons'] }; + + setExplored(explored) { + this.explored=explored; } - if (!this.tile && this.tile.biome === BIOMES.GRASSLAND && Math.random() < 0.2) { - this.structure={type:'farm', name: 'farm', population: 2, buildings: []} - // npcs.push({ x: nx, y: ny, homeX: loc.x, homeY: loc.y, workX: nx, workY: ny, type: 'farmer', icon: '🧑‍🌾' }); - } - } -} -class map { - constructor(size) { - this.map_size = size; - this.tiles = initializeMap(); - } - - initializeMap(){ - console.time('Map Generation'); - const simplex = new SimplexNoise(); - this.tiles = Array(size).fill(null).map((_, y) => Array(size).fill(null).map((_, x) => new Tile(new Position(x, y)))); - } -} -class Time { - constructor() { - this.year = 812; - this.month = 1; - this.day = 13; - this.season = 'winter'; - this.hour = 12; - this.day = true; - } - - tick(){ - this.hour=(this.hour+1) % 24 - if (this.hour==1) this.day=(this.day+1) % 30 - if (this.day==1) {this.month=(this.month+1) % 12;} - if (this.month==1) {this.year=(this.year+1); } - if (this.month==6 && this.day==21) {this.season='summer'; } - if (this.month==9 && this.day==21) {this.season='autumn'; } - if (this.month==12 && this.day==21) {this.season='winter'; } - if (this.month==3 && this.day==21) {this.season='fall'; } - } - - wait(nbrHour){ - for (let i = 0; i < nbrHour; i++) { - this.tick(); + setElevation(){ + if (!this.biome) { + console.error("Impossible de définir l'élévation car le biome n'est pas défini pour la tuile", this.position); + return; + } + this.position.h = Math.floor(Math.random() * (this.biome.maxElevation - this.biome.minElevation+ 1) + this.biome.minElevation); + } + setStructure(){ + if (!this.biome) { + console.error("Impossible de définir une structure car le biome n'est pas défini pour la tuile", this.position); + return; + } + var rand = seededRandom(this.position.x * 13 + this.position.y * 59); + var structureChance = rand(); + if (structureChance < 0.005 && this.biome.acceptStructure) { + this.structure = { type: 'city', name: NAME_PREFIXES[Math.floor(rand() * NAME_PREFIXES.length)] + NAME_SUFFIXES[Math.floor(rand() * NAME_SUFFIXES.length)], population: StructureType.TOWN.population + Math.floor(rand() * 150), buildings: ['Remparts', 'Place Forte (Château)', 'Grand Marché', 'Forge', 'Alchimiste', 'Enchanteur', 'Écurie', 'Ferme', 'Ferme', 'Ferme', 'Ferme', 'Ferme', 'Lieu de Culte', 'Nombreuses Maisons'] }; + } else if (structureChance < 0.02 && this.biome.acceptStructure) { + this.structure = { type: 'village', name: NAME_PREFIXES[Math.floor(rand() * NAME_PREFIXES.length)] + NAME_SUFFIXES[Math.floor(rand() * NAME_SUFFIXES.length)], population: StructureType.VILLAGE.population + Math.floor(rand() * 20), buildings: ['Maison du Chef', 'Marchand', 'Lieu de Culte', 'Ferme', 'Ferme', 'Plusieurs Maisons'] }; + } + if ( this.biome.acceptStructure && Math.random() < 0.2) { + this.structure={type:'farm', name: 'farm', population: 2, buildings: []} + // npcs.push({ x: nx, y: ny, homeX: loc.x, homeY: loc.y, workX: nx, workY: ny, type: 'farmer', icon: '🧑‍🌾' }); + } } } - // Méthode de l'objet - -} -class attributes { - constructor(race) { - this.strength = race.strength; - this.vitality = race.vitality; - this.dexterity = race.dexterity; - this.intelligence = race.intelligence; - this.wisdom = race.wisdom; - this.luck = rand(); + class Map { + constructor(size) { + this.size = size; + this.tiles = null + this.initializeMap(); + } + + initializeMap(){ + console.time('Map Generation'); + this.tiles=Array(this.size).fill(null).map((_, y) => Array(this.size).fill(null).map((_, x) => new Tile(new Position(y, x)))); + } } - - // Méthode de l'objet - -} - -class creature { - constructor(name,attributes,level,affinities,alignments,tile,species,race,hp,items) { - this.name = name; - this.attributes = attributes; - this.level = level; - this.affinities = affinities; - this.name = name; - this.alignments = alignments; - this.species = species; - this.race = race; - this.tile=tile; - this.items=items; - this.hp=hp; - } - - fight(){ + class Time { + constructor() { + this.year = 812; + this.month = 1; + this.day = 13; + this.season = 'winter'; + this.hour = 12; + this.day = true; + } + + tick(){ + this.hour=(this.hour+1) % 24 + if (this.hour==1) this.day=(this.day+1) % 30 + if (this.day==1) {this.month=(this.month+1) % 12;} + if (this.month==1) {this.year=(this.year+1); } + if (this.month==6 && this.day==21) {this.season='summer'; } + if (this.month==9 && this.day==21) {this.season='autumn'; } + if (this.month==12 && this.day==21) {this.season='winter'; } + if (this.month==3 && this.day==21) {this.season='fall'; } + } + + wait(nbrHour){ + for (let i = 0; i < nbrHour; i++) { + this.tick(); + } + } + // Méthode de l'objet } - // Méthode de l'objet - -} -class npc { - constructor(name, creature, settlement, equipments) { - this.creature = creature; - this.settlement = settlement; - this.equipments = equipments; - } - - talk() { + class Attributes { + constructor(race) { + this.strength = race.strength; + this.vitality = race.vitality; + this.dexterity = race.dexterity; + this.intelligence = race.intelligence; + this.wisdom = race.wisdom; + this.luck = race.luck; + } + + // Méthode de l'objet } - steal() { + class Creature { + constructor(name,attributes,level,affinities,alignments,tile,species,race,hp,items) { + this.name = name; + this.attributes = attributes; + this.level = level; + this.affinities = affinities; + this.name = name; + this.alignments = alignments; + this.species = species; + this.race = race; + this.tile=tile; + this.items=items; + this.hp=hp; + } + + fight(){ + + } + // Méthode de l'objet + + } + class Npc { + constructor(name, creature, settlement, equipments) { + this.creature = creature; + this.settlement = settlement; + this.equipments = equipments; + } + + talk() { + + } + + steal() { + + } + + trade() { + + } + // Méthode de l'objet } - trade() { + class Player { + constructor(creature,equipments) { + this.creature=creature; + this.equipments=equipments; + this.color = '#E53E3E'; + this.position=creature.tile.position + } + draw(ctx) { + const tile = this.creature.tile + if(!tile) return; + + const screenPos = tile.position.cartToIso(); + const elevationHeight = tile.position.h * 8; + + ctx.save(); + ctx.translate(screenPos.x, screenPos.y - elevationHeight); + ctx.fillStyle = 'rgba(0, 0, 0, 0.4)'; + ctx.beginPath(); + ctx.ellipse(0, 16 + 2, 6, 3, 0, 0, Math.PI * 2); + ctx.fill(); + ctx.fillStyle = this.color; + ctx.beginPath(); + ctx.arc(0, 16 - 6, 8, 0, Math.PI * 2); + ctx.fill(); + ctx.strokeStyle = shadeColor(this.color, -30); + ctx.lineWidth = 2; + ctx.stroke(); + ctx.restore(); + } + move(dx, dy) { + const newX = this.position.x + dx; + const newY = this.position.y + dy; + if (newX >= 0 && newX < gameMap.size && newY >= 0 && newY < gameMap.size) { + const targetTile = gameMap.tiles[newY][newX]; + if (targetTile.biome && targetTile.biome.movements.includes('walk')) { + this.position.x = newX; + this.position.y = newY; + centerCameraOnPlayer(this.position); + } + } + } + rename(name) { + this.name = name; + } + + equipItem(item,slot) { + equipment.equip(item,slot); + updateAttribute(); + } } - // Méthode de l'objet -} - -class player { - constructor(creature,equipments) { - this.creature=creature; - this.equipments=equipments; - this.color = '#E53E3E'; - } - draw(ctx) { - const tile = this.creature.tile - if(!tile) return; + class Settlement { + constructor(level, affinities, name, alignments, position, structures) { + this.level = level; + this.affinities = affinities; + this.name = name; + this.alignments = alignments; + this.structures = structures; + } - const screenPos = tile.cartToIso(); - const elevationHeight = tile.position.h * 8; + // Méthode de l'objet - ctx.save(); - ctx.translate(screenPos.x, screenPos.y - elevationHeight); - ctx.fillStyle = 'rgba(0, 0, 0, 0.4)'; - ctx.beginPath(); - ctx.ellipse(0, 16 + 2, 6, 3, 0, 0, Math.PI * 2); - ctx.fill(); - ctx.fillStyle = this.color; - ctx.beginPath(); - ctx.arc(0, 16 - 6, 8, 0, Math.PI * 2); - ctx.fill(); - ctx.strokeStyle = shadeColor(this.color, -30); - ctx.lineWidth = 2; - ctx.stroke(); - ctx.restore(); } - move(dx, dy) { - const newX = this.position.x + dx; - const newY = this.position.y + dy; - if (newX >= 0 && newX < gameMap.size && newY >= 0 && newY < gameMap.size) { - const targetTile = gameMap.tiles[newY][newX]; - if (targetTile.biome && targetTile.biome.movements.includes('walk')) { - this.position.x = newX; - this.position.y = newY; - centerCameraOnPlayer(); + class Resource { + constructor(resourceType, position) { + this.resourceType = resourceType; + this.position = position; + } + collect(tile){ + + } + collect(creature){ + // check if creature can collect + // transfer resulting items creature.items[]=this.resourceType.item + //end object + } + // Méthode de l'objet + + } + class Equipment { + + constructor(item,slot) { + this.item = item; + this.slot = slot; + } + // Méthode de l'objet + equip(slot) { + //replace current equipment + //check if compatible + //check if equipable + this.slot = slot; + } + unequip() { + this.slot = null; + + } + } + class Soul { + + constructor(name,description) { + this.name = name; + this.description = description; + } + // Méthode de l'objet + invoke() { + // + + } + } + class Item { + + constructor(name,duration,itemType) { + this.name = name; + if (!Object.values(ItemType).includes(itemType)) { + throw new Error('Item type invalide !'); + } + this.itemType = itemType; + if (duration){ + this.duration = itemType.duration + } else { + this.duration = duration + } + + } + // Méthode de l'objet + throw() { + // delete after x Tick + } + destroy() { + + } + use(){ + if (this.duration >1) { + this.duration = this.duration-1 + } + else { + this.destroy(); } } } - rename(name) { - this.name = name; - } - equipItem(item,slot) { - equipment.equip(item,slot); - updateAttribute(); - } -} - -class settlement { - constructor(level, affinities, name, alignments, position, structures) { - this.level = level; - this.affinities = affinities; - this.name = name; - this.alignments = alignments; - this.structures = structures; - } - - // Méthode de l'objet - -} -class resource { - constructor(resourceType, position) { - this.resourceType = resourceType; - this.position = position; - } - collect(tile){ + class World { - } - collect(creature){ - // check if creature can collect - // transfer resulting items creature.items[]=this.resourceType.item - this = null;//end object - } - // Méthode de l'objet - -} -class equipment { - - constructor(item,slot) { - this.item = item; - this.slot = slot; - } - // Méthode de l'objet - equip(slot) { - //replace current equipment - //check if compatible - //check if equipable - this.slot = slot; - } - unequip() { - this.slot = null; - - } -} -class soul { - - constructor(name,description) { - this.name = name; - this.description = description; - } - // Méthode de l'objet - invoke() { - // - - } -} -class item { - - constructor(name,duration,itemType) { - this.name = name; - if (!Object.values(ItemType).includes(itemType)) { - throw new Error('Item type invalide !'); + constructor(name) { + this.name = name; + this.map = new Map(200) + let spawnX, spawnY; + do { + spawnX = Math.floor(Math.random() * this.map.size); + spawnY = Math.floor(Math.random() * this.map.size); + } while (!this.map.tiles[spawnY][spawnX].biome || !this.map.tiles[spawnY][spawnX].biome.movements.includes('walk') ); + this.player = new Player(new Creature('player',new Attributes(RACE.HUMAN),1,null,null,this.map.tiles[spawnY][spawnX],'human',RACE.HUMAN,10,null),null); + centerCameraOnPlayer(this.player.position); } - this.itemType = itemType; - if (duration){ - this.duration = itemType.duration - } else { - this.duration = duration + // Méthode de l'objet + save() { + // delete after x Tick } } - // Méthode de l'objet - throw() { - // delete after x Tick + + const controls = { up: false, down: false, left: false, right: false }; + const simplex = new SimplexNoise(); + let lastMoveTime = 0; + let isGameReady = false; + const canvas = document.getElementById('gameCanvas'); + const ctx = canvas.getContext('2d'); + const loadingScreen = document.getElementById('loading'); + const TILE_WIDTH = 64; + const TILE_HEIGHT = 32; + const ELEVATION_STEP = TILE_HEIGHT / 4; + + + function seededRandom(seed) { + let s = seed; + return () => { s = (s * 9301 + 49297) % 233280; return s / 233280.0; }; } - destroy() { - this = null; + + function centerCameraOnPlayer(position) { + const camera = { x: 0, y: 0 }; + const playerScreenPos = position.cartToIso(); + const elevationHeight = position.h * ELEVATION_STEP; + camera.x = canvas.width / 2 - playerScreenPos.x; + camera.y = canvas.height / 2 - (playerScreenPos.y - elevationHeight); } - use(){ - if (this.duration >1) { - this.duration = this.duration-1 - } - else { - this.destroy(); + + + function handleMovement(player,currentTime) { + if (currentTime - lastMoveTime < 150) return; + let moved = false; + if (controls.up) { player.move(0, -1); moved = true; } + if (controls.down) { player.move(0, 1); moved = true; } + if (controls.left) { player.move(-1, 0); moved = true; } + if (controls.right) { player.move(1, 0); moved = true; } + if(moved) lastMoveTime = currentTime; + } + function gameLoop(world,currentTime) { + handleMovement(world.player,currentTime); + //draw(currentTime); + requestAnimationFrame(gameLoop); + } + function setupControls() { + document.addEventListener('keydown', e => { + const key = e.key.toLowerCase(); + if (key.startsWith('arrow')) { controls[key.replace('arrow', '')] = true; } + else if (['w', 'z'].includes(key)) { controls.up = true; } + else if (['s'].includes(key)) { controls.down = true; } + else if (['a', 'q'].includes(key)) { controls.left = true; } + else if (['d'].includes(key)) { controls.right = true; } + }); + document.addEventListener('keyup', e => { + const key = e.key.toLowerCase(); + if (key.startsWith('arrow')) { controls[key.replace('arrow', '')] = false; } + else if (['w', 'z'].includes(key)) { controls.up = false; } + else if (['s'].includes(key)) { controls.down = false; } + else if (['a', 'q'].includes(key)) { controls.left = false; } + else if (['d'].includes(key)) { controls.right = false; } + }); + const controlMap = { 'btn-up': 'up', 'btn-down': 'down', 'btn-left': 'left', 'btn-right': 'right' }; + for (const [id, dir] of Object.entries(controlMap)) { + const btn = document.getElementById(id); + btn.addEventListener('touchstart', e => { e.preventDefault(); controls[dir] = true; }, { passive: false }); + btn.addEventListener('touchend', e => { e.preventDefault(); controls[dir] = false; }); } } -} -class Camera { - constructor(x=0,y=0) { - this.x = x; - this.y = y - } - // Méthode de l'objet - setPosition(positon) { - if (!isGameReady) return; - const screenPos = positon.cartToIso(); - const elevationHeight = position.h * 8; - this.x = canvas.width / 2 - screenPos.x; - this.y = canvas.height / 2 - (screenPos.y - elevationHeight); + function shadeColor(c, p) { + let R=parseInt(c.substring(1,3),16),G=parseInt(c.substring(3,5),16),B=parseInt(c.substring(5,7),16);R=parseInt(R*(100+p)/100);G=parseInt(G*(100+p)/100);B=parseInt(B*(100+p)/100);R=(R<255)?R:255;G=(G<255)?G:255;B=(B<255)?B:255; + return "#"+("0"+R.toString(16)).slice(-2)+("0"+G.toString(16)).slice(-2)+("0"+B.toString(16)).slice(-2); } -} -class world { - constructor(name) { - this.name = name; - this.map = new map(1000) - let spawnX, spawnY; - do { - spawnX = Math.floor(Math.random() * this.map.size); - spawnY = Math.floor(Math.random() * this.map.size); - } while (!this.map.tiles[spawnY][spawnX].biome || !this.map.tiles[spawnY][spawnX].biome.movements.includes('walk') ); - this.player = new player(new creature('player',new attributes(HUMAN),1,null,null,this.map.tiles[spawnY][spawnX],'human',HUMAN,10,null),null); - this.camera = new Camera(); - this.camera.setPosition(player.position); + function initializeGame() { + canvas.width = canvas.parentElement.clientWidth; + canvas.height = canvas.parentElement.clientHeight; + setupControls(); + let world=new World('1sttest'); + isGameReady = true; + centerCameraOnPlayer(world.player.position) + loadingScreen.style.display = 'none'; + gameLoop(world); } - // Méthode de l'objet - save() { - // delete after x Tick - } - -} - -const controls = { up: false, down: false, left: false, right: false }; -let lastMoveTime = 0; -let isGameReady = false; -const canvas = document.getElementById('gameCanvas'); -const ctx = canvas.getContext('2d'); -const loadingScreen = document.getElementById('loading'); - -function handleMovement(player,currentTime) { - if (currentTime - lastMoveTime < 150) return; - let moved = false; - if (controls.up) { player.move(0, -1); moved = true; } - if (controls.down) { player.move(0, 1); moved = true; } - if (controls.left) { player.move(-1, 0); moved = true; } - if (controls.right) { player.move(1, 0); moved = true; } - if(moved) lastMoveTime = currentTime; -} -function gameLoop(world,currentTime) { - handleMovement(world.player,currentTime); - draw(currentTime); - requestAnimationFrame(gameLoop); -} -function setupControls() { - document.addEventListener('keydown', e => { - const key = e.key.toLowerCase(); - if (key.startsWith('arrow')) { controls[key.replace('arrow', '')] = true; } - else if (['w', 'z'].includes(key)) { controls.up = true; } - else if (['s'].includes(key)) { controls.down = true; } - else if (['a', 'q'].includes(key)) { controls.left = true; } - else if (['d'].includes(key)) { controls.right = true; } - }); - document.addEventListener('keyup', e => { - const key = e.key.toLowerCase(); - if (key.startsWith('arrow')) { controls[key.replace('arrow', '')] = false; } - else if (['w', 'z'].includes(key)) { controls.up = false; } - else if (['s'].includes(key)) { controls.down = false; } - else if (['a', 'q'].includes(key)) { controls.left = false; } - else if (['d'].includes(key)) { controls.right = false; } - }); - const controlMap = { 'btn-up': 'up', 'btn-down': 'down', 'btn-left': 'left', 'btn-right': 'right' }; - for (const [id, dir] of Object.entries(controlMap)) { - const btn = document.getElementById(id); - btn.addEventListener('touchstart', e => { e.preventDefault(); controls[dir] = true; }, { passive: false }); - btn.addEventListener('touchend', e => { e.preventDefault(); controls[dir] = false; }); - } -} - -function shadeColor(c, p) { - let R=parseInt(c.substring(1,3),16),G=parseInt(c.substring(3,5),16),B=parseInt(c.substring(5,7),16);R=parseInt(R*(100+p)/100);G=parseInt(G*(100+p)/100);B=parseInt(B*(100+p)/100);R=(R<255)?R:255;G=(G<255)?G:255;B=(B<255)?B:255; - return "#"+("0"+R.toString(16)).slice(-2)+("0"+G.toString(16)).slice(-2)+("0"+B.toString(16)).slice(-2); -} -function resizeCanvas() { - canvas.width = canvas.parentElement.clientWidth; - canvas.height = canvas.parentElement.clientHeight; - if (isGameReady) centerCameraOnPlayer(); -} - -function initializeGame() { - resizeCanvas(); - setupControls(); - let world=new world('1sttest'); - isGameReady = true; - centerCameraOnPlayer(world.player); - loadingScreen.style.display = 'none'; - gameLoop(world); -} \ No newline at end of file + initializeGame(); +}) \ No newline at end of file