Changeset 7806 for 2016/27/ohjaajat


Ignore:
Timestamp:
2016-07-05 16:56:16 (3 years ago)
Author:
tesatapa
Message:

TimerAddonsiin? Until metodi, ja kiinniotto ja heittely

File:
1 edited

Legend:

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

    r7775 r7806  
    9191        } 
    9292 
     93        //Tämä tänne ehkä? 
     94        if (Carrying != null) 
     95        { 
     96            Carrying.X = this.X; 
     97            Carrying.Bottom = this.Top; 
     98        } 
     99 
    93100        base.Update(time); 
     101    } 
     102} 
     103 
     104public static class TimerAddons 
     105{ 
     106    public static void Until(Func<bool> predicate, Action action, double tickInterval = 0.1, Action onEnd = null) 
     107    { 
     108        Timer timer = new Timer(); 
     109        timer.Interval = tickInterval; 
     110        timer.Timeout += delegate 
     111        { 
     112            if (predicate()) 
     113            { 
     114                timer.Stop(); 
     115                //Wut? this actually exists now in C#.. looks ugly as hell but is shorther than: 
     116                //if(onEnd != null) onEnd(); 
     117                if (onEnd != null) 
     118                    onEnd.Invoke(); 
     119            } 
     120            else 
     121            { 
     122                action(); 
     123                //or action.Invoke(); 
     124            } 
     125        }; 
     126        timer.Start(); 
     127 
    94128    } 
    95129} 
     
    161195        if (advenae.Count > 0 && !advenae[0].IsVisible) { advenae[0].Reappear(); return advenae[0]; }; 
    162196 
    163         Player advenA = MakePlayer(position, width, height, 1, new List<Key> { Key.Up, Key.Down, Key.Left, Key.Right }); 
     197        Player advenA = MakePlayer(position, width, height, 1, new List<Key> { Key.Up, Key.Down, Key.Left, Key.Right, Key.Enter }); 
    164198        advenA.Color = Color.HotPink; 
    165199        advenae.Add(advenA); 
     
    172206        if (advenae.Count > 1 && !advenae[1].IsVisible) { advenae[1].Reappear(); return advenae[1]; }; 
    173207 
    174         Player advenA = MakePlayer(position, width, height, 2, new List<Key> { Key.W, Key.S, Key.A, Key.D }); 
     208        Player advenA = MakePlayer(position, width, height, 2, new List<Key> { Key.W, Key.S, Key.A, Key.D, Key.E }); 
    175209        advenA.Color = Color.Harlequin; 
    176210        advenae.Add(advenA); 
     
    185219        Keyboard.Listen(keys[1], ButtonState.Pressed, Crouch, null, adven, true); 
    186220        Keyboard.Listen(keys[1], ButtonState.Released, Crouch, null, adven, false); 
    187  
     221        Keyboard.Listen(keys[4], ButtonState.Pressed, Interact, null, adven); 
    188222        // Keyboard.Listen(keys[4], ButtonState.Pressed, SplitOrMorph, "Splits or morphs the player back to 1 player", adven); 
    189223 
    190224    } 
     225 
     226    private void Interact(Player adven) 
     227    { 
     228        //Jos kannetaan jotain, viskataan se 
     229        SimplePhysics.Object carryed = adven.Carrying;  
     230        if( carryed != null) //Throw 
     231        { 
     232            adven.Carrying = null; 
     233            double dir = adven.FacingDirection == Direction.Left ? -1 : 1; 
     234            carryed.Velocity = new Vector(dir * 350, 350); 
     235            carryed.Position = carryed.Position + new Vector(dir * 10, 30); 
     236            //Pitäisiköhän oma aliohjelma tehdä 
     237            carryed.IgnoresGravity = false; 
     238            carryed.IgnoresCollisionResponse = false; 
     239        } 
     240 
     241        Vector pingPoint = adven.Position + (adven.FacingDirection == Direction.Left ? new Vector(-adven.Width, 0) : new Vector(adven.Width, 0)); 
     242        List<GameObject> interactedObjects = GetObjectsAt(pingPoint); 
     243 
     244        GameObject sphere = new GameObject(1, 1); 
     245        sphere.Position = pingPoint; 
     246        sphere.Shape = Shape.Circle; 
     247        sphere.Color = new Color(100, 0, 100, 25); 
     248        Add(sphere, 1); 
     249 
     250        TimerAddons.Until(() => sphere.Width == 100.0, delegate { sphere.Width += 1; sphere.Height += 1; }, 0.01, sphere.Destroy); 
     251 
     252        foreach (GameObject item in interactedObjects) 
     253        { 
     254            SimplePhysics.Object obj = item as SimplePhysics.Object; 
     255            if (obj == null) 
     256                continue; 
     257            InteractWith(adven, obj); 
     258        } 
     259    } 
     260 
     261 
     262    private void InteractWith(Player adven, SimplePhysics.Object interactedWith) 
     263    { 
     264        if(adven.Carrying == null) 
     265        { 
     266            adven.Carrying = interactedWith; 
     267            interactedWith.IgnoresGravity = true; 
     268            interactedWith.IgnoresCollisionResponse = true; 
     269            //Lentävän palikan nostaminen  
     270        } 
     271    } 
     272 
     273 
     274 
    191275 
    192276    /*  
Note: See TracChangeset for help on using the changeset viewer.