public ActionRemover(GameMaker.ProjectCommon.Project project) { this.project = project; this.code = new StringBuilder(); }
public static void RemoveDNDForEvent(GameMaker.ProjectCommon.Project project, GameMaker.ProjectCommon.Event ev) { var remover = new ActionRemover(project); foreach (var action in ev.Actions) { remover.Begin(action); switch ((ActionID)action.ActionId) { #region Move case ActionID.StartMoving: remover.AddLine($"direction = {action.Arguments[0].Value};"); remover.AddLine($"speed {remover.RelativeSign} {action.Arguments[1].Value};"); break; case ActionID.SetDirection: remover.AddLine($"direction {remover.RelativeSign} {action.Arguments[0].Value};"); remover.AddLine($"speed {remover.RelativeSign} {action.Arguments[1].Value};"); break; case ActionID.MoveTowardsPoint: remover.AddLine($"move_towards_point({remover.RelativeOnly("x + ")}{action.Arguments[0].Value}, {remover.RelativeOnly("y + ")}{action.Arguments[1].Value}, {action.Arguments[2].Value});"); break; case ActionID.SetHorizontalSpeed: remover.AddLine($"hspeed {remover.RelativeSign} {action.Arguments[0].Value};"); break; case ActionID.SetVerticalSpeed: remover.AddLine($"vspeed {remover.RelativeSign} {action.Arguments[0].Value};"); break; case ActionID.SetGravity: remover.AddLine($"gravity_direction {remover.RelativeSign} {action.Arguments[0].Value};"); remover.AddLine($"gravity {remover.RelativeSign} {action.Arguments[1].Value};"); break; case ActionID.ReverseHorizontalDirection: remover.AddLine($"hspeed *= -1;"); break; case ActionID.ReverseVerticalDirection: remover.AddLine($"vspeed *= -1;"); break; case ActionID.SetFriction: remover.AddLine($"friction {remover.RelativeSign} {action.Arguments[0].Value};"); break; case ActionID.JumpToPosition: remover.AddLine($"x {remover.RelativeSign} {action.Arguments[0].Value};"); remover.AddLine($"y {remover.RelativeSign} {action.Arguments[1].Value};"); break; case ActionID.JumpToStartPosition: remover.AddLine($"x = xstart;"); remover.AddLine($"y = ystart;"); break; case ActionID.JumpToRandomPosition: remover.AddLine($"x = random(room_width);"); remover.AddLine($"y = random(room_height);"); remover.AddLine($"move_snap({action.Arguments[0].Value}, {action.Arguments[1].Value});"); break; case ActionID.AlignToGrid: remover.AddLine($"move_snap({action.Arguments[0].Value}, {action.Arguments[1].Value});"); break; case ActionID.WarpWhenOutside: var warpX = action.Arguments[0].Value == "0" || action.Arguments[0].Value == "2"; var warpY = action.Arguments[0].Value == "1" || action.Arguments[0].Value == "2"; remover.AddLine($"move_warp({(warpX ? "true" : "false")}, {(warpY ? "true" : "false")}, 0);"); break; case ActionID.MoveToContact: var funcName = "move_contact_solid"; if (action.Arguments[2].Value == "1") { funcName = "move_contact_all"; } remover.AddLine($"{funcName}({action.Arguments[0].Value}, {action.Arguments[1].Value});"); break; case ActionID.BounceAgainst: funcName = "move_bounce_solid"; if (action.Arguments[1].Value == "1") { funcName = "move_bounce_all"; } remover.AddLine($"{funcName}({action.Arguments[0].Value});"); break; case ActionID.SetPath: remover.AddLine($"path_start({action.Arguments[0].Value}, {action.Arguments[1].Value}, {action.Arguments[2].Value}, {action.Arguments[3].Value});"); break; case ActionID.EndPath: remover.AddLine($"path_end();"); break; case ActionID.SetPathPosition: remover.AddLine($"path_position {remover.RelativeSign} {action.Arguments[0].Value};"); break; case ActionID.SetPathSpeed: remover.AddLine($"path_speed {remover.RelativeSign} {action.Arguments[0].Value};"); break; case ActionID.StepTowardPoint: remover.AddLine($"mp_linear_step({action.Arguments[0].Value}, {action.Arguments[1].Value}, {action.Arguments[2].Value}, {action.Arguments[3].Value});"); break; case ActionID.StepAvoiding: remover.AddLine($"mp_potential_step({action.Arguments[0].Value}, {action.Arguments[1].Value}, {action.Arguments[2].Value}, {action.Arguments[3].Value});"); break; #endregion #region Main 1 case ActionID.CreateInstance: var objName = action.Arguments[0].Value; remover.AddLine($"instance_create({remover.RelativeOnly("x + ")}{action.Arguments[1].Value}, {remover.RelativeOnly("y + ")}{action.Arguments[2].Value}, {objName});"); break; case ActionID.CreateMovingInstance: objName = action.Arguments[0].Value; remover.AddLine($"with (instance_create({remover.RelativeOnly("x + ")}{action.Arguments[1].Value}, {remover.RelativeOnly("y + ")}{action.Arguments[2].Value}, {objName}))"); remover.AddLine("{"); remover.AddLine($" speed {remover.RelativeSign} {action.Arguments[3].Value};"); remover.AddLine($" direction {remover.RelativeSign} {action.Arguments[4].Value};"); remover.AddLine("}"); break; case ActionID.CreateRandomInstance: objName = $"{project.Objects[int.Parse(action.Arguments[0].Value)].Name} || {project.Objects[int.Parse(action.Arguments[1].Value)].Name} || {project.Objects[int.Parse(action.Arguments[2].Value)].Name} || {project.Objects[int.Parse(action.Arguments[3].Value)].Name}"; remover.AddLine($"instance_create({remover.RelativeOnly("x + ")}{action.Arguments[4].Value}, {remover.RelativeOnly("y + ")}{action.Arguments[5].Value}, {objName});"); break; case ActionID.ChangeInstanceInto: objName = action.Arguments[0].Value; remover.AddLine($"instance_change({objName}, {action.Arguments[0].Value});"); break; case ActionID.DestroyInstance: remover.AddLine($"instance_destroy();"); break; case ActionID.DestroyInstanceAtPosition: var x = action.Arguments[0].Value; var y = action.Arguments[1].Value; remover.AddLine($"while (instance_position({remover.RelativeOnly("x + ")}{x}, {remover.RelativeOnly("y + ")}{y}, all) != noone)"); remover.AddLine("{"); remover.AddLine($" with (instance_position({remover.RelativeOnly("x + ")}{x}, {remover.RelativeOnly("y + ")}{y}, all))"); remover.AddLine($" instance_destroy();"); remover.AddLine("}"); break; case ActionID.ChangeSpriteInto: var sprName = action.Arguments[0].Value; remover.AddLine($"sprite_index = {sprName};"); remover.AddLine($"image_index = {action.Arguments[1].Value};"); remover.AddLine($"image_speed = {action.Arguments[2].Value};"); break; case ActionID.TransformSprite: var xscale = int.Parse(action.Arguments[0].Value) * (action.Arguments[3].Value == "1" || action.Arguments[3].Value == "3" ? -1 : 1); var yscale = int.Parse(action.Arguments[1].Value) * (action.Arguments[3].Value == "2" || action.Arguments[3].Value == "3" ? -1 : 1); remover.AddLine($"image_xscale = {xscale};"); remover.AddLine($"image_yscale = {yscale};"); remover.AddLine($"image_angle = {action.Arguments[2].Value};"); break; case ActionID.SetSpriteColorBlending: remover.AddLine($"image_blend = {action.Arguments[0].Value};"); remover.AddLine($"image_alpha = {action.Arguments[1].Value};"); break; case ActionID.PlaySound: funcName = "sound_play"; if (action.Arguments[1].Value == "1") { funcName = "sound_loop"; } remover.AddLine($"{funcName}({action.Arguments[0].Value});"); break; case ActionID.StopSound: remover.AddLine($"sound_stop({action.Arguments[0].Value});"); break; case ActionID.IfSoundIsPlaying: remover.AddCondition($"if (sound_is_playing({action.Arguments[0].Value})"); break; case ActionID.GoToNextRoom: remover.AddLine($"transition_kind = {action.Arguments[0].Value};"); remover.AddLine($"room_goto_previous();"); break; case ActionID.RestartCurrentRoom: remover.AddLine($"transition_kind = {action.Arguments[0].Value};"); remover.AddLine($"room_restart();"); break; case ActionID.GoToRoom: remover.AddLine($"transition_kind = {action.Arguments[0].Value};"); remover.AddLine($"room_goto({action.Arguments[1].Value});"); break; case ActionID.IfPreviousRoomExists: remover.AddCondition($"if (room_previous(room) != -1)"); break; case ActionID.IfNextRoomExists: remover.AddCondition($"if (room_next(room) != -1)"); break; #endregion #region Main 2 case ActionID.SetAlarm: remover.AddLine($"alarm[{action.Arguments[1].Value}] {remover.RelativeSign} {action.Arguments[0].Value};"); break; case ActionID.Sleep: remover.AddLine($"sleep({action.Arguments[0].Value});"); if (action.Arguments[0].Value == "1") { remover.AddLine($"screen_redraw();"); } break; case ActionID.SetTimeLine: remover.AddLine($"timeline_index = {action.Arguments[0].Value};"); remover.AddLine($"timeline_position = {action.Arguments[1].Value};"); break; case ActionID.SetTimeLinePosition: remover.AddLine($"timeline_position = {action.Arguments[0].Value};"); break; case ActionID.DisplayMessage: remover.AddLine($"show_message({action.Arguments[0].Value});"); break; case ActionID.ShowGameInfo: remover.AddLine($"show_info({action.Arguments[0].Value});"); break; case ActionID.ShowVideo: remover.AddLine($"splash_set_fullscreen({action.Arguments[1].Value});"); remover.AddLine($"splash_show_video({action.Arguments[0].Value}, {action.Arguments[2].Value});"); break; case ActionID.RestartGame: remover.AddLine($"game_restart();"); break; case ActionID.EndGame: remover.AddLine($"game_end();"); break; case ActionID.SaveGame: remover.AddLine($"game_save({action.Arguments[0].Value});"); break; case ActionID.LoadGame: remover.AddLine($"game_load({action.Arguments[0].Value});"); break; case ActionID.ReplaceFromSprite: remover.AddLine($"sprite_replace({action.Arguments[0].Value}, {action.Arguments[1].Value}, {action.Arguments[2].Value}, 0, 0, sprite_get_xoffset({action.Arguments[0].Value}), sprite_get_yoffset({action.Arguments[0].Value}));"); break; case ActionID.ReplaceFromSound: remover.AddLine($"sound_replace({action.Arguments[0].Value}, {action.Arguments[1].Value}, 0, 0);"); break; case ActionID.ReplaceFromBackground: remover.AddLine($"background_replace_background({action.Arguments[0].Value}, {action.Arguments[1].Value});"); break; #endregion #region Control case ActionID.IfPositionCollisionFree: funcName = "place_free"; if (action.Arguments[2].Value == "1") { funcName = "place_empty"; } remover.AddCondition($"if ({remover.NotSign}{funcName}({remover.RelativeOnly("x + ")}{action.Arguments[0].Value}, {remover.RelativeOnly("y + ")}{action.Arguments[1].Value}))"); break; case ActionID.IfCollisionAtPosition: funcName = "place_free"; if (action.Arguments[2].Value == "1") { funcName = "place_empty"; } remover.AddCondition($"if ({(remover.NotSign == "!" ? "" : "!")}{funcName}({remover.RelativeOnly("x + ")}{action.Arguments[0].Value}, {remover.RelativeOnly("y + ")}{action.Arguments[1].Value}))"); break; case ActionID.IfObjectAtPosition: remover.AddCondition($"if ({remover.NotSign}position_meeting({remover.RelativeOnly("x + ")}{action.Arguments[1].Value}, {remover.RelativeOnly("y + ")}{action.Arguments[2].Value}, {action.Arguments[0].Value}))"); break; case ActionID.IfNumberInstancesIsValue: var op = action.Arguments[2].Value switch { "0" => "==", "1" => "<", "2" => ">", _ => throw new Exception("Unknown operation"), }; remover.AddCondition($"if ({remover.NotSign}instance_number({action.Arguments[0].Value}) {op} {action.Arguments[1].Value})"); break; case ActionID.IfUserAnswerYes: remover.AddCondition($"if ({remover.NotSign}show_question({action.Arguments[0].Value}))"); break; case ActionID.IfExpressionIsTrue: remover.AddCondition($"if ({remover.NotSign}({action.Arguments[0].Value}))"); break; case ActionID.IfMouseButtonIsPressed: var button = action.Arguments[0].Value switch { "0" => "noone", "1" => "mb_left", "2" => "mb_right", "3" => "mb_middle", _ => throw new Exception("Unknown mouse button"), }; remover.AddCondition($"if ({remover.NotSign}mouse_check_button({button}))"); break; case ActionID.IfInstanceIsAlignedWithGrid: remover.AddCondition($"if ({remover.NotSign}place_snapped({action.Arguments[0].Value}, {action.Arguments[1].Value}))"); break; case ActionID.StartBlock: remover.AddLine("{"); break; case ActionID.Else: remover.AddCondition($"else"); break; case ActionID.Exit: remover.AddLine($"exit;"); break; case ActionID.EndBlock: remover.AddLine("}"); break; case ActionID.Repeat: remover.AddCondition($"repeat ({action.Arguments[0].Value})"); break; case ActionID.CallInheritedEvent: remover.AddLine($"event_inherited();"); break; case ActionID.ExecuteCode: foreach (var ln in action.Arguments[0].Value.Split('\n')) { remover.AddLine(ln); } break; case ActionID.ExecuteScript: remover.AddLine($"{action.Arguments[0].Value}({action.Arguments[1].Value}, {action.Arguments[2].Value}, {action.Arguments[3].Value}, {action.Arguments[4].Value}, {action.Arguments[5].Value});"); break; case ActionID.Comment: remover.AddLine($"// {action.Arguments[0].Value}"); break; case ActionID.SetVariable: remover.AddLine($"{action.Arguments[0].Value} {remover.RelativeSign} {action.Arguments[1].Value};"); break; case ActionID.IfVariable: op = action.Arguments[2].Value switch { "0" => "==", "1" => "<", "2" => ">", _ => throw new Exception("Unknown operation"), }; remover.AddCondition($"if ({remover.NotSign}{action.Arguments[0].Value} {op} {action.Arguments[1].Value})"); break; case ActionID.DrawVariable: remover.AddLine($"draw_text({remover.RelativeOnly("x + ")}{action.Arguments[1].Value}, {remover.RelativeOnly("y + ")}{action.Arguments[2].Value}, {action.Arguments[0].Value});"); break; #endregion } remover.Finish(); } var codeAction = remover.Apply(); ev.Actions.Clear(); ev.Actions.Add(codeAction); }