olistic / warriorjs

🏰 An exciting game of programming and Artificial Intelligence
https://warriorjs.com
MIT License
9.43k stars 490 forks source link

Defining unit detection on units themselves #96

Closed maradotwebp closed 4 years ago

maradotwebp commented 6 years ago

In the current state of the project, there are two methods in the Space API to check the unit type:

According to Issue #61, these will soon be altered:

However, it would not only be nice to check if a unit is friendly or not, but also the type of the specific unit. The possibility to check the type will allow distinction between multiple friendly units (which are needed if different actions could be performed on different unit types).

An Example on how to implement this would be to specify the name of the detection function directly in the unit definition:

//warrior-tower-community/units/Shopkeeper.js
const Shopkeeper = {
  name: 'Shopkeeper',
  character: '$',
  maxHealth: 7,
  friendly: true,
  detectionFunction: 'isShopkeeper' // the name of the exposed function
}
export default Shopkeeper;

Warriorjs should then automatically expose the method unit.isTypeShopkeeper() (based on the value of detectionFunction) in their API.

Another possibility would be to automatically get the name of the exposed method based on the name property of the unit object:

//warrior-tower-community/units/Dragon.js
const Dragon = {
  name: 'Town Dragon',
  character: '§',
  maxHealth: 100,
  friendly: true,
  canBeTypeChecked: true // allows type checking on this entity
}
export default Dragon;

Warriorjs should then automatically expose the method unit.isTypeTownDragon() (build from the name property) in their API.

With this principle, the tower creators themselves would be able to give players the possibility to check if an unit is a specific unit type or not, because players may want to take different actions if a unit is a friendly shopkeeper, a friendly captive or another friendly unit.