示例#1
0
 void Keyboard_KeyDown(object sender, OpenTK.Input.KeyboardKeyEventArgs e)
 {
     if (KeyMapper.GetMap(e.Key) == VirtualKeyCode.NoKey)
     {
         return; // If there is no corresponding key mapping, we can't do anything about it.
     }
     keysdown.Enqueue((byte)e.Key);
     keyspressed.Add((byte)e.Key);
 }
示例#2
0
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            System.Windows.Forms.Application.DoEvents();
            // don't update until the screen is drawn
            if (!drawn)
            {
                return;
            }
            drawn = false;
            // Begin step
            for (var i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                LibraryContext.Current.PerformEvent(env, EventType.Step, (int)StepEventNumber.BeginStep);
            }
            // Alarm 0-12
            for (var i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                if (env.Destroyed)
                {
                    continue;                // don't bother.
                }
                var o = Framework.Object.Manager[env.object_index];
                for (int j = 0; j < 12; j++)
                {
                    if (o.EventDefined(EventType.Alarm, i))
                    {
                        if (env.alarm[j] >= 0 && --env.alarm[j] == 0)
                        {
                            LibraryContext.Current.PerformEvent(env, EventType.Alarm, j);
                        }
                    }
                }
            }
            bool found = false;

            // Keyboard events
            foreach (byte key in keyspressed)
            {
                if (!Keyboard[(OpenTK.Input.Key)key])
                {
                    continue;
                }
                found = true;
                for (int i = 0; i < SortedInstanceIds.Count; i++)
                {
                    var env = Game.GetInstance(SortedInstanceIds[i]);
                    LibraryContext.Current.PerformEvent(env, EventType.Keyboard, (int)KeyMapper.GetMap((OpenTK.Input.Key)key));
                }
            }
            // vk_any/vk_none
            for (int i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                LibraryContext.Current.PerformEvent(env, EventType.Keyboard, (int)(found ? VirtualKeyCode.AnyKey : VirtualKeyCode.NoKey));
            }
            found = keysdown.Count != 0;
            // Key press events
            while (keysdown.Count != 0)
            {
                int key = keysdown.Dequeue();
                for (int i = 0; i < SortedInstanceIds.Count; i++)
                {
                    var env = Game.GetInstance(SortedInstanceIds[i]);
                    LibraryContext.Current.PerformEvent(env, EventType.KeyPress, (int)KeyMapper.GetMap((OpenTK.Input.Key)key));
                }
            }
            // vk_any/vk_none
            for (int i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                LibraryContext.Current.PerformEvent(env, EventType.KeyPress, (int)(found ? VirtualKeyCode.AnyKey : VirtualKeyCode.NoKey));
            }
            // Key release events
            found = false;
            int released = 0;

            for (int i = 0; i < keyspressed.Count; i++)
            {
                byte key = keyspressed[i];
                keyspressed[i - released] = key;
                if (!Keyboard[(OpenTK.Input.Key)key])
                {
                    found = true;
                    released++;
                    for (int j = 0; j < SortedInstanceIds.Count; j++)
                    {
                        var env = Game.GetInstance(SortedInstanceIds[j]);
                        LibraryContext.Current.PerformEvent(env, EventType.KeyRelease, (int)KeyMapper.GetMap((OpenTK.Input.Key)key));
                    }
                }
            }
            if (released > 0)
            {
                keyspressed.RemoveRange(keyspressed.Count - released, released);
            }
            // vk_any/vk_none
            for (int i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                LibraryContext.Current.PerformEvent(env, EventType.KeyRelease, (int)(found ? VirtualKeyCode.AnyKey : VirtualKeyCode.NoKey));
            }
            // Step
            for (int i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                LibraryContext.Current.PerformEvent(env, EventType.Step, (int)StepEventNumber.Normal);
            }
            // Set instances to their new positions
            for (int i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                if (env.Destroyed)
                {
                    continue;                // don't bother.
                }
                env.xprevious = env.x;
                env.yprevious = env.y;
                // friction first
                env.speed = env.friction > 0 && Math.Abs(env.speed) <= env.friction ? 0.0 : env.speed - env.friction * Math.Sign(env.speed);
                // then gravity
                env.AddSpeedVector(
                    GmlFunctions.lengthdir_x(env.gravity, env.gravity_direction),
                    GmlFunctions.lengthdir_y(env.gravity, env.gravity_direction)
                    );
                // move the object
                env.x += env.hspeed;
                env.y += env.vspeed;
            }
            // End Step
            for (int i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                LibraryContext.Current.PerformEvent(env, EventType.Step, (int)StepEventNumber.EndStep);
            }
            /* After-step duty */
            // Purge destroyed instances, in O(N) time.
            // This is done by first compacting the array, then shortening it.
            // Also update image_index in this loop.
            int purged = 0; // the number of purged instances.

            for (int i = 0; i < SortedInstanceIds.Count; i++)
            {
                var env = Game.GetInstance(SortedInstanceIds[i]);
                if (env.Destroyed)
                {
                    purged++;                                       // increment the number of purged instances
                    SortedInstanceIds.Remove(SortedInstanceIds[i]); // remove the instance from the instance lookup dictionary
                    continue;
                }
                // if (image_single < 0) image_index += image_speed
                if (env.image_single < 0.0)
                {
                    env.image_index += env.image_speed;
                }
                if (purged != 0)
                {
                    SortedInstanceIds[i - purged] = SortedInstanceIds[i]; // move the instance to compact the array
                }
            }
            if (purged != 0)
            {
                SortedInstanceIds.RemoveRange(SortedInstanceIds.Count - purged, purged); // Shorten the list
            }
        }