<commit_before>/**
 Copyright (C) 2013 by <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

/// <reference path="../gameCombatState.ts" />
/// <reference path="../../components/damageComponent.ts" />
/// <reference path="../../../core/state.ts" />

module pow2 {

   // Combat Begin
   //--------------------------------------------------------------------------
   export class CombatBeginTurnState extends CombatState {
      static NAME:string = "Combat Begin Turn";
      name:string = CombatBeginTurnState.NAME;
      transitions:IStateTransition[] = [
         new CombatEndTurnTransition()
      ];
      enter(machine:CombatStateMachine){
         super.enter(machine);
         machine.currentDone = false;
         if(!machine.isFriendlyTurn()){
            this.attack(machine);
         }
      }
      keyPress(machine:CombatStateMachine,keyCode:KeyCode):boolean {
         if(!machine.isFriendlyTurn()){
            return true;
         }
         switch(keyCode){
            case KeyCode.ENTER:
               this.attack(machine);
               break;
            default:
               return super.keyPress(machine,keyCode);
         }
         return false;
      }

      attack(machine:CombatStateMachine){
         //
         var attacker:GameEntityObject = null;
         var defender:GameEntityObject = null;
         if(machine.current.id === machine.friendly.id){
            attacker = machine.friendly;
            defender = machine.enemy;
         }
         else {
            attacker = machine.enemy;
            defender = machine.friendly;
         }
         _.delay(() => {
            var defName:string = defender.model.get('name');
            var attName:string = attacker.model.get('name');
            var damage:number = attacker.model.attack(defender.model);
//            console.log(attName + " attacked " + defName + " for (" + damage + ") damage");
//            console.log(defName + " has (" + defender.model.get('hp') + ") hit points left");
            var hit:boolean = damage > 0;
            var components = {
               animation: new pow2.AnimatedSpriteComponent("attack"),
               sprite: new pow2.SpriteComponent({
                  name:"attack",
                  icon: hit ? "animHit.png" : "animMiss.png"
               }),
               damage: new pow2.DamageComponent(),
               sound: new pow2.SoundComponent({
                  url: hit ? "/data/sounds/hit" : "/data/sounds/miss"
               })

            };
            defender.addComponentDictionary(components);<commit_msg>Add deathblow sound for when an enemy is killed.<commit_after>/**
 Copyright (C) 2013 by <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

/// <reference path="../gameCombatState.ts" />
/// <reference path="../../components/damageComponent.ts" />
/// <reference path="../../../core/state.ts" />

module pow2 {

   // Combat Begin
   //--------------------------------------------------------------------------
   export class CombatBeginTurnState extends CombatState {
      static NAME:string = "Combat Begin Turn";
      name:string = CombatBeginTurnState.NAME;
      transitions:IStateTransition[] = [
         new CombatEndTurnTransition()
      ];
      enter(machine:CombatStateMachine){
         super.enter(machine);
         machine.currentDone = false;
         if(!machine.isFriendlyTurn()){
            this.attack(machine);
         }
      }
      keyPress(machine:CombatStateMachine,keyCode:KeyCode):boolean {
         if(!machine.isFriendlyTurn()){
            return true;
         }
         switch(keyCode){
            case KeyCode.ENTER:
               this.attack(machine);
               break;
            default:
               return super.keyPress(machine,keyCode);
         }
         return false;
      }

      attack(machine:CombatStateMachine){
         //
         var attacker:GameEntityObject = null;
         var defender:GameEntityObject = null;
         if(machine.current.id === machine.friendly.id){
            attacker = machine.friendly;
            defender = machine.enemy;
         }
         else {
            attacker = machine.enemy;
            defender = machine.friendly;
         }
         _.delay(() => {
            var damage:number = attacker.model.attack(defender.model);
            var didKill:boolean = defender.model.get('hp') <= 0;

//            console.log(attName + " attacked " + defName + " for (" + damage + ") damage");
//            console.log(defName + " has (" + defender.model.get('hp') + ") hit points left");
            var hit:boolean = damage > 0;
            var hitSound:string = "/data/sounds/" + (didKill ? "killed" : (hit ? "hit" : "miss"));
            var components = {
               animation: new pow2.AnimatedSpriteComponent("attack"),
               sprite: new pow2.SpriteComponent({
                  name:"attack",
                  icon: hit ? "animHit.png" : "animMiss.png"
               }),
               damage: new pow2.DamageComponent(),
               sound: new pow2.SoundComponent({
                  url: hitSound
               })

            };
            defender.addComponentDictionary(components);
{'fraction_non_alphanumeric': 0.13552461139896374, 'fraction_numerical': 0.00469559585492228, 'mean_word_length': 1.8570767807585569, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3948328', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1518, 'n_words': 405}