-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate class for barehanded attack sprite
- Loading branch information
1 parent
0e1cd21
commit e365114
Showing
3 changed files
with
79 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/*************************************************************************** | ||
* Copyright © 2023 - Stendhal * | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU Affero General Public License as * | ||
* published by the Free Software Foundation; either version 3 of the * | ||
* License, or (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
import { ActionSprite } from "./ActionSprite"; | ||
import { RPEntity } from "../../entity/RPEntity"; | ||
|
||
declare var stendhal: any; | ||
|
||
|
||
export class BarehandAttackSprite extends ActionSprite { | ||
|
||
private readonly dir: number; | ||
private readonly image: HTMLImageElement; | ||
|
||
|
||
constructor(source: RPEntity, image: HTMLImageElement) { | ||
super(); | ||
this.dir = source["dir"]; | ||
this.image = image; | ||
} | ||
|
||
public override draw(ctx: CanvasRenderingContext2D, x: number, y: number, entityWidth: number, | ||
entityHeight: number) { | ||
if (!this.image || !this.image.height) { | ||
return; | ||
} | ||
|
||
const dtime = Date.now() - this.initTime; | ||
const frameIndex = Math.floor(Math.min(dtime / 60, 3)); | ||
const drawWidth = this.image.width / 3; | ||
const drawHeight = this.image.height / 4; | ||
const centerX = x + (entityWidth - drawWidth) / 2; | ||
const centerY = y + (entityHeight - drawHeight) / 2; | ||
|
||
// offset sprite for facing direction | ||
let sx, sy; | ||
switch (this.dir+"") { | ||
case "1": // UP | ||
sx = centerX + (stendhal.ui.gamewindow.targetTileWidth / 2); | ||
sy = y - (stendhal.ui.gamewindow.targetTileHeight * 1.5); | ||
break; | ||
case "3": // DOWN | ||
sx = centerX; | ||
sy = y + entityHeight - drawHeight + (stendhal.ui.gamewindow.targetTileHeight / 2); | ||
break; | ||
case "4": // LEFT | ||
sx = x - (stendhal.ui.gamewindow.targetTileWidth / 2); | ||
sy = centerY - (stendhal.ui.gamewindow.targetTileHeight / 2); | ||
break; | ||
case "2": // RIGHT | ||
sx = x + entityWidth - drawWidth + (stendhal.ui.gamewindow.targetTileWidth / 2); | ||
sy = centerY; // - ICON_OFFSET; // ICON_OFFSET = 8 in Java client | ||
break; | ||
default: | ||
sx = centerX; | ||
sy = centerY; | ||
} | ||
|
||
ctx.drawImage(this.image, frameIndex * drawWidth, (this.dir - 1) * drawHeight, | ||
drawWidth, drawHeight, sx, sy, drawWidth, drawHeight); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters