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

issue fix #349 (random ball throw), #375 ("throw ball with mouse" bug) #416

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ export function activate(context: vscode.ExtensionContext) {
updateExtensionPositionContext,
),
);

updateStatusBar();

const spec = PetSpecification.fromConfiguration();
Expand Down Expand Up @@ -365,6 +366,15 @@ export function activate(context: vscode.ExtensionContext) {
}),
);

context.subscriptions.push(
vscode.commands.registerCommand('vscode-pets.throw-with-mouse', () => {
const panel = getPetPanel();
if (panel !== undefined) {
panel.setThrowWithMouse(true);
}
}),
);

context.subscriptions.push(
vscode.commands.registerCommand('vscode-pets.delete-pet', async () => {
const panel = getPetPanel();
Expand Down
5 changes: 3 additions & 2 deletions src/panel/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ export function petPanelApp(
if (canvas) {
canvas.style.display = 'block';
}
ballState = new BallState(100, 100, 4, 5);
/* spawn with randomize */
ballState = new BallState(0, 0, 0, 0, true);
}

function dynamicThrowOn() {
Expand Down Expand Up @@ -430,6 +431,7 @@ export function petPanelApp(
canvas.style.display = 'none';
}
}

function throwBall() {
if (!ballState.paused) {
requestAnimationFrame(throwBall);
Expand Down Expand Up @@ -461,7 +463,6 @@ export function petPanelApp(
}

ballState.vy += gravity;

ballState.cx += ballState.vx;
ballState.cy += ballState.vy;
drawBall();
Expand Down
44 changes: 39 additions & 5 deletions src/panel/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,47 @@ export class BallState {
vy: number;
paused: boolean;

constructor(cx: number, cy: number, vx: number, vy: number) {
this.cx = cx;
this.cy = cy;
this.vx = vx;
this.vy = vy;
constructor(
cx: number,
cy: number,
vx: number,
vy: number,
randomize?: boolean,
) {
if (randomize !== undefined && randomize === true) {
const rVals = this.randomizeBallState();
this.cx = rVals[0];
this.cy = rVals[1];
this.vx = rVals[2];
this.vy = rVals[3];
} else {
this.cx = cx;
this.cy = cy;
this.vx = vx;
this.vy = vy;
}
this.paused = false;
}

private randomizeBallState(): Array<number> {
/* velocity */
const randVx: Array<number> = [
//avoids values close to 0
-1 * (Math.floor(Math.random() * 12) + 3), //left
Math.floor(Math.random() * 12) + 3, // right
];
const randVy: number = Math.floor(Math.random() * 20 - 10);
/* position */
const randX: number = Math.floor(
Math.random() * (window.innerWidth * 0.8) + 10,
);
const randY: number =
Math.floor(Math.random() * (window.innerHeight * 0.6)) + 30;

/* flip a coin to throw left or right */
const coin: number = Math.floor(Math.random() * 2);
return [randX, randY, randVx[coin], randVy];
}
}

export function isStateAboveGround(state: States): boolean {
Expand Down