From 266c2b2ab10a9eea1463eb99abdfaf00fcbc49bb Mon Sep 17 00:00:00 2001 From: Ajeet_rana Date: Sun, 6 Oct 2024 02:14:49 +0530 Subject: [PATCH] Added NPCs --- src/gameObjects/map_city/index.js | 2 + src/gameObjects/map_city/npcsOnmap_city.js | 84 +++++++++++++++++++ .../map_forest/butterfly.gameObject.js | 2 +- src/gameObjects/map_forest/index.js | 2 +- 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/gameObjects/map_city/npcsOnmap_city.js diff --git a/src/gameObjects/map_city/index.js b/src/gameObjects/map_city/index.js index c1666db4..2cf19dcf 100644 --- a/src/gameObjects/map_city/index.js +++ b/src/gameObjects/map_city/index.js @@ -1,4 +1,6 @@ +import { npcsInCityMap } from './npcsOnmap_city'; const gameObjects = [ + npcsInCityMap, // Add more game objects here ]; diff --git a/src/gameObjects/map_city/npcsOnmap_city.js b/src/gameObjects/map_city/npcsOnmap_city.js new file mode 100644 index 00000000..3a017822 --- /dev/null +++ b/src/gameObjects/map_city/npcsOnmap_city.js @@ -0,0 +1,84 @@ +import { scaleFactor } from '../../constants'; +import { makeNpc } from '../../factories/npc.factory'; + +export const npcsInCityMap = (k, map, spawnpoints) => { + const npcStore = []; + + // function to spawn NPCs based on object patterns and position adjustments + const createNpcsForPattern = ( + patterns, + adjustments, + direction = 'idle-side' + ) => { + const objects = map + .get('*') + .filter((obj) => + patterns.some((pattern) => + obj.tags.some((tag) => pattern.test(tag)) + ) + ); + + objects.forEach((obj) => { + const { xAdjust, yAdjust } = adjustments; + const npcDirection = obj.tiledProps?.direction || direction; + + const npc = makeNpc( + obj.name, + k.vec2( + (map.pos.x + obj.pos.x + xAdjust) * scaleFactor, + (map.pos.y + obj.pos.y + yAdjust) * scaleFactor + ), + npcDirection, + 'map_city' + ); + + npcStore.push(npc); + }); + }; + + // Define patterns and their corresponding adjustments + const npcPatterns = [ + { + patterns: [/stall_/, /snack_bar_/], + adjustments: { xAdjust: 12, yAdjust: 18 }, + direction: 'idle-down', + }, + { + patterns: [/snack_bar_red/], + adjustments: { xAdjust: 12, yAdjust: 18 }, + direction: 'idle-up', + }, + { + patterns: [/burger_bar_chairs/], + adjustments: { xAdjust: 12, yAdjust: 6 }, + direction: 'idle-up', + }, + { + patterns: [/enter_map_arcade/], + adjustments: { xAdjust: 12, yAdjust: 6 }, + direction: 'idle-down', + }, + { + patterns: [/car_/], + adjustments: { xAdjust: 30, yAdjust: 6 }, + direction: 'idle-down', + }, + { + patterns: [/car_orange/], + adjustments: { xAdjust: 40, yAdjust: 6 }, + direction: 'idle-side', + }, + { + patterns: [/burger_bar_menu_sign/], + adjustments: { xAdjust: 12, yAdjust: 40 }, + direction: 'idle-down', + }, + ]; + + // Generate NPCs + npcPatterns.forEach(({ patterns, adjustments, direction }) => { + createNpcsForPattern(patterns, adjustments, direction); + }); + + return npcStore; +}; diff --git a/src/gameObjects/map_forest/butterfly.gameObject.js b/src/gameObjects/map_forest/butterfly.gameObject.js index 2fdfd082..67afee50 100644 --- a/src/gameObjects/map_forest/butterfly.gameObject.js +++ b/src/gameObjects/map_forest/butterfly.gameObject.js @@ -13,7 +13,7 @@ export const butterfly = (k, map, spawnpoints) => { return k.make([ k.sprite('butterfly', { anim: 'idle-side' }), k.area({ - shape: new k.Rect(k.vec2(0), 16,16), + shape: new k.Rect(k.vec2(0), 16, 16), }), k.body({ isStatic: true }), k.anchor('center'), diff --git a/src/gameObjects/map_forest/index.js b/src/gameObjects/map_forest/index.js index 8f4bba73..33a1998f 100644 --- a/src/gameObjects/map_forest/index.js +++ b/src/gameObjects/map_forest/index.js @@ -1,4 +1,4 @@ -import { butterfly } from "./butterfly.gameObject"; +import { butterfly } from './butterfly.gameObject'; const gameObjects = [butterfly];