Skip to content

Commit

Permalink
add: crafting screen classes
Browse files Browse the repository at this point in the history
  • Loading branch information
agustincarriso committed Jan 17, 2025
1 parent 84b0f71 commit 864387d
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 6 deletions.
98 changes: 92 additions & 6 deletions src/craftingmachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
PointerEventType,
Transform
} from '@dcl/sdk/ecs'
import { WearablesState } from './enums'
import { ItemIcons, WearablesState } from './enums'
import { type Recipe } from './projectdata'
import { type ItemAmountPanel } from './ui/itemamountpanel'
import { type SpritePlane } from './ui/spriteplane'
Expand All @@ -17,6 +17,7 @@ import { ProjectLoader } from './projectloader'
import { som } from './som'
import { SoundManager } from '../shared-dcl/src/sound/soundmanager'
import { openExternalUrl } from '~system/RestrictedActions'
import { GameData } from './gamedata'

export type MachineData = {
filename: string
Expand Down Expand Up @@ -58,10 +59,10 @@ export class CraftingMachine {

public nameTextEntity: Entity = engine.addEntity()
public nameTxt: string = ''
public descTxt: string = ''
public descTxt: string = ''
public idTxt: string = ''
public levelMinTxt: string = ''
public readyTxt: string = ''
public readyTxt: string = ''

public iconSprite: SpritePlane | null = null
public arrowSprite: SpritePlane | null = null
Expand Down Expand Up @@ -214,13 +215,98 @@ export class CraftingMachine {
return loader.spawnSceneObject(_data, false)
}

prevRecipe(): void {}
loadScreen(): void {}

nextRecipe(): void {}
prevRecipe(): void {
if (this.isBusy) return
if (--this.recipeIndex < -1) {
this.recipeIndex = GameData.recipes.length - 1
}
if (this.recipeIndex === -1) {
this.showInstructions()
} else {
this.showRecipe(this.recipeIndex)
}
}

refreshRecipe(): void {
if (this.recipeIndex < 0) {
this.showInstructions()
} else {
this.showRecipe(this.recipeIndex)
}
}

nextRecipe(): void {
if (this.isBusy) return
const numRecipes = GameData.recipes.length
if (++this.recipeIndex > numRecipes) {
this.recipeIndex = 0
}
if (this.recipeIndex === numRecipes) {
this.showInstructions()
} else {
this.showRecipe(this.recipeIndex)
}
}

startCrafting(recipeNum: number): void {}

enableCrafting(onOrOff: boolean = true): void {}

loadScreen(): void {}
showInstructions(): void {
this.pageIndex = 0
this.recipeIndex = -1

this.showName('Craft-O-Matic')
this.showDesc(
'Use the red arrows below to see the crafting recipes.\nWhen you have all the ingredients, the lever to the right\nwill glow. Click the lever to craft your item!'
)
this.showId('')

// clear the ingredients
this.clearRecipe()

// this.readyTxt.color = Color3.FromHexString('#DDDDDD') // "#22BB44"
this.setCooldownStatus()
}

showRecipe(recipeNum: number): void {}

showName(msg: string): void {
if (this.nameTxt != null) {
this.nameTxt = msg
}
}

showDesc(msg: string): void {
if (this.descTxt != null) {
this.descTxt = msg
}
}

showId(msg: string): void {
if (this.idTxt != null) {
this.idTxt = msg
}
}

clearRecipe(): void {
if (this.iconSprite != null) {
this.iconSprite.changeFrame(ItemIcons.Empty)
}

this.enableCrafting(false)

let tile
for (var i: number = 0; i < this.ingredientPanels.length; i++) {
tile = this.ingredientPanels[i]
tile.clear(ItemIcons.Empty)
tile.showText('')
}

this.levelMinTxt = ''
}

setCooldownStatus(): void {}
}
110 changes: 110 additions & 0 deletions src/gamedata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Recipe } from './projectdata'

/**
* Local store of game-wide data (not user specific)
*/
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class GameData {
public static instance: GameData | null = null

public static thresholds: number[] = new Array<number>()
public static recipes: Recipe[] = new Array<Recipe>()
public static wearableCounts: Record<number, number> = {}
public static recipeIndex: number = -1

public static createInstance(): void {
if (GameData.instance == null) {
GameData.instance = new GameData()
}
}

public static setLevels(thresholds: []): void {
// levelObjects:Array<Object>
// log(thresholds);
GameData.thresholds = thresholds
}

// eslint-disable-next-line @typescript-eslint/ban-types
public static setRecipes(recipeList: Object[]): void {
// log("setRecipes()");
const recipeArray: Recipe[] = []
let item: Recipe

for (var i: number = 0; i < recipeList.length; i++) {
try {
item = this.populate(new Recipe(), recipeList[i]) // TODO: error checking
recipeArray.push(item)
} catch (e) {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
console.log('Error converting recipe item: ' + e)
}
}

if (recipeArray.length > 0) {
// NOTE: This overrides the existing recipes, since server-side is authoritative
GameData.recipes = recipeArray
// log(recipeArray);
}
}

public static setWearableCounts(itemCounts: Record<number, number>): void {
GameData.wearableCounts = itemCounts
}

public static getWearableCount(wi: number): number {
if (wi === 0) return 0
let count: number = GameData.wearableCounts[wi]
if (count == null) count = 0
return count
}

// eslint-disable-next-line @typescript-eslint/no-useless-constructor
constructor() {
// GameData.thresholds = [];
// GameData.recipes = [];
}

// --- UTILITY FUNCTIONS ---
// eslint-disable-next-line @typescript-eslint/ban-types
static populate(target: Recipe, ...args: Array<Record<string, any>>): Recipe {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!target) {
throw TypeError('Cannot convert undefined or null to object')
}
for (const source of args) {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (source) {
// eslint-disable-next-line no-return-assign
Object.keys(source).forEach((key) => ((target as any)[key] = source[key]))
}
}
return target
}

public static getRecipeNum(index: number): Recipe | null {
if (index < GameData.recipes.length) {
return GameData.recipes[index]
}
return null
}

public static getRecipeById(recipeId: string): Recipe | null {
let item: Recipe
for (var i: number = 0; i < GameData.recipes.length; i++) {
item = GameData.recipes[i]
if (item.id === recipeId) {
return item
}
}
return null
}

public static nextRecipe(): Recipe | null {
if (GameData.recipes.length === 0) return null

if (++GameData.recipeIndex >= GameData.recipes.length) {
GameData.recipeIndex = 0
}
return GameData.recipes[GameData.recipeIndex]
}
}

0 comments on commit 864387d

Please sign in to comment.