首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CS1503参数1:无法从'Poc.Core.Player‘转换为'Poc.Interfaces.IScheduleable’

CS1503参数1:无法从'Poc.Core.Player‘转换为'Poc.Interfaces.IScheduleable’
EN

Stack Overflow用户
提问于 2020-03-31 16:24:57
回答 1查看 42关注 0票数 1

对不起,我是编程新手,所以这可能是一个容易解决的问题,我没有足够的知识知道如何修复。

我正在使用本教程来实现一个简单的地下城爬虫https://bitbucket.org/FaronBracy/roguesharpv3tutorial/src/master/

当涉及到实现kobolds (我的项目中的变种)的行为时,我会得到一个错误,说‘参数1:无法从’Poc.Core.(玩家/怪物)‘转换为"Poc.Interface.ISchedule“。

这发生在DungeonMap.cs中的addplayer void、addmonster void和removemonster void上,并且两次发生在CommandSystem.cs中的ActivateMonsters void上。

如果有人能帮我解决这个问题,我将不胜感激。

问题空格:

代码语言:javascript
复制
public void AddPlayer(Player player)
    {
        Game.Player = player;
        SetIsWalkable(player.X, player.Y, false);
        UpdatePlayerFieldOfView();
        **Game.SchedulingSystem.Add(player);**
    }




public void AddMonster(Monster monster)
    {
        _monsters.Add(monster);
        // After adding the monster to the map make sure to make the 
        cell not walkable
        SetIsWalkable(monster.X, monster.Y, false);
         **Game.SchedulingSystem.Add( monster );**
    }



public void RemoveMonster(Monster monster)
    {
        _monsters.Remove(monster);
        SetIsWalkable(monster.X, monster.Y, true);
        **SchedulingSystem.Remove(monster);**

    }


public void ActivateMonsters()
    {
        IScheduleable scheduleable = Game.SchedulingSystem.Get();
        if (scheduleable is Player)
        {
            IsPlayerTurn = true;
            **Game.SchedulingSystem.Add(Game.Player);**
        }
        else
        {
            Monster monster = scheduleable as Monster;

            if (monster != null)
            {
                monster.PerformAction(this);
                **Game.SchedulingSystem.Add(monster);**
            }

            ActivateMonsters();
        }
    }

然后我的调度系统代码

代码语言:javascript
复制
namespace Poc.Systems
{
public class SchedulingSystem
{
    private int _time;
    private readonly SortedDictionary<int, List<IScheduleable>> _scheduleables;

    public SchedulingSystem()
    {
        _time = 0;
        _scheduleables = new SortedDictionary<int, List<IScheduleable>>();
    }


    public void Add(IScheduleable scheduleable)
    {
        int key = _time + scheduleable.Time;
        if (!_scheduleables.ContainsKey(key))
        {
            _scheduleables.Add(key, new List<IScheduleable>());
        }
        _scheduleables[key].Add(scheduleable);
    }

    public void Remove(IScheduleable scheduleable)
    {
        KeyValuePair<int, List<IScheduleable>> scheduleableListFound
          = new KeyValuePair<int, List<IScheduleable>>(-1, null);

        foreach (var scheduleablesList in _scheduleables)
        {
            if (scheduleablesList.Value.Contains(scheduleable))
            {
                scheduleableListFound = scheduleablesList;
                break;
            }
        }
        if (scheduleableListFound.Value != null)
        {
            scheduleableListFound.Value.Remove(scheduleable);
            if (scheduleableListFound.Value.Count <= 0)
            {
                _scheduleables.Remove(scheduleableListFound.Key);
            }
        }
    }


    public IScheduleable Get()
    {
        var firstScheduleableGroup = _scheduleables.First();
        var firstScheduleable = firstScheduleableGroup.Value.First();
        Remove(firstScheduleable);
        _time = firstScheduleableGroup.Key;
        return firstScheduleable;
    }

    // Get the current time (turn) for the schedule
    public int GetTime()
    {
        return _time;
    }


    {
        _time = 0;
        _scheduleables.Clear();
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2020-11-18 03:18:16

确保您的Actor类( PlayerMonster继承自该类)正在实现IScheduleable

代码语言:javascript
复制
public class Actor : IActor, IDrawable, IScheduleable
{
  // ... Previous Actor code omitted
 
  // IScheduleable
  public int Time
  {
    get
    {
      return Speed;
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60946223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档