Gestion de personnage et de pool de personnage en javascript vanilla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

l5r.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. var stances = ["attack", "full attack", "defense", "full defense", "center"];
  2. const mechanics = new Array();
  3. // [ school name ][ mechanic name ][ action ][ function ]
  4. mechanics[ "œil de l'aigle" ] = { name : "kiho",
  5. action : ()=> {
  6. console.log("En restant immobile et concentré, læ moine peut peut voir à un nombre de milles égal à son rang d'école, avec assez de précision pour reconnaître des individus, mais pas assez pour lire.");
  7. }
  8. };
  9. mechanics[ "moine hitomi" ] = { name : "don de la dame",
  10. mutator : ( charac ) => {
  11. charac['getTN'] = () => { return charac.defaultGetTN() + charac.reflexes;}
  12. addTatoo( charac );
  13. }
  14. };
  15. mechanics[ "moine hitomi" ] = { name : "frapper la base",
  16. mutator : ( charac ) => {
  17. charac['getTN'] = () => { return charac.defaultGetTN() + charac.reflexes;}
  18. }
  19. };
  20. mechanics[ "moine hitomi" ] = { name : "déplacer le vent",
  21. mutator : ( charac ) => {
  22. charac[ 'actions' ][ 'unarmed attack' ].actionType = 'simple';
  23. charac.chooseTatoo();
  24. }
  25. };
  26. class Characters {
  27. constructor(){};
  28. pool = [];
  29. elements = ["Character", "Samourai", "People"];
  30. at( index ){
  31. return this.pool[ index ];
  32. }
  33. elementsType = Character;
  34. create( json ){
  35. let charac = new Character();
  36. if( typeof json === "Object" )
  37. charac.fromJson( json ) ;
  38. this.add( charac );
  39. }
  40. createCharacter( aRole, gender, name, familly,experience, gempukku, skills ){
  41. let character = new Character( aRole, gender, name, familly, school, experience, gempukku, skills );
  42. this.add( character );
  43. }
  44. createSamourai( aRole, gender, name, familly, school, experience, gempukku, skills ){
  45. let samourai = new Samourai( aRole, gender, name, familly, school, experience, gempukku, skills );
  46. this.add( samourai );
  47. }
  48. createPeople( gender, name, familly, school, experience, skills ){
  49. let people = new People( gender, name, familly, school, experience, skills );
  50. this.add( people );
  51. }
  52. fromJson( json ){
  53. let data = JSON.parse(json);
  54. for( prop in data){
  55. this[ prop ] = data[ prop ];
  56. }
  57. }
  58. /** add
  59. * character, parameter, must be declare in the characters.elemets.list
  60. * return boolean
  61. **/
  62. add(character){
  63. if( this.elements.includes( character.constructor.name )){
  64. this.pool.push( character );
  65. return true;
  66. }else{
  67. return false;
  68. }
  69. }
  70. indexOf( character ){
  71. if( this.elements.includes( character.constructor.name )){
  72. for(let i = 0; i < this.pool.length ; i++){
  73. if( character == this.pool[ i ] )
  74. return i;
  75. }
  76. }
  77. }
  78. getByName( name ){
  79. for(let i = 0; i < this.pool.length ; i++){
  80. if( this.pool[i].getName() == name)
  81. return this.pool[i];
  82. }
  83. }
  84. getIndexByName( name ){
  85. for(let i = 0; i < this.pool.length ; i++){
  86. if( this.pool[i].getName() == name)
  87. return i;
  88. }
  89. }
  90. size(){
  91. return this.pool.length;
  92. }
  93. }
  94. var RNG = new RandomGenerator(4);
  95. function characterFromJSON( json ){
  96. let result = new Character();
  97. let data = JSON.parse(json);
  98. for( prop in data){
  99. result[ prop ] = data[ prop ];
  100. }
  101. return result;
  102. }
  103. function fight(charac1, charac2){
  104. let result = {
  105. "attacker" : charac1.initiative(),
  106. "defender" : charac2.initiative()
  107. };
  108. let first = ( charac1.initiative() + (charac1.water/10) > charac1.initiative() + (charac1.water()/10))? charac1 : charac2 ;
  109. let second = ( charac1 == first)? charac2 : charac1 ;
  110. let winner = 0
  111. let round = 0;
  112. while( !winner ){
  113. round++;
  114. result[round] = {
  115. "attack" : {
  116. "attacker" : first.attack(),
  117. "defender" : second.attack()
  118. },
  119. "damage" : {
  120. "attacker" : first.damage(),
  121. "defender" : second.damage()
  122. }
  123. };
  124. result[round]["wounds"] = {
  125. "attacker" : ( result[round]['attack']['attacker'] >= second.getTN() )? second.getWounded( result[round]['damage']['attacker'] ) : 0,
  126. "defender" : (( result[round]['attack']['defender'] >= first.getTN() ) && second.malus < 100)? first.getWounded( result[round]['damage']['defender'] ) : 0,
  127. }
  128. if( second.malus > 99 ){
  129. winner = first;
  130. } else if( first.malus > 99 )
  131. winner = second;
  132. }
  133. result['winner'] = winner ;
  134. result['winner_side'] = (winner == charac1)? "attacker": "defender";
  135. return result;
  136. }