Skip to content

Commit 1dddb5b

Browse files
authored
Merge pull request #35 from mashirochan/main
2 parents 9934e21 + 3b497cb commit 1dddb5b

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

src/branches/Branch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export default abstract class Branch extends Container {
2727
return this.count >= this.capacity;
2828
}
2929

30-
addDonation(donation: Donation): LeafInfo {
30+
addDonation(donation: Donation, branchLabel: string, isLeftBranch: boolean): LeafInfo {
3131
if (this.full) throw new Error('Branch is full!');
3232

3333
this.donationCount += 1;
34-
return this.leafs[this.count - 1].setDonation(donation);
34+
return this.leafs[this.count - 1].setDonation(donation, branchLabel, this.leafs[this.count - 1].label, isLeftBranch);
3535
}
3636

3737
protected renderBranchSection(sprite: Sprite, label?: string) {

src/branches/Branch01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Leaf from './Leaf.ts';
55
export default class Branch01 extends Branch {
66
constructor() {
77
super();
8-
this.label = 'Branch variant 01';
8+
this.label = 'Branch_01';
99
this.scale = 0.4;
1010
}
1111

src/branches/Leaf.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ export interface LeafInfo {
99
brightness: number;
1010
}
1111

12+
type BranchSide = 'leftSide' | 'rightSide';
13+
type BranchLabel = 'Branch_01' | 'Branch_02';
14+
type LeafLabel = 'Leaf 01' | 'Leaf 02' | 'Leaf 03' | 'Leaf 04' | 'Leaf 05' | 'Leaf 06' | 'Leaf 07' | 'Leaf 08' | 'Leaf 09' | 'Leaf 10' | 'Leaf 11' | 'Leaf 12' | 'Leaf 13' | 'Leaf 14' | 'Leaf 15';
15+
16+
function isLeafLabel(label: string): label is LeafLabel {
17+
return ['Leaf 01', 'Leaf 02', 'Leaf 03', 'Leaf 04', 'Leaf 05', 'Leaf 06', 'Leaf 07', 'Leaf 08', 'Leaf 09', 'Leaf 10', 'Leaf 11', 'Leaf 12', 'Leaf 13', 'Leaf 14', 'Leaf 15'].includes(label);
18+
}
19+
1220
export default class Leaf extends Container {
1321
private readonly prerequisites?: Container[];
1422
private readonly leafSprite;
@@ -17,6 +25,17 @@ export default class Leaf extends Container {
1725

1826
private hasDonation = false;
1927

28+
private static readonly leavesToRotate: Record<BranchSide, Record<BranchLabel, LeafLabel[]>> = {
29+
'leftSide': {
30+
'Branch_01': ['Leaf 01', 'Leaf 02', 'Leaf 03', 'Leaf 04', 'Leaf 05', 'Leaf 06', 'Leaf 07', 'Leaf 08', 'Leaf 09', 'Leaf 10', 'Leaf 11', 'Leaf 12', 'Leaf 14', 'Leaf 15'],
31+
'Branch_02': ['Leaf 01', 'Leaf 02', 'Leaf 03', 'Leaf 04', 'Leaf 05', 'Leaf 06', 'Leaf 07', 'Leaf 08', 'Leaf 09', 'Leaf 11', 'Leaf 12', 'Leaf 13', 'Leaf 15']
32+
},
33+
'rightSide': {
34+
'Branch_01': ['Leaf 12', 'Leaf 13'],
35+
'Branch_02': ['Leaf 13', 'Leaf 14']
36+
}
37+
};
38+
2039
// tint, brightness
2140
public static readonly sakuraThemeLeafColors: [number, number][] = [
2241
[0xff8bd4, 1.75],
@@ -111,7 +130,7 @@ export default class Leaf extends Container {
111130
}
112131
}
113132

114-
setDonation(donation: Donation): LeafInfo {
133+
setDonation(donation: Donation, branchLabel: string, leafLabel: string, isLeftBranch: boolean): LeafInfo {
115134
if (this.hasDonation) {
116135
throw new Error('Cannot reassign leaf!');
117136
}
@@ -122,12 +141,27 @@ export default class Leaf extends Container {
122141
this.usernameText.text = donation.username;
123142
this.amountText.text = `$${donation.amount}`;
124143

144+
const currentBranch: BranchSide = isLeftBranch ? 'leftSide' : 'rightSide';
145+
const currentBranchLabel: BranchLabel = branchLabel === 'Branch_01' ? 'Branch_01' : 'Branch_02'; // Oof...
146+
147+
if (isLeafLabel(leafLabel)) {
148+
const shouldRotateText = Leaf.leavesToRotate[currentBranch][currentBranchLabel].includes(leafLabel);
149+
150+
if (shouldRotateText) {
151+
this.usernameText.angle += 180;
152+
this.usernameText.y = 40;
153+
this.amountText.angle += 180;
154+
this.amountText.y = -40;
155+
}
156+
} else {
157+
console.error(`Error! Invalid leaf label: ${leafLabel}\nSkipping rotation check for this leaf!`);
158+
}
159+
125160
const [tint, brightness] = this.getTint(donation.amount);
126161
this.leafSprite.tint = tint;
127162
const brightnessFilter = new ColorMatrixFilter();
128163
brightnessFilter.brightness(brightness, true);
129164
this.leafSprite.filters = brightnessFilter;
130-
131165
this.eventMode = 'static';
132166
this.cursor = 'pointer';
133167
this.on('pointerdown', () => {

src/tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export async function buildTreeSpriteGraph(
218218
treeContainer.addChild(currentBranch);
219219
}
220220

221-
const { x, y, tint, brightness } = currentBranch.addDonation(donation);
221+
const { x, y, tint, brightness } = currentBranch.addDonation(donation, currentBranch.label, !isLeftBranch);
222222
void db.leaves.upsert({
223223
id: donationId,
224224
x: x,

0 commit comments

Comments
 (0)