Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:25:36+00:00 2026-05-25T11:25:36+00:00

My game has class Enemy who’s AI/functionality I can change with trait Moving trait

  • 0

My game has

class Enemy

who’s AI/functionality I can change with

trait Moving
trait VerticalMover extends Moving
trait RandomMover extends Moving

and so on. Now I need to fetch preloaded stuff based on trait. What I would like to do is have a Map that accepts all traits that extend Moving as keys and then some EnemyContainer as value that would have trait related content preloaded.

But how do I define such a Map and how do format my .get() to get the container by an instance of some Enemy. Something like:

val myEnemy = new Enemy with RandomMover 
val myDetails:EnemyContainer = enemyDetailsStore.get(myEnemy.getClass)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:25:37+00:00Added an answer on May 25, 2026 at 11:25 am

    Well, I assume that your enemy details store is of type Map[Class[_ <: Moving], EnemyDetails]. I suspect that something like:

    //gives a Map[Class[_ <: Moving], EnemyDetails] for all matching keys
    enemyDetailsStore.filterKeys(_ isInstance myEnemy) 
    

    Or:

    //Iterable[EnemyDetails]
    enemyDetailsStore collect { case (c, d) if c isInstance myEnemy => d }
    

    Or even just:

    //Option[EnemyDetails]
    enemyDetailsStore collectFirst { case (c, d) if c isInstance myEnemy => d }
    

    Will do for you. The only “issue” with this code is that it’s O(N), in that it requires a traversal of the map, rather than a simple lookup, which would be O(1), or O(log N)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I'm writing a game that has a vector of a pairent class (enemy)
I'm making a game right now and pretty much everything has its own class.
So I'm developing and iPhone game right now and everything has been working just
I have a game I'm writing in java, it has the functionality to save
I have a core data class Game which has a to-many relationship to another
The JDBC java.sql.Statement class has a cancel() method. This can be called in another
I've got a class: Game which has values. I've got two arrays with Game
My question is really short. I have a game which has a reset class
I am making a game which has a global (static) class called MedGameController. In
I have a game with a variety of ship types. My Ship class has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.