Changeset 7740 for 2016/27


Ignore:
Timestamp:
2016-07-04 23:14:14 (3 years ago)
Author:
tesatapa
Message:

Made the splitting of 1 char into 2 and back again into 2.
Added dozen complementary methods in between to make it work for creating players x and y and so on

File:
1 edited

Legend:

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

    r7736 r7740  
    3737 
    3838    public bool Crouch { get; set; } 
    39  
     39    public int PlayerNumber { get; set; } 
    4040    public SimplePhysics.Object Carrying { get; set; } 
    4141 
     
    8484    private Dictionary<string, ObjectMethod> objectMethods; 
    8585 
    86     Player advenA; 
     86    private List<Player> advenae = new List<Player>(); 
    8787 
    8888    public VenienteFragore() : base(tileSize: 50) 
     
    131131    void CreatePlayer(Vector position, double width, double height, Dictionary<string, string> properties) 
    132132    { 
    133         advenA = new Player(TileSize * 0.99, TileSize * 1.98); 
    134         advenA.Position = position + new Vector(0, advenA.Height); 
    135         advenA.Friction = 0.2; 
    136         advenA.Tag = "player"; 
    137         advenA.Crushed += advenA.Destroy; 
    138         advenA.CollisionIgnoreGroup = 1; 
    139         Add(advenA); 
    140  
    141         Keyboard.Listen(Key.Right, ButtonState.Down, Walk, null, advenA, 200.0); 
    142         Keyboard.Listen(Key.Left, ButtonState.Down, Walk, null, advenA, -200.0); 
    143         Keyboard.Listen(Key.Up, ButtonState.Down, Jump, null, advenA); 
    144         Keyboard.Listen(Key.Down, ButtonState.Pressed, Crouch, null, advenA, true); 
    145         Keyboard.Listen(Key.Down, ButtonState.Released, Crouch, null, advenA, false); 
     133        CreatePlayer1(position, width, height); 
     134        SetGeneralKeys(); 
     135    } 
     136 
     137 
     138    void CreatePlayer1(Vector position, double width, double height) 
     139    { 
     140        Player advenA = MakePlayer(position, width, height, 1); 
     141        advenae.Add(advenA); 
     142        SetMovementKeys(advenA, Key.Up, Key.Down, Key.Left, Key.Right); 
     143    } 
     144 
     145 
     146    void CreatePlayer2(Vector position, double width, double height) 
     147    { 
     148        Player advenA = MakePlayer(position, width, height, 2); 
     149        advenae.Add(advenA); 
     150        SetMovementKeys(advenA, Key.W, Key.S, Key.A, Key.D); 
     151    } 
     152 
     153     
     154    private void SetMovementKeys(Player adven, Key up, Key down, Key left, Key right) 
     155    { 
     156        Keyboard.Listen(right, ButtonState.Down, Walk, null, adven, 200.0); 
     157        Keyboard.Listen(left, ButtonState.Down, Walk, null, adven, -200.0); 
     158        Keyboard.Listen(up, ButtonState.Down, Jump, null, adven); 
     159        Keyboard.Listen(down, ButtonState.Pressed, Crouch, null, adven, true); 
     160        Keyboard.Listen(down, ButtonState.Released, Crouch, null, adven, false); 
     161    } 
     162 
     163    private void SetGeneralKeys() 
     164    { 
    146165        Keyboard.Listen(Key.R, ButtonState.Pressed, () => { ClearAll(); Begin(); }, "Lopeta peli"); 
    147166        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    148     } 
     167        Keyboard.Listen(Key.X, ButtonState.Pressed, SplitOrMorph, "Splits or morphs the player back to 1 player"); 
     168    } 
     169 
     170 
     171    private Player MakePlayer(Vector position, double width, double height, int playerNum) 
     172    { 
     173        Player adven = new Player(TileSize * 0.99, TileSize * 1.98); 
     174        adven.Position = position + new Vector(0, adven.Height); 
     175        adven.Friction = 0.2; 
     176        adven.Tag = "player"; 
     177        adven.Crushed += adven.Destroy; 
     178        adven.Destroyed += delegate { advenae.Remove(adven); }; 
     179        adven.CollisionIgnoreGroup = playerNum; 
     180        adven.PlayerNumber = playerNum; 
     181        adven.AddCollisionHandler("player", HandleMorphing); 
     182        Add(adven); 
     183 
     184        return adven; 
     185    } 
     186 
     187 
     188    private void SplitOrMorph() 
     189    { 
     190        if(advenae.Count == 1) 
     191        { 
     192            Split(advenae[0]); 
     193        } 
     194        else 
     195        { 
     196            Morph(); 
     197        } 
     198    } 
     199 
     200    //Would be nice to have this hidden or just 1 time collision handlers 
     201    bool morphing = false; 
     202 
     203    private void Morph() 
     204    { 
     205        if (advenae.Count < 2) return; 
     206 
     207        Vector center = new Vector(); 
     208 
     209        for (int i = 0; i < advenae.Count; i++) 
     210        { 
     211            center = (advenae[i].Position - center) * 0.5; 
     212        } 
     213        foreach (Player item in advenae) 
     214        { 
     215            item.Velocity = (center - item.Position) * 3.0; 
     216        } 
     217        morphing = true; 
     218        Timer.SingleShot(2.0, delegate { morphing = false; }); 
     219    } 
     220 
     221    void HandleMorphing(SimplePhysics.Object player, SimplePhysics.Object player2) 
     222    { 
     223        if(!morphing) return; 
     224        Player first = advenae[0]; 
     225        for (int i = 1; i < advenae.Count; i++) 
     226        { 
     227            advenae[i].Destroy(); 
     228        } 
     229       // advenae.Clear(); 
     230       // advenae.Add(first); //So no hassle with i-- 
     231        ClearControls(); 
     232        //Way to not redo this nonstop 
     233        SetMovementKeys(first, Key.Up, Key.Down, Key.Left, Key.Right); 
     234        SetGeneralKeys(); 
     235        morphing = false; 
     236    } 
     237 
     238    void Split(Player adven) 
     239    { 
     240        //Maybe remove copy pate later 
     241        int missingPlayer = 1; 
     242        foreach (Player item in advenae) 
     243        { 
     244            if (item.PlayerNumber == missingPlayer) missingPlayer++; 
     245        } 
     246        switch (missingPlayer) 
     247        { 
     248            case 1: 
     249                CreatePlayer1(adven.Position + new Vector(adven.Width * 2, 0), adven.Width, adven.Height); 
     250                break; 
     251            case 2: 
     252                CreatePlayer2(adven.Position + new Vector(adven.Width * 2, 0), adven.Width, adven.Height); 
     253                break; 
     254            default: 
     255                break; 
     256        } 
     257    } 
     258 
    149259 
    150260    void Walk(Player pelaaja, double nopeus) 
Note: See TracChangeset for help on using the changeset viewer.