Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: character scaling #9

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@
"command": "codachi.spawnNewPet",
"title": "Codachi: New Pet"
}
]
],
"configuration": [
{
"properties": {
"codachi.scaleFactor": {
"type": "number",
"default": 1.0,
"description": "The scale factor to apply to the pet",
"scope": "window"
}
}
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
Expand Down
38 changes: 28 additions & 10 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const randomPet = (): UserPet =>
generatePet({
type: randomPetType(),
name: randomPetName(),
// TODO: Pass in scale
})

const getPetFromStorage = (storage: vscode.Memento): UserPet => {
Expand Down Expand Up @@ -46,16 +47,26 @@ class PetPanel {

constructor({
userPet,
extensionPath,
extensionUri,
context,
}: {
userPet: UserPet
extensionPath: string
extensionUri: vscode.Uri
context: vscode.ExtensionContext
}) {
this._userPet = userPet
this._extensionUri = extensionUri
this._mediaPath = path.join(extensionPath, 'media')
this._extensionUri = context.extensionUri
this._mediaPath = path.join(context.extensionPath, 'media')
}

updateScale(context: vscode.ExtensionContext, scale: number) {
if (!this.panel) {
return
}
this._userPet.scale = scale
this.panel.webview.postMessage({
command: 'update-pet',
data: { userPet: this._userPet },
})
context.globalState.update('pets', [this._userPet])
}

onKeystroke(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -227,8 +238,7 @@ export function activate(context: vscode.ExtensionContext) {

const petPanel = new PetPanel({
userPet,
extensionUri: context.extensionUri,
extensionPath: context.extensionPath,
context,
}) // Default pet is random

vscode.commands.registerCommand('type', async (args) => {
Expand Down Expand Up @@ -288,13 +298,21 @@ export function activate(context: vscode.ExtensionContext) {
async deserializeWebviewPanel(panel: vscode.WebviewPanel, _: any) {
PetPanel.currentPanel = new PetPanel({
userPet,
extensionUri: context.extensionUri,
extensionPath: context.extensionPath,
context,
})
petPanel.createPanel({ context, panel })
},
})
}

vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('codachi.scaleFactor')) {
const scaleFactor = vscode.workspace
.getConfiguration()
.get('codachi.scaleFactor', 1.0)
petPanel.updateScale(context, scaleFactor)
}
})
}

// this method is called when your extension is deactivated
Expand Down
3 changes: 2 additions & 1 deletion src/panel/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const tick = ({ userPet }: { userPet: UserPet }) => {
direction: userPet.direction,
speed: userPet.speed,
offset: getPetAnimations({ userPet }).animation.offset || 0,
scale: userPet.scale,
})

userPet.leftPosition = leftPosition
Expand All @@ -47,7 +48,7 @@ const tick = ({ userPet }: { userPet: UserPet }) => {
movementContainer.style.marginLeft = `${userPet.leftPosition}px`

const petImageElement = dom.getPetImageSelector()
petImageElement.style.transform = `scaleX(${userPet.direction})`
petImageElement.style.transform = `scaleX(${userPet.direction}) scale(${userPet.scale})`

if (userPet.isTransitionIn) {
const { transition: animation } = getPetAnimations({
Expand Down
2 changes: 2 additions & 0 deletions src/panel/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export const getPetAnimations = ({
}
}

// TODO: Set scale (passed from settings)
export const generatePet = ({ name, type }: UserPetArgs): UserPet => ({
leftPosition: 0,
speed: 0,
Expand All @@ -431,6 +432,7 @@ export const generatePet = ({ name, type }: UserPetArgs): UserPet => ({
isTransitionIn: true,
name,
type,
scale: 1, // TODO: require scale (passed from settings)
})

export const getLevel = ({
Expand Down
5 changes: 4 additions & 1 deletion src/panel/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Transforms, NextFrameOpts, Direction } from './'

export const transforms: Transforms = {
idle: {
nextFrame: ({ direction, offset }: NextFrameOpts) => ({
nextFrame: ({ direction, offset, scale }: NextFrameOpts) => ({
direction,
leftPosition: offset,
scale,
}),
},
walking: {
Expand All @@ -13,6 +14,7 @@ export const transforms: Transforms = {
leftPosition: oldLeftPosition,
direction: oldDirection,
speed,
scale,
}: // offset,
NextFrameOpts) => {
const direction =
Expand All @@ -30,6 +32,7 @@ export const transforms: Transforms = {
return {
direction,
leftPosition,
scale,
}
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/panel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface UserPetBaseProps {
xp: number
state: PetState
isTransitionIn: boolean
scale: number
}

export type PetType = 'monster1' | 'monster2' | 'unknown'
Expand All @@ -59,6 +60,7 @@ export type NextFrameOpts = {
direction: number
speed: number
offset: number
scale: number
}

export type NextFrameFnReturn = {
Expand Down