Skip to content

Refactoring and New Init Features #38

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
"@typescript-eslint"
],
"rules": {
"prettier/prettier": "error",
"prettier/prettier": ["warn", {
"endOfLine": "auto"
}],
"default-param-last": 0,
"linebreak-style": 0,
"camelcase": ["error", {"allow": ["((bj_)\\w+[A-Z]$)"]}], //exclude bj_CAPITAL_LETTER
"eqeqeq": ["error", "always", {"null": "never"}],
"@typescript-eslint/no-implicit-any-catch": "error",
"@typescript-eslint/strict-boolean-expressions": ["warn"],
"@typescript-eslint/no-non-null-assertion": 0,
"no-underscore-dangle": ["error", { "allowAfterThis": true, "allowAfterSuper": true }],
"no-throw-literal": 0, //Maybe one day if wc3 gets a polyfill for the debug library
"no-cond-assign": 0,
Expand Down
11 changes: 3 additions & 8 deletions globals/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { MapPlayer } from "../handles/player";

export * from "./order";
export const Players: MapPlayer[] = [];

for (let i = 0; i < bj_MAX_PLAYER_SLOTS; i++) {
const pl = MapPlayer.fromHandle(Player(i));
if (pl) {
Players[i] = pl;
}
}
export const Players = Array.from({ length: bj_MAX_PLAYER_SLOTS }).map((_, i) =>
MapPlayer.fromHandle(Player(i))
);
24 changes: 2 additions & 22 deletions handles/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { Handle } from "./handle";
import { Point } from "./point";

export class Camera {
// eslint-disable-next-line no-useless-constructor
private constructor() {
// nothing
}

public static set visible(flag: boolean) {
DisplayCineFilter(flag);
}
Expand Down Expand Up @@ -339,17 +334,8 @@ export class CameraSetup extends Handle<camerasetup> {
/**
* Creates a new CameraSetup object.
*/
public static create(): CameraSetup | undefined {
const handle = CreateCameraSetup();
if (handle) {
const obj = this.getObject(handle) as CameraSetup;

const values: Record<string, unknown> = {};
values.handle = handle;

return Object.assign(obj, values);
}
return undefined;
public static create() {
return this.fromHandle(CreateCameraSetup());
}

/**
Expand Down Expand Up @@ -502,10 +488,4 @@ export class CameraSetup extends Handle<camerasetup> {
public setField(whichField: camerafield, value: number, duration: number) {
CameraSetupSetField(this.handle, whichField, value, duration);
}

public static fromHandle(
handle: camerasetup | undefined
): CameraSetup | undefined {
return handle ? this.getObject(handle) : undefined;
}
}
50 changes: 12 additions & 38 deletions handles/destructable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,11 @@ export class Destructable extends Widget {
objectId: number,
x: number,
y: number,
face?: number,
scale?: number,
variation?: number,
face = 0,
scale = 1,
variation = 0,
skinId?: number
): Destructable | undefined {
if (face === undefined) face = 0;
if (scale === undefined) scale = 1;
if (variation === undefined) variation = 0;

) {
let handle: destructable | undefined;

if (skinId !== undefined) {
Expand All @@ -79,18 +75,10 @@ export class Destructable extends Widget {
handle = CreateDestructable(objectId, x, y, face, scale, variation);
}

if (handle) {
const obj = this.getObject(handle) as Destructable;

const values: Record<string, unknown> = {};
values.handle = handle;
if (skinId !== undefined) {
values.skin = skinId;
}

return Object.assign(obj, values);
}
return undefined;
return this.fromHandle(
handle,
skinId !== undefined ? { skin: skinId } : undefined
);
}

/**
Expand Down Expand Up @@ -134,18 +122,10 @@ export class Destructable extends Widget {
handle = CreateDestructableZ(objectId, x, y, z, face, scale, variation);
}

if (handle) {
const obj = this.getObject(handle) as Destructable;

const values: Record<string, unknown> = {};
values.handle = handle;
if (skinId !== undefined) {
values.skin = skinId;
}

return Object.assign(obj, values);
}
return undefined;
return this.fromHandle(
handle,
skinId !== undefined ? { skin: skinId } : undefined
);
}

public set invulnerable(flag: boolean) {
Expand Down Expand Up @@ -238,10 +218,4 @@ export class Destructable extends Widget {
public static override fromEvent() {
return this.fromHandle(GetTriggerDestructable());
}

public static override fromHandle(
handle: destructable | undefined
): Destructable | undefined {
return handle ? this.getObject(handle) : undefined;
}
}
42 changes: 6 additions & 36 deletions handles/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class DialogButton extends Handle<button> {

let handle: button | undefined;

if (quit === false) {
if (!quit) {
handle = DialogAddButton(whichDialog.handle, text, hotkey);
} else {
handle = DialogAddQuitButton(whichDialog.handle, score, text, hotkey);
Expand All @@ -40,36 +40,21 @@ export class DialogButton extends Handle<button> {
hotkey = 0,
quit = false,
score = false
): DialogButton | undefined {
) {
let handle: button | undefined;

if (quit === false) {
if (!quit) {
handle = DialogAddButton(whichDialog.handle, text, hotkey);
} else {
handle = DialogAddQuitButton(whichDialog.handle, score, text, hotkey);
}

if (handle) {
const obj = this.getObject(handle) as DialogButton;

const values: Record<string, unknown> = {};
values.handle = handle;

return Object.assign(obj, values);
}

return undefined;
return this.fromHandle(handle);
}

public static fromEvent() {
return this.fromHandle(GetClickedButton());
}

public static fromHandle(
handle: button | undefined
): DialogButton | undefined {
return handle ? this.getObject(handle) : undefined;
}
}

/**
Expand Down Expand Up @@ -114,19 +99,8 @@ export class Dialog extends Handle<dialog> {
super(handle);
}

public static create(): Dialog | undefined {
const handle = DialogCreate();

if (handle) {
const obj = this.getObject(handle) as Dialog;

const values: Record<string, unknown> = {};
values.handle = handle;

return Object.assign(obj, values);
}

return undefined;
public static create() {
return this.fromHandle(DialogCreate());
}

public addButton(text: string, hotkey = 0, quit = false, score = false) {
Expand Down Expand Up @@ -155,8 +129,4 @@ export class Dialog extends Handle<dialog> {
public static fromEvent() {
return this.fromHandle(GetClickedDialog());
}

public static fromHandle(handle: dialog | undefined): Dialog | undefined {
return handle ? this.getObject(handle) : undefined;
}
}
71 changes: 16 additions & 55 deletions handles/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,8 @@ export class Effect extends Handle<effect> {
modelName: string,
x: number,
y: number
): Effect | undefined {
const handle = AddSpecialEffect(modelName, x, y);
if (handle) {
const obj = this.getObject(handle) as Effect;

const values: Record<string, unknown> = {};
values.handle = handle;

return Object.assign(obj, values);
}

return undefined;
) {
return this.fromHandle(AddSpecialEffect(modelName, x, y));
}

/**
Expand All @@ -92,23 +82,14 @@ export class Effect extends Handle<effect> {
modelName: string,
targetWidget: Widget,
attachPointName: string
): Effect | undefined {
const handle = AddSpecialEffectTarget(
modelName,
targetWidget.handle,
attachPointName
) {
return this.fromHandle(
AddSpecialEffectTarget(modelName, targetWidget.handle, attachPointName),
{
attachWidget: targetWidget,
attachPointName,
}
);
if (handle) {
const obj = this.getObject(handle) as Effect;

const values: Record<string, unknown> = {};
values.handle = handle;
values.attachWidget = targetWidget;
values.attachPointName = attachPointName;

return Object.assign(obj, values);
}
return undefined;
}

/**
Expand All @@ -123,17 +104,8 @@ export class Effect extends Handle<effect> {
effectType: effecttype,
x: number,
y: number
): Effect | undefined {
const handle = AddSpellEffectById(abilityId, effectType, x, y);
if (handle) {
const obj = this.getObject(handle) as Effect;

const values: Record<string, unknown> = {};
values.handle = handle;

return Object.assign(obj, values);
}
return undefined;
) {
return this.fromHandle(AddSpellEffectById(abilityId, effectType, x, y));
}

/**
Expand All @@ -151,24 +123,17 @@ export class Effect extends Handle<effect> {
effectType: effecttype,
targetWidget: Widget,
attachPointName: string
): Effect | undefined {
) {
const handle = AddSpellEffectTargetById(
abilityId,
effectType,
targetWidget.handle,
attachPointName
);
if (handle) {
const obj = this.getObject(handle) as Effect;

const values: Record<string, unknown> = {};
values.handle = handle;
values.attachWidget = targetWidget;
values.attachPointName = attachPointName;

return Object.assign(obj, values);
}
return undefined;
return this.fromHandle(handle, {
attachWidget: targetWidget,
attachPointName,
});
}

public get scale() {
Expand Down Expand Up @@ -297,8 +262,4 @@ export class Effect extends Handle<effect> {
public setYaw(y: number) {
BlzSetSpecialEffectYaw(this.handle, y);
}

public static fromHandle(handle: effect | undefined): Effect | undefined {
return handle ? this.getObject(handle) : undefined;
}
}
17 changes: 1 addition & 16 deletions handles/fogmodifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,7 @@ export class FogModifier extends Handle<fogmodifier> {
afterUnits
);

if (handle) {
const obj = this.getObject(handle) as FogModifier;

const values: Record<string, unknown> = {};
values.handle = handle;

return Object.assign(obj, values);
}

return undefined;
return this.fromHandle(handle);
}

public destroy() {
Expand All @@ -102,12 +93,6 @@ export class FogModifier extends Handle<fogmodifier> {
FogModifierStop(this.handle);
}

public static fromHandle(
handle: fogmodifier | undefined
): FogModifier | undefined {
return handle ? this.getObject(handle) : undefined;
}

public static fromRect(
forWhichPlayer: MapPlayer,
whichState: fogstate,
Expand Down
Loading