create account

[ARW Game] Mixins for TypeScript by smjn

View this thread on: hive.blogpeakd.comecency.com
· @smjn ·
$16.21
[ARW Game] Mixins for TypeScript
## Polished mixin solution 
Work has continued with [ARW](https://apps.spelmakare.se/arw) the past weeks. Not much progress with the actual game, been concentrating on reviewing and improving the code before continuing on.

I have [previously](https://peakd.com/hive-176981/@smjn/tmos-game-using-mixins-in-typescript) described the Mixin solution, but would like to revisit it as it got some simplifications that are worth talking about. As with the [waitDo class](https://peakd.com/hive-176981/@smjn/arw-game-a-timer-for-making-sequences-of-events), I have [uploaded](https://github.com/smjnab/mixin) the Mixin class to GitHub.
<center>![Class using a mixin.](https://files.peakd.com/file/peakd-hive/smjn/23swbmk7Zck6on7C2qv34idGmgqXemLWsiVFeqWJghYdgx9bXNRNJF2rmBcDbGD57XqRe.png)</center><br>

## Mixin
Mixin is a solution based on the alternative solution found on [TypeScriptLang.org](https://www.typescriptlang.org/docs/handbook/mixins.html). I wanted a solution that merged with regular class inheritance and where mixins could be initialized in a similar manner as a regular class.

The solution is an abstract Mixin class that is used to create new mixins. Check the [GitHub page](https://github.com/smjnab/mixin) for the code, example and documentation. To keep it short, lets do an example of making a mixin and one example of using that mixin.
<br>A basic mixin that adds a string property to a class:
```
export class MixinString extends Mixin {
	myString: string;

	MixinString() {
		this.myString = "MyString";
	}

	MixinStringDestroy() {
		delete this.myString;
	}
}
```
<br>A class using MixinString to get a string property. The interface is created to get proper type support.
```
// Class using MixinString 
export class MyClass {
	constructor(){
		Mixin.mixinInits(this, MyClass, MixinString);
	}
  
	destroy(){
		this.mixinDestroys(MyClass);
	}
}
export interface MyClass extends MixinString { }
```
<br> The above is as basic as you can make it, to the point of making it pointless! To show some actual usage, here is a screenshot from the player class in [ARW](https://apps.spelmakare.se/arw).

![player example.png](https://files.peakd.com/file/peakd-hive/smjn/Eo1vWXrEztM83fvVPtMcA362njd4AdVtkTrYgRRGL8D25MncWMDC9pRazCbAW8k6ztD.png)

**MixinGameObjectBodies** gives a class unlimited rigidbodies and colliders, here it is used to give the player colliders that are used to check various contacts between the player and ground, walls and ceiling.

**MixinColliderCollector** gives a class the functionality to collect other objects of interest to do specific events. The objects of interested will be determined by what MixinInteractions they have. If other objects use MixinIntPlayer they will be able to interact with the player.

**MixinIntLadder** One of the MixinInteractions that the player have. This gives the player the ability to use ladders. By adding MixinIntLadder to other classes they too can use ladders.

As shown here, mixins are very powerful to easily create functionality that can be added to different classes that otherwise have nothing in common or any inheritance.

That's it! Thanks for reading.
<br>
___
<i> Spelmakare is game development using web technologies.

[Spelmakare.se](https://spelmakare.se)
[Discord](https://discord.gg/VG5EW9R)
[GitHub](https://github.com/smjnab)
[Play ARW](https://apps.spelmakare.se/arw)
[Play Hive P v. S](https://spelmakare.se/games/hivepvs)
</i>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 36 others
👎  
properties (23)
authorsmjn
permlinkarw-game-mixins-for-typescript
categoryhive-169321
json_metadata{"app":"peakd/2022.05.9","format":"markdown","tags":["gamedev","development","javascript","smjnblog","gaming"],"users":["smjn"],"image":["https://files.peakd.com/file/peakd-hive/smjn/23swbmk7Zck6on7C2qv34idGmgqXemLWsiVFeqWJghYdgx9bXNRNJF2rmBcDbGD57XqRe.png","https://files.peakd.com/file/peakd-hive/smjn/Eo1vWXrEztM83fvVPtMcA362njd4AdVtkTrYgRRGL8D25MncWMDC9pRazCbAW8k6ztD.png"]}
created2022-06-10 10:14:39
last_update2022-06-10 10:14:39
depth0
children0
last_payout2022-06-17 10:14:39
cashout_time1969-12-31 23:59:59
total_payout_value8.030 HBD
curator_payout_value8.175 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,430
author_reputation8,662,132,469,561
root_title"[ARW Game] Mixins for TypeScript"
beneficiaries
0.
accounthive-169321
weight200
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id113,936,607
net_rshares34,946,526,870,732
author_curate_reward""
vote details (101)