Skip to content

Commit

Permalink
Refactored item sprite handling
Browse files Browse the repository at this point in the history
  • Loading branch information
colecrouter committed Nov 30, 2024
1 parent f6ae42c commit 753cf2a
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 793 deletions.
397 changes: 223 additions & 174 deletions src/dump.ts

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions src/lib/ItemData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Size } from "$types/items/1.5";
import type { Size } from "$types/items/1.6";
import {
type Clothing,
FurnitureType,
ObjectCategory,
type ItemInformation,
type Shirt,
} from "$types/items/1.6";
import { Category } from "$types/save/1.5";
import type { Item } from "$types/save/1.6";
import jsondata from "../../static/iteminfo.json";

Expand Down Expand Up @@ -135,18 +135,18 @@ export const QualityToEdibilityMultiplier = new Map([

// TODO: I don't know what items are allowed to have quality, or if there's some sort of rule.
export const CategoriesWithQuality = new Set([
Category.Fish,
Category.Egg,
Category.Milk,
Category.Cooking,
Category.SellAtPierres,
Category.SellAtPierresAndMarnies,
Category.ArtisanGoods,
Category.Syrup,
Category.Vegetable,
Category.Fruit,
Category.Flower,
Category.Forage,
ObjectCategory.Fish,
ObjectCategory.Egg,
ObjectCategory.Milk,
ObjectCategory.Cooking,
ObjectCategory.SellAtPierres,
ObjectCategory.SellAtPierresAndMarnies,
ObjectCategory.ArtisanGoods,
ObjectCategory.Syrup,
ObjectCategory.Vegetable,
ObjectCategory.Fruit,
ObjectCategory.Flower,
ObjectCategory.Forage,
]);

export const DefaultFurnitureSizes = new Map<FurnitureType, Size>([
Expand Down Expand Up @@ -282,8 +282,8 @@ export const ItemNameHelper = (item: Item) => {
return item.name;
};

export const Shirts = new Map<string, Clothing>(
export const Shirts = new Map<string, Shirt>(
jsondata
.filter(([name, item]) => item._type === "Shirt")
.map(([name, item]) => [item.ItemId, item]),
.filter(([, item]) => item._type === "Shirt")
.map(([, item]) => [item._key, item]),
);
68 changes: 51 additions & 17 deletions src/lib/Spritesheet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { ItemInformation } from "$types/items/1.6";
import { DefaultFurnitureSizes } from "$lib/ItemData";
import {
FurnitureType,
type ItemInformation,
type Size,
} from "$types/items/1.6";

const ShirtsWithFemaleVariant = new Set<string>([
"Plain Shirt",
Expand All @@ -9,19 +14,12 @@ const ShirtsWithFemaleVariant = new Set<string>([
"Classy Top",
]);

const ShirtsWithMessedUpSpriteIndex = new Map<number, { x: number; y: number }>(
[
[1997, { x: 256, y: 64 }],
[1998, { x: 256, y: 0 }],
],
);

export const GetSpritesheet = (lookupItem: ItemInformation): string => {
let spritesheet = "";
switch (lookupItem._type) {
case "Object":
case "Boots":
if (lookupItem.Texture === "TileSheets\\Objects_2") {
if (lookupItem.texture === "TileSheets\\Objects_2") {
spritesheet = "Objects_2.png";
} else {
spritesheet = "springobjects.png";
Expand Down Expand Up @@ -66,14 +64,17 @@ export const IndexToSprite = (
return { x, y };
};

export const GetSprite = (
info: Pick<ItemInformation, "_type" | "Texture">,
index: number,
dyeable: boolean | undefined = undefined,
): { x: number; y: number } => {
export const GetSprite = (info: ItemInformation): { x: number; y: number } => {
const index =
"menuSpriteIndex" in info && info.menuSpriteIndex !== undefined
? info.menuSpriteIndex
: (info.spriteIndex ?? Number(info.spriteIndex ?? info._key));
if (Number.isNaN(index)) throw new Error("Invalid sprite index");

const dyeable = "canBeDyed" in info && info.canBeDyed;
switch (info._type) {
case "Object":
if (info.Texture === "TileSheets\\Objects_2") {
if (info.texture === "TileSheets\\Objects_2") {
return IndexToSprite(index, 16, 16, 128, 320);
}
return IndexToSprite(index, 16, 16, 384, 624);
Expand All @@ -89,8 +90,13 @@ export const GetSprite = (
return { x: pantSprite.x, y: pantSprite.y - 672 };
}
case "Shirt": {
// let shirtSprite = ShirtsWithMessedUpSpriteIndex.get(index);
// if (shirtSprite) { return shirtSprite; }
/*
Shirts are a bit weird, because the sprite sheet is 256x608, but the shirts are 8x8,
but it is split into two columns; one for regular shirts and one for dyeable shirts.
When the shirt is dyeable, the sprite index is the same as the regular shirt, but the
corresponding sprite is 128 pixels to the right.
*/
const shirtSprite = IndexToSprite(index, 8, 32, 128, 608, 128);
return dyeable
? { x: shirtSprite.x - 128, y: shirtSprite.y }
Expand All @@ -109,6 +115,34 @@ export const GetSprite = (
}
};

export const GetSize = (info: ItemInformation): Size => {
switch (info._type) {
case "BigCraftable":
return { width: 16, height: 32 };
case "Shirt":
return { width: 8, height: 8 };
case "Hat":
return { width: 20, height: 20 };
case "Furniture": {
if (info.tilesheetSize) {
return {
width: info.tilesheetSize.width * 16,
height: info.tilesheetSize.height * 16,
};
}

return (
DefaultFurnitureSizes.get(info.type) ?? {
width: 16,
height: 16,
}
);
}
default:
return { width: 16, height: 16 };
}
};

export const GetPlayerSpriteForPants = (index: number, isMale: boolean) => {
const { x, y } = IndexToSprite(index, 192, 688, 1920, 1376);
return isMale ? { x, y } : { x: x - 96, y };
Expand Down
Loading

0 comments on commit 753cf2a

Please sign in to comment.