public GameObject(Control form, Rectangle playAreaBoundary, string name, float speed, float scale = 1f) { this._playArea = playAreaBoundary; Position = new Point(); Dimensions = new Dimensions(); // Subscribe to key press events. form.MouseDown += MouseClick; form.MouseUp += MouseUp; // Starting position. this.Scale = scale; this.BaseSpeed = speed; this.Name = name; this.Animator = new Animator(this); // load collision json data string jsonData = File.ReadAllText(Path.Combine("Data", Name, "Collision.json")); CollisionConfig = JsonConvert.DeserializeObject <CollisionConfig>(jsonData); ArraySetup(); // Setup arrays for overflow handling. }
public bool CanMove(Level level, Direction dir) { // Recursive movement ability checking var targetPos = _entity.Position + Utils.MoveDelta(dir); foreach (var entityInTargetPos in level.GetActiveEntitiesAt(targetPos)) { // If we collide with object in a target position and an object can be pushed // Then recursively check whether it has some space to move if (CollisionConfig.ObjectsCollide(_entity.ObjectType, entityInTargetPos.ObjectType)) { if (CollisionConfig.CanPush( _entity.ObjectType, entityInTargetPos.ObjectType)) { var movable = entityInTargetPos.GetComponent <Movable>(); if (movable != null) { return(movable.CanMove(level, dir)); } } return(false); } } // No obstacles or no collisions return(true); }
public override void OnInspectorGUI() { CollisionConfig config = target as CollisionConfig; GUILayout.Label("Back To Empty state"); if (GUILayout.Button("init/reset")) { config.init(); ZLog.log(config.ToString()); } GUILayout.Space(20); GUILayout.Label("Sync if you change ColliderType"); if (GUILayout.Button("Refresh")) { config.Refresh(); ZLog.log(config.ToString()); valueChange = true; } GUILayout.Space(20); if (valueChange) { if (GUILayout.Button("Value changed, Click or Press Ctrl + S", GUILayout.Height(50))) { valueChange = false; AssetDatabase.SaveAssets(); } } show(config); EditorUtility.SetDirty(config); //This is very important }
private void show(CollisionConfig config) { int labelSize = 50; // find the longest label for (int i = 0; i < config.allTypeNames.Length; i++) { var textDimensions = GUI.skin.label.CalcSize(new GUIContent(config.allTypeNames[i])); if (labelSize < textDimensions.x) { labelSize = (int)textDimensions.x; } } labelSize += 30; if (true) { int checkboxSize = 16; int indent = 0; var topLabelRect = GUILayoutUtility.GetRect(checkboxSize + labelSize, labelSize); var topLeft = new Vector2(topLabelRect.x, topLabelRect.y); var y = 0; for (int i = 0; i < config.allTypeNames.Length; i++) { var translate = new Vector3(labelSize + indent + checkboxSize * y + topLeft.x, topLeft.y, 0); //GUI.matrix = Matrix4x4.TRS(translate, Quaternion.Euler(0, 0, 90), Vector3.one); GUI.matrix = Matrix4x4.TRS(new Vector3(180 + checkboxSize * y, topLeft.y, 0), Quaternion.Euler(0, 0, 90), Vector3.one); GUI.Label(new Rect(0, 0, labelSize, checkboxSize), config.allTypeNames[i], "RightLabel"); y++; } GUI.matrix = Matrix4x4.identity; y = 0; for (int i = 0; i < config.allTypeNames.Length; i++) { int x = 0; var r = GUILayoutUtility.GetRect(indent + checkboxSize * config.allTypeNames.Length + labelSize, checkboxSize); GUI.Label(new Rect(/*r.x + indent*/ 0, r.y, labelSize, checkboxSize), config.allTypeNames[i], "RightLabel"); for (int j = 0; j < config.allTypeNames.Length; j++) { var tooltip = new GUIContent("", config.allTypeNames[i] + " with " + config.allTypeNames[j]); bool val = getValue(config, i, j); bool toggle = GUI.Toggle(new Rect(labelSize + indent + r.x + x * checkboxSize, r.y, checkboxSize, checkboxSize), val, tooltip); if (toggle != val) { setValue(config, i, j, toggle); valueChange = true; } x++; } y++; } } }
public IEnumerable <IChange> Handle(Level level, ICommand command) { if (command is SpawnCommand spawnCommand) { var entity = level.Spawn( Prefab, _entity.Position + Utils.MoveDelta(_entity.Orientation), _entity.Orientation); foreach (var entityInTargetPos in level.GetActiveEntitiesAt(entity.Position)) { if (entityInTargetPos.Id != entity.Id) { // If current object collides with target object if (CollisionConfig.ObjectsCollide(entity.ObjectType, entityInTargetPos.ObjectType)) { level.DispatchEarly(new CollisionEvent( target: entityInTargetPos.Id, sourceId: entity.Id, direction: Utils.AbsoluteDirectionToRelative(Utils.RevertDirection(spawnCommand.Direction), entityInTargetPos.Orientation))); level.DispatchEarly(new CollisionEvent( target: entity.Id, sourceId: entityInTargetPos.Id, direction: Utils.AbsoluteDirectionToRelative(spawnCommand.Direction, entity.Orientation))); } if (CollisionConfig.ObjectsHit(entity.ObjectType, entityInTargetPos.ObjectType)) { level.DispatchEarly(new HitCommand( target: entityInTargetPos.Id, sourceId: entity.Id, direction: Utils.AbsoluteDirectionToRelative(Utils.RevertDirection(spawnCommand.Direction), entityInTargetPos.Orientation))); level.DispatchEarly(new HitCommand( target: entity.Id, sourceId: entityInTargetPos.Id, direction: Utils.AbsoluteDirectionToRelative(spawnCommand.Direction, entity.Orientation))); } } } if (entity != null) { if (Animator != null) { Animator.SetTrigger(AnimOnSpawnTrigger); } SoundManager.Instance.Play(SpawnSound); ShootFx?.Trigger(transform); yield return(new SpawnChange(_entity.Id, entity.Id)); } } }
public override void OnInspectorGUI() { base.OnInspectorGUI(); _colliderConfig = (target as UnityGameConfig).pureConfig.CollisionConfig; var pos = new Vector2(_colliderConfig.scrollPos.x, _colliderConfig.scrollPos.y); DoGUI("LayerCollisionMatrix", ref _colliderConfig.isShow, ref pos, _colliderConfig.GetColliderPair, _colliderConfig.SetColliderPair); if (GUILayout.Button("ToJson")) { UnityGameConfig.SaveToJson((target as UnityGameConfig).pureConfig); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }
public IEnumerable <IChange> DoMove(Level level, Direction moveDirection, bool updateOrientation, MovementType movementType) { var canMove = CanMove(level, moveDirection); // Update logical position var targetPos = _entity.Position + Utils.MoveDelta(moveDirection); // Move neighbor movable (push) foreach (var entityInTargetPos in level.GetActiveEntitiesAt(targetPos)) { // If current object collides with target object if (CollisionConfig.ObjectsCollide(_entity.ObjectType, entityInTargetPos.ObjectType)) { level.DispatchEarly(new CollisionEvent( target: entityInTargetPos.Id, sourceId: _entity.Id, direction: Utils.AbsoluteDirectionToRelative(Utils.RevertDirection(moveDirection), entityInTargetPos.Orientation))); level.DispatchEarly(new CollisionEvent( target: _entity.Id, sourceId: entityInTargetPos.Id, direction: Utils.AbsoluteDirectionToRelative(moveDirection, _entity.Orientation))); // Push (collidable only) if (canMove && CollisionConfig.CanPush( _entity.ObjectType, entityInTargetPos.ObjectType)) { var movable = entityInTargetPos.GetComponent <Movable>(); if (movable != null) { foreach (var change in movable.DoMove(level, moveDirection, false, MovementType.Pushed)) { yield return(change); } } } } if (CollisionConfig.ObjectsHit(_entity.ObjectType, entityInTargetPos.ObjectType)) { level.DispatchEarly(new HitCommand( target: entityInTargetPos.Id, sourceId: _entity.Id, direction: Utils.AbsoluteDirectionToRelative(Utils.RevertDirection(moveDirection), entityInTargetPos.Orientation))); level.DispatchEarly(new HitCommand( target: _entity.Id, sourceId: entityInTargetPos.Id, direction: Utils.AbsoluteDirectionToRelative(moveDirection, _entity.Orientation))); } } if (!canMove) { yield break; } if (!_entity.IsActive) { yield break; } // Finally, move self targetPos = _entity.Position + Utils.MoveDelta(moveDirection); var targetOrientation = _entity.Orientation; if (updateOrientation) { targetOrientation = moveDirection; } // Effects SoundManager.Instance.Play(MoveSound); var selfMove = new MoveChange(_entity.Id) { OriginalOrientation = _entity.Orientation, OriginalPosition = _entity.Position, TargetOrientation = targetOrientation, TargetPosition = targetPos, MovementType = movementType }; _entity.MoveTo(targetPos, targetOrientation, movementType); yield return(selfMove); }
private void setValue(CollisionConfig config, int i, int j, bool value) { config[i, j] = value; }
private bool getValue(CollisionConfig config, int i, int j) { return(config[i, j]); }