@@ -52,29 +52,19 @@ Game.EntityMixins.Destructible = {
52
52
} ,
53
53
takeDamage : function ( attacker , damage ) {
54
54
this . _hp -= damage ;
55
- // If have 0 or less HP, then remove ourselves from the map
55
+ // If have 0 or less HP, then remove ourseles from the map
56
56
if ( this . _hp <= 0 ) {
57
57
Game . sendMessage ( attacker , 'You kill the %s!' , [ this . getName ( ) ] ) ;
58
- // If the entity is a corpse dropper, try to add a corpse
59
- if ( this . hasMixin ( Game . EntityMixins . CorpseDropper ) ) {
60
- this . tryDropCorpse ( ) ;
61
- }
58
+ // Raise events
59
+ this . raiseEvent ( 'onDeath' , attacker ) ;
60
+ attacker . raiseEvent ( 'onKill' , this ) ;
62
61
this . kill ( ) ;
63
- // Give the attacker experience points.
64
- if ( attacker . hasMixin ( 'ExperienceGainer' ) ) {
65
- var exp = this . getMaxHp ( ) + this . getDefenseValue ( ) ;
66
- if ( this . hasMixin ( 'Attacker' ) ) {
67
- exp += this . getAttackValue ( ) ;
68
- }
69
- // Account for level differences
70
- if ( this . hasMixin ( 'ExperienceGainer' ) ) {
71
- exp -= ( attacker . getLevel ( ) - this . getLevel ( ) ) * 3 ;
72
- }
73
- // Only give experience if more than 0.
74
- if ( exp > 0 ) {
75
- attacker . giveExperience ( exp ) ;
76
- }
77
- }
62
+ }
63
+ } ,
64
+ listeners : {
65
+ onGainLevel : function ( ) {
66
+ // Heal the entity.
67
+ this . setHp ( this . getMaxHp ( ) ) ;
78
68
}
79
69
}
80
70
}
@@ -379,14 +369,17 @@ Game.EntityMixins.CorpseDropper = {
379
369
// Chance of dropping a cropse (out of 100).
380
370
this . _corpseDropRate = template [ 'corpseDropRate' ] || 100 ;
381
371
} ,
382
- tryDropCorpse : function ( ) {
383
- if ( Math . round ( Math . random ( ) * 100 ) < this . _corpseDropRate ) {
384
- // Create a new corpse item and drop it.
385
- this . _map . addItem ( this . getX ( ) , this . getY ( ) , this . getZ ( ) ,
386
- Game . ItemRepository . create ( 'corpse' , {
387
- name : this . _name + ' corpse' ,
388
- foreground : this . _foreground
389
- } ) ) ;
372
+ listeners : {
373
+ onDeath : function ( attacker ) {
374
+ // Check if we should drop a corpse.
375
+ if ( Math . round ( Math . random ( ) * 100 ) <= this . _corpseDropRate ) {
376
+ // Create a new corpse item and drop it.
377
+ this . _map . addItem ( this . getX ( ) , this . getY ( ) , this . getZ ( ) ,
378
+ Game . ItemRepository . create ( 'corpse' , {
379
+ name : this . _name + ' corpse' ,
380
+ foreground : this . _foreground
381
+ } ) ) ;
382
+ }
390
383
}
391
384
}
392
385
} ;
@@ -561,12 +554,22 @@ Game.EntityMixins.ExperienceGainer = {
561
554
// Check if we gained at least one level.
562
555
if ( levelsGained > 0 ) {
563
556
Game . sendMessage ( this , "You advance to level %d." , [ this . _level ] ) ;
564
- // Heal the entity if possible.
565
- if ( this . hasMixin ( 'Destructible' ) ) {
566
- this . setHp ( this . getMaxHp ( ) ) ;
557
+ this . raiseEvent ( 'onGainLevel' ) ;
558
+ }
559
+ } ,
560
+ listeners : {
561
+ onKill : function ( victim ) {
562
+ var exp = victim . getMaxHp ( ) + victim . getDefenseValue ( ) ;
563
+ if ( victim . hasMixin ( 'Attacker' ) ) {
564
+ exp += victim . getAttackValue ( ) ;
567
565
}
568
- if ( this . hasMixin ( 'StatGainer' ) ) {
569
- this . onGainLevel ( ) ;
566
+ // Account for level differences
567
+ if ( victim . hasMixin ( 'ExperienceGainer' ) ) {
568
+ exp -= ( this . getLevel ( ) - victim . getLevel ( ) ) * 3 ;
569
+ }
570
+ // Only give experience if more than 0.
571
+ if ( exp > 0 ) {
572
+ this . giveExperience ( exp ) ;
570
573
}
571
574
}
572
575
}
@@ -575,24 +578,83 @@ Game.EntityMixins.ExperienceGainer = {
575
578
Game . EntityMixins . RandomStatGainer = {
576
579
name : 'RandomStatGainer' ,
577
580
groupName : 'StatGainer' ,
578
- onGainLevel : function ( ) {
579
- var statOptions = this . getStatOptions ( ) ;
580
- // Randomly select a stat option and execute the callback for each
581
- // stat point.
582
- while ( this . getStatPoints ( ) > 0 ) {
583
- // Call the stat increasing function with this as the context.
584
- statOptions . random ( ) [ 1 ] . call ( this ) ;
585
- this . setStatPoints ( this . getStatPoints ( ) - 1 ) ;
581
+ listeners : {
582
+ onGainLevel : function ( ) {
583
+ var statOptions = this . getStatOptions ( ) ;
584
+ // Randomly select a stat option and execute the callback for each
585
+ // stat point.
586
+ while ( this . getStatPoints ( ) > 0 ) {
587
+ // Call the stat increasing function with this as the context.
588
+ statOptions . random ( ) [ 1 ] . call ( this ) ;
589
+ this . setStatPoints ( this . getStatPoints ( ) - 1 ) ;
590
+ }
586
591
}
587
592
}
588
593
} ;
589
594
590
595
Game . EntityMixins . PlayerStatGainer = {
591
596
name : 'PlayerStatGainer' ,
592
597
groupName : 'StatGainer' ,
593
- onGainLevel : function ( ) {
594
- // Setup the gain stat screen and show it.
595
- Game . Screen . gainStatScreen . setup ( this ) ;
596
- Game . Screen . playScreen . setSubScreen ( Game . Screen . gainStatScreen ) ;
598
+ listeners : {
599
+ onGainLevel : function ( ) {
600
+ // Setup the gain stat screen and show it.
601
+ Game . Screen . gainStatScreen . setup ( this ) ;
602
+ Game . Screen . playScreen . setSubScreen ( Game . Screen . gainStatScreen ) ;
603
+ }
597
604
}
598
605
} ;
606
+
607
+ Game . EntityMixins . GiantZombieActor = Game . extend ( Game . EntityMixins . TaskActor , {
608
+ init : function ( template ) {
609
+ // Call the task actor init with the right tasks.
610
+ Game . EntityMixins . TaskActor . init . call ( this , Game . extend ( template , {
611
+ 'tasks' : [ 'growArm' , 'spawnSlime' , 'hunt' , 'wander' ]
612
+ } ) ) ;
613
+ // We only want to grow the arm once.
614
+ this . _hasGrownArm = false ;
615
+ } ,
616
+ canDoTask : function ( task ) {
617
+ // If we haven't already grown arm and HP <= 20, then we can grow.
618
+ if ( task === 'growArm' ) {
619
+ return this . getHp ( ) <= 20 && ! this . _hasGrownArm ;
620
+ // Spawn a slime only a 10% of turns.
621
+ } else if ( task === 'spawnSlime' ) {
622
+ return Math . round ( Math . random ( ) * 100 ) <= 10 ;
623
+ // Call parent canDoTask
624
+ } else {
625
+ return Game . EntityMixins . TaskActor . canDoTask . call ( this , task ) ;
626
+ }
627
+ } ,
628
+ growArm : function ( ) {
629
+ this . _hasGrownArm = true ;
630
+ this . increaseAttackValue ( 5 ) ;
631
+ // Send a message saying the zombie grew an arm.
632
+ Game . sendMessageNearby ( this . getMap ( ) ,
633
+ this . getX ( ) , this . getY ( ) , this . getZ ( ) ,
634
+ 'An extra arm appears on the giant zombie!' ) ;
635
+ } ,
636
+ spawnSlime : function ( ) {
637
+ // Generate a random position nearby.
638
+ var xOffset = Math . floor ( Math . random ( ) * 3 ) - 1 ;
639
+ var yOffset = Math . floor ( Math . random ( ) * 3 ) - 1 ;
640
+
641
+ // Check if we can spawn an entity at that position.
642
+ if ( ! this . getMap ( ) . isEmptyFloor ( this . getX ( ) + xOffset , this . getY ( ) + yOffset ,
643
+ this . getZ ( ) ) ) {
644
+ // If we cant, do nothing
645
+ return ;
646
+ }
647
+ // Create the entity
648
+ var slime = Game . EntityRepository . create ( 'slime' ) ;
649
+ slime . setX ( this . getX ( ) + xOffset ) ;
650
+ slime . setY ( this . getY ( ) + yOffset )
651
+ slime . setZ ( this . getZ ( ) ) ;
652
+ this . getMap ( ) . addEntity ( slime ) ;
653
+ } ,
654
+ listeners : {
655
+ onDeath : function ( attacker ) {
656
+ // Switch to win screen when killed!
657
+ Game . switchScreen ( Game . Screen . winScreen ) ;
658
+ }
659
+ }
660
+ } ) ;
0 commit comments