Changeset 7775 for 2016/27/ohjaajat


Ignore:
Timestamp:
2016-07-05 13:03:04 (3 years ago)
Author:
empaheik
Message:

Uusi versio morphista ja splitistä + hahmon Destroy uusiksi. Ja vähän muuta.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 2016/27/ohjaajat/VenienteFragore/VenienteFragore/VenienteFragore/VenienteFragore.cs

    r7742 r7775  
    4343    private readonly double crouchHeight; 
    4444 
     45    public List<Key> ControlKeys; 
     46 
    4547    public Player(double width, double height) : base(width, height) 
    4648    { 
    4749        fullHeight = height; 
    4850        crouchHeight = height / 2.0 - 1.0; 
     51    } 
     52 
     53    public void Reappear() 
     54    { 
     55        IsVisible = true; 
     56        IgnoresCollisionResponse = false; 
     57        IgnoresGravity = false; 
     58    } 
     59 
     60    public override void Destroy() 
     61    { 
     62        IsVisible = false; 
     63        IgnoresCollisionResponse = true; 
     64        IgnoresGravity = true; 
     65        Position = new Vector(Double.MinValue, Double.MinValue); 
    4966    } 
    5067 
     
    92109    public override void Begin() 
    93110    { 
     111 
     112 
    94113        objectMethods = new Dictionary<string, ObjectMethod>(); 
    95114        objectMethods.Add("ball", TestBall); 
     
    105124        Gravity = new Vector(0, -800); 
    106125 
    107         Camera.ZoomToAllObjects(); 
    108          
    109  
    110         Level.Background.Color = Color.DarkOrange; 
     126        Camera.ZoomToLevel(); 
     127 
     128 
     129        Level.Background.Color = Color.Beige; 
    111130 
    112131        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     
    133152    { 
    134153        CreatePlayer1(position, width, height); 
     154        var p = CreatePlayer2(position, width, height); 
     155        p.Destroy(); 
    135156        SetGeneralKeys(); 
    136157    } 
    137158 
    138  
    139     void CreatePlayer1(Vector position, double width, double height) 
    140     { 
    141         Player advenA = MakePlayer(position, width, height, 1); 
     159    Player CreatePlayer1(Vector position, double width, double height) 
     160    { 
     161        if (advenae.Count > 0 && !advenae[0].IsVisible) { advenae[0].Reappear(); return advenae[0]; }; 
     162 
     163        Player advenA = MakePlayer(position, width, height, 1, new List<Key> { Key.Up, Key.Down, Key.Left, Key.Right }); 
     164        advenA.Color = Color.HotPink; 
    142165        advenae.Add(advenA); 
    143         SetMovementKeys(advenA, Key.Up, Key.Down, Key.Left, Key.Right); 
    144     } 
    145  
    146  
    147     void CreatePlayer2(Vector position, double width, double height) 
    148     { 
    149         Player advenA = MakePlayer(position, width, height, 2); 
     166        return advenA; 
     167    } 
     168 
     169 
     170    Player CreatePlayer2(Vector position, double width, double height) 
     171    { 
     172        if (advenae.Count > 1 && !advenae[1].IsVisible) { advenae[1].Reappear(); return advenae[1]; }; 
     173 
     174        Player advenA = MakePlayer(position, width, height, 2, new List<Key> { Key.W, Key.S, Key.A, Key.D }); 
     175        advenA.Color = Color.Harlequin; 
    150176        advenae.Add(advenA); 
    151         SetMovementKeys(advenA, Key.W, Key.S, Key.A, Key.D); 
    152     } 
    153  
    154      
    155     private void SetMovementKeys(Player adven, Key up, Key down, Key left, Key right) 
    156     { 
    157         Keyboard.Listen(right, ButtonState.Down, Walk, null, adven, 200.0); 
    158         Keyboard.Listen(left, ButtonState.Down, Walk, null, adven, -200.0); 
    159         Keyboard.Listen(up, ButtonState.Down, Jump, null, adven); 
    160         Keyboard.Listen(down, ButtonState.Pressed, Crouch, null, adven, true); 
    161         Keyboard.Listen(down, ButtonState.Released, Crouch, null, adven, false); 
    162     } 
     177        return advenA; 
     178    } 
     179 
     180    private void SetMovementKeys(Player adven, List<Key> keys) 
     181    { 
     182        Keyboard.Listen(keys[3], ButtonState.Down, Walk, null, adven, 200.0); 
     183        Keyboard.Listen(keys[2], ButtonState.Down, Walk, null, adven, -200.0); 
     184        Keyboard.Listen(keys[0], ButtonState.Down, Jump, null, adven); 
     185        Keyboard.Listen(keys[1], ButtonState.Pressed, Crouch, null, adven, true); 
     186        Keyboard.Listen(keys[1], ButtonState.Released, Crouch, null, adven, false); 
     187 
     188        // Keyboard.Listen(keys[4], ButtonState.Pressed, SplitOrMorph, "Splits or morphs the player back to 1 player", adven); 
     189 
     190    } 
     191 
     192    /*  
     193     private void SetMovementKeys(Player adven, Key up, Key down, Key left, Key right, Key morph) 
     194     { 
     195         Keyboard.Listen(right, ButtonState.Down, Walk, null, adven, 200.0); 
     196         Keyboard.Listen(left, ButtonState.Down, Walk, null, adven, -200.0); 
     197         Keyboard.Listen(up, ButtonState.Down, Jump, null, adven); 
     198         Keyboard.Listen(down, ButtonState.Pressed, Crouch, null, adven, true); 
     199         Keyboard.Listen(down, ButtonState.Released, Crouch, null, adven, false); 
     200 
     201         Keyboard.Listen(morph, ButtonState.Pressed, SplitOrMorph, "Splits or morphs the player back to 1 player"); 
     202 
     203     } 
     204     */ 
    163205 
    164206    private void SetGeneralKeys() 
    165207    { 
    166         Keyboard.Listen(Key.R, ButtonState.Pressed, () => { ClearAll(); Begin(); }, "Lopeta peli"); 
     208        Keyboard.Listen(Key.R, ButtonState.Pressed, () => { ClearAll(); advenae = new List<Player>(); Begin(); }, "Lopeta peli"); 
    167209        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    168         Keyboard.Listen(Key.X, ButtonState.Pressed, SplitOrMorph, "Splits or morphs the player back to 1 player"); 
    169     } 
    170  
    171  
    172     private Player MakePlayer(Vector position, double width, double height, int playerNum) 
     210 
     211        //Keyboard.Listen(keys[4], ButtonState.Pressed, SplitOrMorph, "Splits or morphs the player back to 1 player", adven); 
     212        Keyboard.Listen(Key.X, ButtonState.Pressed, SplitOrMorph, "Splits or morphs the player", 2); 
     213        Keyboard.Listen(Key.RightShift, ButtonState.Pressed, SplitOrMorph, "Splits or morphs the player", 1); 
     214        Keyboard.Listen(Key.LeftShift, ButtonState.Pressed, () => { advenae[0].Reappear(); advenae[0].X = Level.Left - advenae[0].Width; }, ""); 
     215    } 
     216 
     217 
     218    private Player MakePlayer(Vector position, double width, double height, int playerNum, List<Key> keys) 
    173219    { 
    174220        Player adven = new Player(TileSize * 0.99, TileSize * 1.98); 
    175         adven.Position = position + new Vector(0, adven.Height); 
     221        adven.Position = position; 
    176222        adven.Friction = 0.2; 
    177223        adven.Tag = "player"; 
    178224        adven.Crushed += adven.Destroy; 
    179         adven.Destroyed += delegate { advenae.Remove(adven); }; 
    180         adven.CollisionIgnoreGroup = 1; 
     225        //adven.Destroyed += delegate { advenae.Remove(adven); }; 
     226        adven.CollisionIgnoreGroup = playerNum; 
    181227        adven.PlayerNumber = playerNum; 
    182         adven.AddCollisionHandler("player", HandleMorphing); 
     228        //adven.AddCollisionHandler("player", HandleMorphing); 
     229        adven.ControlKeys = keys; 
    183230        Add(adven); 
    184231 
     232        adven.AddCollisionHandler("death", (a, b) => adven.Destroy()); 
     233 
     234        SetMovementKeys(adven, keys); 
     235 
    185236        return adven; 
    186237    } 
    187238 
    188  
    189     private void SplitOrMorph() 
    190     { 
    191         if(advenae.Count == 1) 
    192         { 
    193             Split(advenae[0]); 
     239    /// <summary> 
     240    /// Sotku 
     241    /// </summary> 
     242    /// <param name="playerNro">Sen pelaajan numero, joka haluaa toimia</param> 
     243    private void SplitOrMorph(int playerNro) 
     244    { 
     245        //if (advenae.FindAll(o => o.IsVisible == true).Count == 1) 
     246        if(!advenae[playerNro-1].IsVisible && advenae[1 - playerNro + 1].IsVisible) 
     247        { 
     248            Split(advenae[1 - playerNro + 1]); 
    194249        } 
    195250        else 
    196251        { 
    197             Morph(); 
    198         } 
    199     } 
    200  
    201     //Would be nice to have this hidden or just 1 time collision handlers 
    202     bool morphing = false; 
    203  
    204     private void Morph() 
    205     { 
    206         if (advenae.Count < 2) return; 
    207  
     252            Morph(advenae[1 - playerNro + 1]); 
     253        } 
     254    } 
     255 
     256    private void Morph(Player adven) 
     257    { 
     258        if (advenae.FindAll(o => o.IsVisible == true).Count != 2) return; 
     259 
     260        if (Vector.Distance(advenae[0].Position, advenae[1].Position) < adven.Width * 1.5) 
     261        { 
     262            Player destroyable = advenae.Find(o => o.PlayerNumber != adven.PlayerNumber); 
     263 
     264            /*destroyable.ControlKeys.ForEach(delegate (Key key) 
     265            { 
     266                Keyboard.Disable(key); 
     267            });*/ 
     268 
     269            destroyable.Destroy(); 
     270        } 
     271 
     272        //TODO: Eeppinen hypäytys takaisin? Samalla voisi ajastimella tarkastaa jatkuvasti, ovatko pelaajat tarpeeksi lähellä toisiaan. 
     273        /* 
    208274        Vector center = new Vector(); 
    209275 
     
    218284        morphing = true; 
    219285        Timer.SingleShot(2.0, delegate { morphing = false; }); 
    220     } 
    221  
    222     void HandleMorphing(SimplePhysics.Object player, SimplePhysics.Object player2) 
    223     { 
    224         if(!morphing) return; 
    225         Player first = advenae[0]; 
    226         for (int i = 1; i < advenae.Count; i++) 
    227         { 
    228             advenae[i].Destroy(); 
    229         } 
    230        // advenae.Clear(); 
    231        // advenae.Add(first); //So no hassle with i-- 
    232         ClearControls(); 
    233         //Way to not redo this nonstop 
    234         SetMovementKeys(first, Key.Up, Key.Down, Key.Left, Key.Right); 
    235         SetGeneralKeys(); 
    236         morphing = false; 
     286        */ 
    237287    } 
    238288 
    239289    void Split(Player adven) 
    240290    { 
    241         //Maybe remove copy pate later 
    242         int missingPlayer = 1; 
    243         foreach (Player item in advenae) 
    244         { 
    245             if (item.PlayerNumber == missingPlayer) missingPlayer++; 
    246         } 
    247         switch (missingPlayer) 
     291        if (advenae.FindAll(o => o.IsVisible == true).Count == 2) return; 
     292 
     293        Player advenNew = new Player(adven.Width, adven.Height); 
     294        switch (adven.PlayerNumber) 
    248295        { 
    249296            case 1: 
    250                 CreatePlayer1(adven.Position + new Vector(adven.Width * 2, 0), adven.Width, adven.Height); 
     297                advenNew = CreatePlayer2(adven.Position + new Vector(adven.Width, 0), adven.Width, adven.Height); 
    251298                break; 
    252299            case 2: 
    253                 CreatePlayer2(adven.Position + new Vector(adven.Width * 2, 0), adven.Width, adven.Height); 
     300                advenNew = CreatePlayer1(adven.Position + new Vector(adven.Width, 0), adven.Width, adven.Height); 
    254301                break; 
    255302            default: 
    256303                break; 
    257304        } 
    258     } 
    259  
     305 
     306        advenNew.Position = adven.Position + new Vector(adven.Width, 0); 
     307 
     308        if (Collisions(advenNew, Vector.Zero, true).Count > 0) 
     309        { 
     310            advenNew.Destroy(); 
     311            return; 
     312        } 
     313 
     314        advenNew.ControlKeys.ForEach(delegate (Key key) 
     315        { 
     316            Keyboard.Enable(key); 
     317        }); 
     318    } 
    260319 
    261320    void Walk(Player pelaaja, double nopeus) 
Note: See TracChangeset for help on using the changeset viewer.