Currently, I am developing a small JavaScript game that relies on a map rendered from a collection of coordinates stored in a database. As I work on designing the database, I am contemplating between two methods to determine which one would be more preferable.
The world in my game is mapped to 1000x1000 tiles, with each coordinate pair (x,y) representing a unique tile with its own set of properties.
Given that this is my first time creating an object/database like this, my initial thought was to use the unique coordinate pair as the primary key. This method not only guarantees no collision but also provides a clear way of allocating the primary keys. For example, tile x: 844, y:444 would have an ID of 08440444. Would using this approach make it more computationally challenging to retrieve an X coordinate from a given ID compared to the alternative? Are there other ways to construct an ID from an [x,y] pair?
┌──────────────────┐ │ table: tiles │ ┌─────────────────┼──────────────────┼─────────────────┐ │ coord ├ treasures │ │ ├ people │ │───┬────── integer │ │ │ │─primary key │ hasMany │ │unique x,y coord│ relationship │relationship │ │ │ │ │ └─────────────────┴──────────────────┴─────────────────┘
or
┌──────────────────┐ │ table: tiles │ ┌─────────────────┬────────────┼──────────────────┼─────────────────┬─────────────────┐ │ id │ x ├ y │ treasures │ people │ │ │ │ │ │ │ ├────────────────-┼────────────┼──────────────────┼──────────────────┼──────────────┤ │ int │ int │ int │ │ │ │ primary key │represents x│represents y│hasMany │hasMany │ │ autoincrementing│coordinate │ coordinate │ relationship │ relationship │ │ │ │ │ │ └────────────────-┴────────────┴──────────────────┴────────────────--┴──────────────┘