commit ce91b12e7f82fbb1954a415eac51ca79a6d1daf3 Author: dox Date: Sun Feb 12 00:09:02 2023 -0500 screenshots diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..6154ed1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,68 @@ +cmake_minimum_required(VERSION 3.10) + +project(calamitycobra C) + +set_target_properties(PROPERTIES LINKER_LANGUAGE C) + +include_directories(${PROJECT_SOURCE_DIR}/src) + +file(GLOB src + "${PROJECT_SOURCE_DIR}/src/*.c" + "${PROJECT_SOURCE_DIR}/src/engine/*.c" + "${PROJECT_SOURCE_DIR}/src/game/*.c" + "${PROJECT_SOURCE_DIR}/src/game/ending/*.c" + "${PROJECT_SOURCE_DIR}/src/game/gui/*.c" + "${PROJECT_SOURCE_DIR}/src/game/info/*.c" + "${PROJECT_SOURCE_DIR}/src/game/intro/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/collectible/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/enemy/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/gui/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/message/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/pause/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/player/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/shadow/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/spawn/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/tutorial/*.c" + "${PROJECT_SOURCE_DIR}/src/game/play/wave/*.c" + "${PROJECT_SOURCE_DIR}/src/game/results/*.c" + "${PROJECT_SOURCE_DIR}/src/game/results/*.c" + "${PROJECT_SOURCE_DIR}/src/game/scene/*.c" + "${PROJECT_SOURCE_DIR}/src/game/title/*.c" + "${PROJECT_SOURCE_DIR}/src/sdl/*.c" + "${PROJECT_SOURCE_DIR}/src/util/*.c" +) + +add_executable(calamitycobra ${src}) + +if (EMSCRIPTEN) + set (CMAKE_C_FLAGS "-O2 --preload-file res --use-preload-plugins -s NO_DYNAMIC_EXECUTION=1 -s ALLOW_MEMORY_GROWTH=1 -s ASSERTIONS=1 -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=0 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='['png']' -s USE_SDL_MIXER=2 -s SDL2_MIXER_FORMATS='['ogg']' -s USE_SDL_TTF=2") + + if (CMAKE_BUILD_TYPE STREQUAL "Debug") + set (CMAKE_C_FLAGS "-DDEBUG -g -Wno-unused-command-line-argument" ${CMAKE_C_FLAGS}) + else() + set (CMAKE_BUILD_TyPE "Release") + endif() + + set(CMAKE_EXECUTABLE_SUFFIX ".html") +else() + set (CMAKE_C_FLAGS "-O2") + + if (CMAKE_BUILD_TYPE STREQUAL "Debug") + set (CMAKE_C_FLAGS "-DDEBUG -g -O2 -Wno-unused-variable -Wno-unused-parameter -Wall -Wextra -pedantic" ${CMAKE_C_FLAGS}) + else() + set (CMAKE_BUILD_TYPE "Release") + endif() + + if (WIN32) + set (CMAKE_C_FLAGS "-mwindows ${CMAKE_C_FLAGS}") + target_link_libraries(${PROJECT_NAME} mingw32 m SDL2main SDL2 SDL2_ttf SDL2_image SDL2_mixer) + else() + target_link_libraries(${PROJECT_NAME} m SDL2 SDL2_ttf SDL2_image SDL2_mixer) + endif() +endif() + +message("System: ${CMAKE_SYSTEM_NAME}") +message("Project: ${PROJECT_NAME}") +message("Build: ${CMAKE_BUILD_TYPE}") +message("Flags: ${CMAKE_C_FLAGS}") diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..fe2fe8a --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README.md b/README.md new file mode 100755 index 0000000..e6f0e57 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# "Calamity Cobra in "A Sweet Surprise" + +![1](/screenshots/1.png) + +![2](/screenshots/2.png) + +![3](/screenshots/3.png) + +![4](/screenshots/4.png) + +My entry for the 2022 WeightGaming Gain Jam. A top down arcade shooter with some fetishy elements. + +## Build + +### Dependencies + + - SDL2 + - SDL_image + - SDL_mixer + - SDL_ttf + - emscripten (Web only) + +### Linux + +This repository uses CMake to compile, so: + +`mkdir build` + +`cd build` + +`cmake ..` + +`make` + +### Windows + +I used MinGW for the Windows build, so basically the same as Linux but you'll need to use MSYS, MinGW, etc. to compile it. + +### Web (Emscripten) + +Make sure you have Emscripten all configured with all the needed ports (see above). + +Then: + +`mkdir build` + +`cd build` + +`emcmake cmake ..` + +`emmake make` + +Make sure you've got the resources folder and that it's placed in the same directory as the executable. You can get that [here](https://shweetz.net/files/games/calamity-cobra-resources-1.2.7z). diff --git a/screenshots/1.png b/screenshots/1.png new file mode 100644 index 0000000..02e9b0c Binary files /dev/null and b/screenshots/1.png differ diff --git a/screenshots/2.png b/screenshots/2.png new file mode 100644 index 0000000..b3e6aa8 Binary files /dev/null and b/screenshots/2.png differ diff --git a/screenshots/3.png b/screenshots/3.png new file mode 100644 index 0000000..a52267b Binary files /dev/null and b/screenshots/3.png differ diff --git a/screenshots/4.png b/screenshots/4.png new file mode 100644 index 0000000..71fb551 Binary files /dev/null and b/screenshots/4.png differ diff --git a/src/COMMON.h b/src/COMMON.h new file mode 100755 index 0000000..1e6d0e6 --- /dev/null +++ b/src/COMMON.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include +#include + +#define FALSE 0 +#define TRUE !FALSE + +#define PI 3.14159265358979323846 +#define SECOND 1000 +#define MINUTE 60000 + +#define RAD_TO_DEG (180 / PI) +#define DEG_TO_RAD (PI / 180) + +#define ROTATION_MAX_DEG 360 +#define ROTATION_MAX_RAD (2 * PI) + +#define FLOOR(x, f) x < f ? f : x +#define CEIL(x, c) x > c ? c : x +#define CLAMP(x, f, c) FLOOR(CEIL(x, c), f) + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +typedef float f32; +typedef double f64; + +typedef u8 bool_u8; diff --git a/src/engine/atlas.c b/src/engine/atlas.c new file mode 100755 index 0000000..18ba375 --- /dev/null +++ b/src/engine/atlas.c @@ -0,0 +1,43 @@ +#include "atlas.h" + +/* + * Initializes a given atlas, provided a + * texture, rows, columns, each frame's width, and + * each frame's height. +*/ +void +atlas_init(Atlas* _a, SDL_Texture* _t, s32 _r, s32 _c, s32 _fW, s32 _fH) +{ + memset(_a, '\0', sizeof(Atlas)); + + _a->texture = _t; + _a->rows = _r; + _a->cols = _c; + _a->frameW = _fW; + _a->frameH = _fH; +} + +/* + * Returns the source rectangle of an atlas provided a frame; + * where it's currently "focused" on. +*/ +SDL_Rect +atlas_source_rect_get(Atlas* _a, s32 _f) +{ + SDL_Rect r; + + r.w = _a->frameW; + r.h = _a->frameH; + + if (_a->cols == 1) + r.x = 0; + else + r.x = (_f % _a->cols) * _a->frameW; + + if (_a->rows == 1) + r.y = 0; + else + r.y = (_f / _a->rows) * _a->frameH; + + return r; +} diff --git a/src/engine/atlas.h b/src/engine/atlas.h new file mode 100755 index 0000000..78c8010 --- /dev/null +++ b/src/engine/atlas.h @@ -0,0 +1,18 @@ +#pragma once + +#include "texture.h" + +#define ATLAS_ROWS_DEFAULT 1 +#define ATLAS_COLUMNS_DEFAULT 1 + +typedef struct Atlas +{ + SDL_Texture* texture; + s32 rows; + s32 cols; + s32 frameW; + s32 frameH; +} Atlas; + +void atlas_init(Atlas* _a, SDL_Texture* _t, s32 _r, s32 _c, s32 _fW, s32 _fH); +SDL_Rect atlas_source_rect_get(Atlas* _a, s32 _f); diff --git a/src/engine/circle.c b/src/engine/circle.c new file mode 100755 index 0000000..1c25940 --- /dev/null +++ b/src/engine/circle.c @@ -0,0 +1,28 @@ +/* + * DESCRIPTION: + * Circle code. +*/ + +#include "circle.h" + +/* Are two circles colliding? */ +bool_u8 +circle_collide(Circle* _a, Circle* _b) +{ + f64 sum; + f64 distance; + + distance = math_distance + ( + _a->x, + _a->y, + _b->x, + _b->y + ); + + sum = _a->r + _b->r; + + if (distance < sum) + return TRUE; + return FALSE; +} diff --git a/src/engine/circle.h b/src/engine/circle.h new file mode 100755 index 0000000..312b79e --- /dev/null +++ b/src/engine/circle.h @@ -0,0 +1,18 @@ +/* + * DESCRIPTION: + * Header file for circle.h. +*/ + +#pragma once + +#include "../util/math.h" +#include "../COMMON.h" + +typedef struct Circle +{ + f64 r; + f64 x; + f64 y; +} Circle; + +bool_u8 circle_collide(Circle* _a, Circle* _b); diff --git a/src/engine/event.c b/src/engine/event.c new file mode 100755 index 0000000..0dd6946 --- /dev/null +++ b/src/engine/event.c @@ -0,0 +1,44 @@ +#include "event.h" + +/* Updates events. */ +void +event_update(Event* _e) +{ + SDL_Event event; + + memcpy(&_e->prev, &_e->curr, EVENT_MAX); + memset(&_e->curr, '\0', EVENT_MAX); + + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + _e->curr[EVENT_QUIT] = TRUE; + break; + default: + break; + } + } +} + +/* Is a given event pressed? */ +bool_u8 +event_press(Event* _e, EventType _t) +{ + return (_e->curr[_t] && !_e->prev[_t]); +} + +/* Is a given event held? */ +bool_u8 +event_held(Event* _e, EventType _t) +{ + return (_e->curr[_t] && _e->prev[_t]); +} + +/* Is a given event released? */ +bool_u8 +event_release(Event* _e, EventType _t) +{ + return (!_e->curr[_t] && _e->prev[_t]); +} diff --git a/src/engine/event.h b/src/engine/event.h new file mode 100755 index 0000000..49aa088 --- /dev/null +++ b/src/engine/event.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "../COMMON.h" + +#define EVENT_MAX 2 + +typedef enum EventType +{ + EVENT_NONE, + EVENT_QUIT +} EventType; + +typedef struct Event +{ + bool_u8 curr[EVENT_MAX]; + bool_u8 prev[EVENT_MAX]; +} Event; + +void event_update(Event* _e); +bool_u8 event_press(Event* _e, EventType _t); +bool_u8 event_held(Event* _e, EventType _t); +bool_u8 event_release(Event* _e, EventType _t); diff --git a/src/engine/font.c b/src/engine/font.c new file mode 100755 index 0000000..d81aae1 --- /dev/null +++ b/src/engine/font.c @@ -0,0 +1,15 @@ +#include "font.h" + +/* Loads a given font, with a filepath and a font size specified. */ +void +font_init(TTF_Font** _x, char* _p, s32 _s) +{ + *_x = TTF_OpenFont(_p, _s); +} + +/* Frees a given font. */ +void +font_free(TTF_Font* _x) +{ + TTF_CloseFont(_x); +} diff --git a/src/engine/font.h b/src/engine/font.h new file mode 100755 index 0000000..541e11c --- /dev/null +++ b/src/engine/font.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +#include "../COMMON.h" + +void font_init(TTF_Font** _x, char* _p, s32 _s); +void font_free(TTF_Font* _x); diff --git a/src/engine/keyboard.c b/src/engine/keyboard.c new file mode 100755 index 0000000..6539a4f --- /dev/null +++ b/src/engine/keyboard.c @@ -0,0 +1,36 @@ +#include "keyboard.h" + +/* Updates the keyboard. */ +void +keyboard_update(Keyboard* _k) +{ + const u8* state; + + memcpy(&_k->prev, &_k->curr, KEYBOARD_MAX); + memset(&_k->curr, '\0', KEYBOARD_MAX); + + state = SDL_GetKeyboardState(NULL); + + memcpy(&_k->curr, state, KEYBOARD_MAX); +} + +/* Is the given key pressed? */ +bool_u8 +keyboard_press(Keyboard* _k, KeyboardType _t) +{ + return (_k->curr[_t] && !_k->prev[_t]); +} + +/* Is the given key held? */ +bool_u8 +keyboard_held(Keyboard* _k, KeyboardType _t) +{ + return (_k->curr[_t] && _k->prev[_t]); +} + +/* Is the given key released? */ +bool_u8 +keyboard_release(Keyboard* _k, KeyboardType _t) +{ + return (!_k->curr[_t] && _k->prev[_t]); +} diff --git a/src/engine/keyboard.h b/src/engine/keyboard.h new file mode 100755 index 0000000..3cfcad2 --- /dev/null +++ b/src/engine/keyboard.h @@ -0,0 +1,241 @@ +#pragma once + +#include + +#include "../COMMON.h" + +#define KEYBOARD_MAX 0xFF + +typedef enum KeyboardType +{ + KEYBOARD_UNKNOWN = 0, + KEYBOARD_UNKNOWN_TWO = 1, + KEYBOARD_UNKNOWN_THREE = 2, + KEYBOARD_UNKNOWN_FOUR = 3, + KEYBOARD_A = 4, + KEYBOARD_B = 5, + KEYBOARD_C = 6, + KEYBOARD_D = 7, + KEYBOARD_E = 8, + KEYBOARD_F = 9, + KEYBOARD_G = 10, + KEYBOARD_H = 11, + KEYBOARD_I = 12, + KEYBOARD_J = 13, + KEYBOARD_K = 14, + KEYBOARD_L = 15, + KEYBOARD_M = 16, + KEYBOARD_N = 17, + KEYBOARD_O = 18, + KEYBOARD_P = 19, + KEYBOARD_Q = 20, + KEYBOARD_R = 21, + KEYBOARD_S = 22, + KEYBOARD_T = 23, + KEYBOARD_U = 24, + KEYBOARD_V = 25, + KEYBOARD_W = 26, + KEYBOARD_X = 27, + KEYBOARD_Y = 28, + KEYBOARD_Z = 29, + KEYBOARD_1 = 30, + KEYBOARD_2 = 31, + KEYBOARD_3 = 32, + KEYBOARD_4 = 33, + KEYBOARD_5 = 34, + KEYBOARD_6 = 35, + KEYBOARD_7 = 36, + KEYBOARD_8 = 37, + KEYBOARD_9 = 38, + KEYBOARD_0 = 39, + KEYBOARD_RETURN = 40, + KEYBOARD_ESCAPE = 41, + KEYBOARD_BACKSPACE = 42, + KEYBOARD_TAB = 43, + KEYBOARD_SPACE = 44, + KEYBOARD_MINUS = 45, + KEYBOARD_EQUALS = 46, + KEYBOARD_LEFTBRACKET = 47, + KEYBOARD_RIGHTBRACKET = 48, + KEYBOARD_BACKSLASH = 49, + KEYBOARD_NONUSHASH = 50, + KEYBOARD_SEMICOLON = 51, + KEYBOARD_APOSTROPHE = 52, + KEYBOARD_GRAVE = 53, + KEYBOARD_COMMA = 54, + KEYBOARD_PERIOD = 55, + KEYBOARD_SLASH = 56, + KEYBOARD_CAPSLOCK = 57, + KEYBOARD_F1 = 58, + KEYBOARD_F2 = 59, + KEYBOARD_F3 = 60, + KEYBOARD_F4 = 61, + KEYBOARD_F5 = 62, + KEYBOARD_F6 = 63, + KEYBOARD_F7 = 64, + KEYBOARD_F8 = 65, + KEYBOARD_F9 = 66, + KEYBOARD_F10 = 67, + KEYBOARD_F11 = 68, + KEYBOARD_F12 = 69, + KEYBOARD_PRINTSCREEN = 70, + KEYBOARD_SCROLLLOCK = 71, + KEYBOARD_PAUSE = 72, + KEYBOARD_INSERT = 73, + KEYBOARD_HOME = 74, + KEYBOARD_PAGEUP = 75, + KEYBOARD_DELETE = 76, + KEYBOARD_END = 77, + KEYBOARD_PAGEDOWN = 78, + KEYBOARD_RIGHT = 79, + KEYBOARD_LEFT = 80, + KEYBOARD_DOWN = 81, + KEYBOARD_UP = 82, + KEYBOARD_NUMLOCKCLEAR = 83, + KEYBOARD_KP_DIVIDE = 84, + KEYBOARD_KP_MULTIPLY = 85, + KEYBOARD_KP_MINUS = 86, + KEYBOARD_KP_PLUS = 87, + KEYBOARD_KP_ENTER = 88, + KEYBOARD_KP_1 = 89, + KEYBOARD_KP_2 = 90, + KEYBOARD_KP_3 = 91, + KEYBOARD_KP_4 = 92, + KEYBOARD_KP_5 = 93, + KEYBOARD_KP_6 = 94, + KEYBOARD_KP_7 = 95, + KEYBOARD_KP_8 = 96, + KEYBOARD_KP_9 = 97, + KEYBOARD_KP_0 = 98, + KEYBOARD_KP_PERIOD = 99, + KEYBOARD_NONUSBACKSLASH = 100, + KEYBOARD_APPLICATION = 101, + KEYBOARD_POWER = 102, + KEYBOARD_KP_EQUALS = 103, + KEYBOARD_F13 = 104, + KEYBOARD_F14 = 105, + KEYBOARD_F15 = 106, + KEYBOARD_F16 = 107, + KEYBOARD_F17 = 108, + KEYBOARD_F18 = 109, + KEYBOARD_F19 = 110, + KEYBOARD_F20 = 111, + KEYBOARD_F21 = 112, + KEYBOARD_F22 = 113, + KEYBOARD_F23 = 114, + KEYBOARD_F24 = 115, + KEYBOARD_EXECUTE = 116, + KEYBOARD_HELP = 117, + KEYBOARD_MENU = 118, + KEYBOARD_SELECT = 119, + KEYBOARD_STOP = 120, + KEYBOARD_AGAIN = 121, + KEYBOARD_UNDO = 122, + KEYBOARD_CUT = 123, + KEYBOARD_COPY = 124, + KEYBOARD_PASTE = 125, + KEYBOARD_FIND = 126, + KEYBOARD_MUTE = 127, + KEYBOARD_VOLUMEUP = 128, + KEYBOARD_VOLUMEDOWN = 129, + KEYBOARD_LOCKINGCAPSLOCK = 130, + KEYBOARD_LOCKINGNUMLOCK = 131, + KEYBOARD_LOCKINGSCROLLLOCK = 132, + KEYBOARD_KP_COMMA = 133, + KEYBOARD_KP_EQUALSAS400 = 134, + KEYBOARD_INTERNATIONAL1 = 135, + KEYBOARD_INTERNATIONAL2 = 136, + KEYBOARD_INTERNATIONAL3 = 137, + KEYBOARD_INTERNATIONAL4 = 138, + KEYBOARD_INTERNATIONAL5 = 139, + KEYBOARD_INTERNATIONAL6 = 140, + KEYBOARD_INTERNATIONAL7 = 141, + KEYBOARD_INTERNATIONAL8 = 142, + KEYBOARD_INTERNATIONAL9 = 143, + KEYBOARD_LANG1 = 144, + KEYBOARD_LANG2 = 145, + KEYBOARD_LANG3 = 146, + KEYBOARD_LANG4 = 147, + KEYBOARD_LANG5 = 148, + KEYBOARD_LANG6 = 149, + KEYBOARD_LANG7 = 150, + KEYBOARD_LANG8 = 151, + KEYBOARD_LANG9 = 152, + KEYBOARD_ALTERASE = 153, + KEYBOARD_SYSREQ = 154, + KEYBOARD_CANCEL = 155, + KEYBOARD_CLEAR = 156, + KEYBOARD_PRIOR = 157, + KEYBOARD_RETURN2 = 158, + KEYBOARD_SEPARATOR = 159, + KEYBOARD_OUT = 160, + KEYBOARD_OPER = 161, + KEYBOARD_CLEARAGAIN = 162, + KEYBOARD_CRSEL = 163, + KEYBOARD_EXSEL = 164, + KEYBOARD_KP_00 = 176, + KEYBOARD_KP_000 = 177, + KEYBOARD_THOUSANDSSEPARATOR = 178, + KEYBOARD_DECIMALSEPARATOR = 179, + KEYBOARD_CURRENCYUNIT = 180, + KEYBOARD_CURRENCYSUBUNIT = 181, + KEYBOARD_KP_LEFTPAREN = 182, + KEYBOARD_KP_RIGHTPAREN = 183, + KEYBOARD_KP_LEFTBRACE = 184, + KEYBOARD_KP_RIGHTBRACE = 185, + KEYBOARD_KP_TAB = 186, + KEYBOARD_KP_BACKSPACE = 187, + KEYBOARD_KP_A = 188, + KEYBOARD_KP_B = 189, + KEYBOARD_KP_C = 190, + KEYBOARD_KP_D = 191, + KEYBOARD_KP_E = 192, + KEYBOARD_KP_F = 193, + KEYBOARD_KP_XOR = 194, + KEYBOARD_KP_POWER = 195, + KEYBOARD_KP_PERCENT = 196, + KEYBOARD_KP_LESS = 197, + KEYBOARD_KP_GREATER = 198, + KEYBOARD_KP_AMPERSAND = 199, + KEYBOARD_KP_DBLAMPERSAND = 200, + KEYBOARD_KP_VERTICALBAR = 201, + KEYBOARD_KP_DBLVERTICALBAR = 202, + KEYBOARD_KP_COLON = 203, + KEYBOARD_KP_HASH = 204, + KEYBOARD_KP_SPACE = 205, + KEYBOARD_KP_AT = 206, + KEYBOARD_KP_EXCLAM = 207, + KEYBOARD_KP_MEMSTORE = 208, + KEYBOARD_KP_MEMRECALL = 209, + KEYBOARD_KP_MEMCLEAR = 210, + KEYBOARD_KP_MEMADD = 211, + KEYBOARD_KP_MEMSUBTRACT = 212, + KEYBOARD_KP_MEMMULTIPLY = 213, + KEYBOARD_KP_MEMDIVIDE = 214, + KEYBOARD_KP_PLUSMINUS = 215, + KEYBOARD_KP_CLEAR = 216, + KEYBOARD_KP_CLEARENTRY = 217, + KEYBOARD_KP_BINARY = 218, + KEYBOARD_KP_OCTAL = 219, + KEYBOARD_KP_DECIMAL = 220, + KEYBOARD_KP_HEXADECIMAL = 221, + KEYBOARD_LCTRL = 224, + KEYBOARD_LSHIFT = 225, + KEYBOARD_LALT = 226, + KEYBOARD_LGUI = 227, + KEYBOARD_RCTRL = 228, + KEYBOARD_RSHIFT = 229, + KEYBOARD_RALT = 230, + KEYBOARD_RGUI = 231 +} KeyboardType; + +typedef struct Keyboard +{ + bool_u8 curr[KEYBOARD_MAX]; + bool_u8 prev[KEYBOARD_MAX]; +} Keyboard; + +void keyboard_update(Keyboard* _k); +bool_u8 keyboard_press(Keyboard* _k, KeyboardType _t); +bool_u8 keyboard_held(Keyboard* _k, KeyboardType _t); +bool_u8 keyboard_release(Keyboard* _k, KeyboardType _t); diff --git a/src/engine/mouse.c b/src/engine/mouse.c new file mode 100755 index 0000000..32d410e --- /dev/null +++ b/src/engine/mouse.c @@ -0,0 +1,45 @@ +#include "mouse.h" + +/* Updates the mouse. */ +void +mouse_update(Mouse* _m) +{ + s32 state; + + memcpy(&_m->prev, &_m->curr, MOUSE_MAX); + memset(&_m->curr, '\0', MOUSE_MAX); + + state = SDL_GetMouseState(NULL, NULL); + + if ((state & SDL_BUTTON_LMASK) != 0) + _m->curr[MOUSE_LEFT] = TRUE; + + if ((state & SDL_BUTTON_MMASK) != 0) + _m->curr[MOUSE_MIDDLE] = TRUE; + + if ((state & SDL_BUTTON_RMASK) != 0) + _m->curr[MOUSE_RIGHT] = TRUE; + + SDL_GetMouseState(&_m->x, &_m->y); +} + +/* Is a given mouse button pressed? */ +bool_u8 +mouse_press(Mouse* _m, MouseType _t) +{ + return (_m->curr[_t] && !_m->prev[_t]); +} + +/* Is a given mouse button held? */ +bool_u8 +mouse_held(Mouse* _m, MouseType _t) +{ + return (_m->curr[_t] && _m->prev[_t]); +} + +/* Is a given mouse button released? */ +bool_u8 +mouse_release(Mouse* _m, MouseType _t) +{ + return (!_m->curr[_t] && _m->prev[_t]); +} diff --git a/src/engine/mouse.h b/src/engine/mouse.h new file mode 100755 index 0000000..052eda0 --- /dev/null +++ b/src/engine/mouse.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "../COMMON.h" + +#define MOUSE_MAX 4 + +typedef enum MouseType +{ + MOUSE_NONE, + MOUSE_LEFT, + MOUSE_MIDDLE, + MOUSE_RIGHT +} MouseType; + +typedef struct Mouse +{ + s32 x; + s32 y; + bool_u8 curr[MOUSE_MAX]; + bool_u8 prev[MOUSE_MAX]; +} Mouse; + +void mouse_update(Mouse* _m); +bool_u8 mouse_press(Mouse* _m, MouseType _t); +bool_u8 mouse_held(Mouse* _m, MouseType _t); +bool_u8 mouse_release(Mouse* _m, MouseType _t); diff --git a/src/engine/music.c b/src/engine/music.c new file mode 100755 index 0000000..1651df7 --- /dev/null +++ b/src/engine/music.c @@ -0,0 +1,39 @@ +#include "music.h" + +/* Returns music from a filepath. */ +void +music_init(Mix_Music** _x, char* _p) +{ + *_x = Mix_LoadMUS(_p); +} + +/* Frees given music. */ +void +music_free(Mix_Music* _x) +{ + Mix_FreeMusic(_x); +} + +/* Plays a given music. */ +void +music_play(Mix_Music* _x, bool_u8 _l) +{ + if (_l) + Mix_PlayMusic(_x, -1); + else + Mix_PlayMusic(_x, 0); +} + +/* Stops whatever music is playing. */ +void +music_stop(void) +{ + Mix_HaltMusic(); +} + +/* Is music currently playing? */ +bool_u8 +music_playing(void) +{ + return Mix_PlayingMusic(); +} diff --git a/src/engine/music.h b/src/engine/music.h new file mode 100755 index 0000000..ea9c551 --- /dev/null +++ b/src/engine/music.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include "../COMMON.h" + + +void music_init(Mix_Music** _x, char* _path); +void music_free(Mix_Music* _x); +void music_play(Mix_Music* _x, bool_u8 _l); +void music_stop(void); +bool_u8 music_playing(void); diff --git a/src/engine/rectangle.c b/src/engine/rectangle.c new file mode 100755 index 0000000..46c71a8 --- /dev/null +++ b/src/engine/rectangle.c @@ -0,0 +1,30 @@ +#include "rectangle.h" + +/* Draws a rectangle onto a renderer, given a rectangle and color. */ +void +rectangle_draw(SDL_Rect* _x, SDL_Renderer* _r, SDL_Color _c) +{ + SDL_Color c; + + c = renderer_color_get(_r); + + renderer_color_set(_r, _c); + + SDL_RenderFillRect(_r, _x); + + renderer_color_set(_r, c); +} + +/* Is a point colliding with a rectangle? */ +bool_u8 +rectangle_point_collide(SDL_Rect* _r, f64 _x, f64 _y) +{ + SDL_Point p; + + p.x = _x; + p.y = _y; + + if (SDL_PointInRect(&p, _r)) + return TRUE; + return FALSE; +} diff --git a/src/engine/rectangle.h b/src/engine/rectangle.h new file mode 100755 index 0000000..29476c2 --- /dev/null +++ b/src/engine/rectangle.h @@ -0,0 +1,6 @@ +#pragma once + +#include "renderer.h" + +void rectangle_draw(SDL_Rect* _x, SDL_Renderer* _renderer, SDL_Color _c); +bool_u8 rectangle_point_collide(SDL_Rect* _r, f64 _x, f64 _y); diff --git a/src/engine/renderer.c b/src/engine/renderer.c new file mode 100755 index 0000000..f41d3c0 --- /dev/null +++ b/src/engine/renderer.c @@ -0,0 +1,88 @@ +#include "renderer.h" + +/* Initializes a renderer, given a window and renderer flags. */ +void +renderer_init(SDL_Renderer** _x, SDL_Window* _w, s32 _f) +{ + *_x = SDL_CreateRenderer + ( + _w, + -1, + _f + ); + + SDL_SetRenderDrawBlendMode(*_x, SDL_BLENDMODE_BLEND); +} + +/* Frees a given renderer. */ +void +renderer_free(SDL_Renderer* _x) +{ + SDL_DestroyRenderer(_x); +} + +/* Clears the renderer. */ +void +renderer_clear(SDL_Renderer* _x) +{ + SDL_RenderClear(_x); +} + +/* Presents the renderer to the screen. */ +void +renderer_present(SDL_Renderer* _x) +{ + SDL_RenderPresent(_x); +} + +/* Gets the renderer's current draw color. */ +SDL_Color +renderer_color_get(SDL_Renderer* _r) +{ + SDL_Color c; + + memset(&c, '\0', sizeof(SDL_Color)); + + SDL_GetRenderDrawColor + ( + _r, + &c.r, + &c.g, + &c.b, + &c.a + ); + + return c; +} + +/* Sets the renderer's current draw color. */ +void +renderer_color_set(SDL_Renderer* _x, SDL_Color _c) +{ + SDL_SetRenderDrawColor + ( + _x, + _c.r, + _c.g, + _c.b, + _c.a + ); +} + +/* Set's the renderer's viewport; e.g., where it's going to be drawing to. */ +void +renderer_viewport_set(SDL_Renderer* _x, SDL_Rect _r) +{ + SDL_RenderSetViewport(_x, &_r); +} + +/* Sets the renderer's viewport to the window's. */ +void +renderer_viewport_window_set(SDL_Renderer* _x, SDL_Window* _w) +{ + SDL_Rect r; + + r = window_rect_get(_w); + + renderer_viewport_set(_x, r); +} diff --git a/src/engine/renderer.h b/src/engine/renderer.h new file mode 100755 index 0000000..0c9f26f --- /dev/null +++ b/src/engine/renderer.h @@ -0,0 +1,12 @@ +#pragma once + +#include "window.h" + +SDL_Color renderer_color_get(SDL_Renderer* _r); +void renderer_clear(SDL_Renderer* _x); +void renderer_color_set(SDL_Renderer* _x, SDL_Color _c); +void renderer_free(SDL_Renderer* _x); +void renderer_init(SDL_Renderer** _x, SDL_Window* _w, s32 _f); +void renderer_present(SDL_Renderer* _x); +void renderer_viewport_set(SDL_Renderer* _x, SDL_Rect _r); +void renderer_viewport_window_set(SDL_Renderer* _x, SDL_Window* _w); diff --git a/src/engine/sound.c b/src/engine/sound.c new file mode 100755 index 0000000..be584d0 --- /dev/null +++ b/src/engine/sound.c @@ -0,0 +1,22 @@ +#include "sound.h" + +/* Initializes a sound from a given filepath. */ +void +sound_init(Mix_Chunk** _x, char* _path) +{ + *_x = Mix_LoadWAV(_path); +} + +/* Frees a given sound. */ +void +sound_free(Mix_Chunk* _x) +{ + Mix_FreeChunk(_x); +} + +/* Plays a given sound, with its channel specified. */ +void +sound_play(Mix_Chunk* _x, s32 _channel) +{ + Mix_PlayChannel(_channel, _x, 0); +} diff --git a/src/engine/sound.h b/src/engine/sound.h new file mode 100755 index 0000000..cddf250 --- /dev/null +++ b/src/engine/sound.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +#include "../COMMON.h" + +void sound_init(Mix_Chunk** _x, char* _path); +void sound_free(Mix_Chunk* _x); +void sound_play(Mix_Chunk* _x, s32 _channel); diff --git a/src/engine/sprite.c b/src/engine/sprite.c new file mode 100755 index 0000000..de83426 --- /dev/null +++ b/src/engine/sprite.c @@ -0,0 +1,144 @@ +#include "sprite.h" + +/* Gets the sprite's current rectangle. */ +SDL_Rect +sprite_rect_get(Sprite* _x) +{ + SDL_Rect r; + + memset(&r, '\0', sizeof(SDL_Rect)); + + r.w = _x->w * _x->scale * _x->squash; + r.h = _x->h * _x->scale * _x->stretch; + + switch (_x->origin) + { + case ORIGIN_TOP_LEFT: + r.x = _x->x; + r.y = _x->y; + break; + /* FALLTHROUGH */ + case ORIGIN_MIDDLE: + default: + r.x = _x->x - (r.w / 2); + r.y = _x->y - (r.h / 2); + break; + } + + return r; +} + +/* + * Initializes a sprite with an atlas, its origin, + * its width and its height. +*/ +void +sprite_atlas_init(Sprite* _x, Atlas* _a, OriginType _o, s32 _w, s32 _h) +{ + memset(_x, '\0', sizeof(Sprite)); + + _x->atlas = *_a; + _x->origin = _o; + _x->w = _w; + _x->h = _h; + _x->rotation = SPRITE_ROTATION_DEFAULT; + _x->frame = SPRITE_FRAME_DEFAULT; + _x->scale = SPRITE_SCALE_DEFAULT; + _x->squash = SPRITE_SQUASH_DEFAULT; + _x->stretch = SPRITE_STRETCH_DEFAULT; + _x->color = SPRITE_COLOR_DEFAULT; + _x->flip = FLIP_NONE; +} + +/* + * Initializes a sprite, given a texture, an origin, + * its width and its height. +*/ +void +sprite_init(Sprite* _x, SDL_Texture* _t, OriginType _o, s32 _w, s32 _h) +{ + Atlas a; + + atlas_init + ( + &a, + _t, + ATLAS_ROWS_DEFAULT, + ATLAS_COLUMNS_DEFAULT, + _w, + _h + ); + + sprite_atlas_init + ( + _x, + &a, + _o, + _w, + _h + ); +} + +/* Draws the sprite. */ +void +sprite_draw(Sprite* _x, SDL_Renderer* _r) +{ + SDL_Rect sourceRect; + SDL_Rect destRect; + s32 flip; + + destRect = sprite_rect_get(_x); + + if (_x->atlas.rows == 1 && _x->atlas.cols == 1) + { + sourceRect.x = 0; + sourceRect.y = 0; + sourceRect.w = texture_w_get(_x->atlas.texture); + sourceRect.h = texture_h_get(_x->atlas.texture); + } + else + sourceRect = atlas_source_rect_get(&_x->atlas, _x->frame); + + switch (_x->flip) + { + case FLIP_HORIZONTAL: + flip = SDL_FLIP_HORIZONTAL; + break; + case FLIP_VERTICAL: + flip = SDL_FLIP_VERTICAL; + break; + case FLIP_HORIZONTAL_VERTICAL: + flip = SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL; + break; + /* FALLTHROUGH */ + case FLIP_NONE: + default: + flip = SDL_FLIP_NONE; + break; + } + + SDL_SetTextureAlphaMod + ( + _x->atlas.texture, + _x->color.a + ); + + SDL_SetTextureColorMod + ( + _x->atlas.texture, + _x->color.r, + _x->color.g, + _x->color.b + ); + + SDL_RenderCopyEx + ( + _r, + _x->atlas.texture, + &sourceRect, + &destRect, + _x->rotation, + NULL, + flip + ); +} diff --git a/src/engine/sprite.h b/src/engine/sprite.h new file mode 100755 index 0000000..50255fd --- /dev/null +++ b/src/engine/sprite.h @@ -0,0 +1,49 @@ +#pragma once + +#include "atlas.h" + +#define SPRITE_SCALE_DEFAULT 1.0 +#define SPRITE_SQUASH_DEFAULT 1.0 +#define SPRITE_STRETCH_DEFAULT 1.0 +#define SPRITE_FRAME_DEFAULT 0 +#define SPRITE_ROTATION_DEFAULT 0.0 + +typedef enum OriginType +{ + ORIGIN_NONE, + ORIGIN_TOP_LEFT, + ORIGIN_MIDDLE +} OriginType; + +typedef enum FlipType +{ + FLIP_NONE, + FLIP_HORIZONTAL, + FLIP_VERTICAL, + FLIP_HORIZONTAL_VERTICAL +} FlipType; + +typedef struct Sprite +{ + FlipType flip; + OriginType origin; + f64 rotation; + f64 scale; + f64 squash; + f64 stretch; + f64 x; + f64 y; + s32 frame; + s32 h; + s32 w; + Atlas atlas; + SDL_Color color; +} Sprite; + +static const SDL_Color SPRITE_COLOR_DEFAULT = {255, 255, 255, 255}; + +SDL_Rect sprite_rect_get(Sprite* _x); +void sprite_atlas_init(Sprite* _x, Atlas* _a, OriginType _o, s32 _w, s32 _h); +void sprite_init(Sprite* _x, SDL_Texture* _t, OriginType _o, s32 _w, s32 _h); +void sprite_draw(Sprite* _x, SDL_Renderer* _r); +void sprite_free(Sprite* _x); diff --git a/src/engine/text.c b/src/engine/text.c new file mode 100755 index 0000000..65ccae4 --- /dev/null +++ b/src/engine/text.c @@ -0,0 +1,100 @@ +#include "text.h" + +static void text_surface_init(SDL_Surface** _x, SDL_Renderer* _r, TTF_Font* _f, SDL_Color _c, char* _s, s32 _w); +static void text_texture_init(SDL_Texture** _x, SDL_Renderer* _r, TTF_Font* _f, SDL_Color _c, char* _s, s32 _w); + +/* + * Initializes a SDL_Surface with text, given + * a renderer, a font, a color, a string, + * and a text wrap width. +*/ +static void +text_surface_init +( + SDL_Surface** _x, + SDL_Renderer* _r, + TTF_Font* _f, + SDL_Color _c, + char* _s, + s32 _w +) +{ + *_x = TTF_RenderText_Blended_Wrapped + ( + _f, + _s, + _c, + _w + ); +} + +/* + * Initializes a SDL_Texture with text, + * given a renderer, a font, a color, a string, and a + * text wrap width. +*/ +static void +text_texture_init +( + SDL_Texture** _x, + SDL_Renderer* _r, + TTF_Font* _f, + SDL_Color _c, + char* _s, + s32 _w +) +{ + SDL_Surface* s; + + text_surface_init + ( + &s, + _r, + _f, + _c, + _s, + _w + ); + + *_x = SDL_CreateTextureFromSurface(_r, s); + + SDL_FreeSurface(s); +} + +/* Initializes a sprite with text, + * given a renderer, a font, a color, a string, an origin, + * and a text wrap width. +*/ +void +text_sprite_init +( + Sprite* _x, + SDL_Renderer* _r, + TTF_Font* _f, + SDL_Color _c, + OriginType _o, + char* _s, + s32 _w +) +{ + SDL_Texture* t; + + text_texture_init + ( + &t, + _r, + _f, + _c, + _s, + _w + ); + + sprite_init + ( + _x, + t, + _o, + texture_w_get(t), + texture_h_get(t) + ); +} diff --git a/src/engine/text.h b/src/engine/text.h new file mode 100755 index 0000000..9447eae --- /dev/null +++ b/src/engine/text.h @@ -0,0 +1,6 @@ +#pragma once + +#include "font.h" +#include "sprite.h" + +void text_sprite_init(Sprite* _x, SDL_Renderer* _r, TTF_Font* _f, SDL_Color _c, OriginType _o, char* _s, s32 _w); diff --git a/src/engine/texture.c b/src/engine/texture.c new file mode 100755 index 0000000..d6b6bc6 --- /dev/null +++ b/src/engine/texture.c @@ -0,0 +1,37 @@ +#include "texture.h" + +/* Initializes a texture, given a renderer and a path. */ +void +texture_init(SDL_Texture** _x, SDL_Renderer* _r, char* _p) +{ + *_x = IMG_LoadTexture(_r, _p); +} + +/* Frees a given texture. */ +void +texture_free(SDL_Texture* _x) +{ + SDL_DestroyTexture(_x); +} + +/* Returns a texture's width. */ +s32 +texture_w_get(SDL_Texture* _x) +{ + s32 w; + + SDL_QueryTexture(_x, NULL, NULL, &w, NULL); + + return w; +} + +/* Returns a texture's height. */ +s32 +texture_h_get(SDL_Texture* _x) +{ + s32 h; + + SDL_QueryTexture(_x, NULL, NULL, NULL, &h); + + return h; +} diff --git a/src/engine/texture.h b/src/engine/texture.h new file mode 100755 index 0000000..ccf683f --- /dev/null +++ b/src/engine/texture.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +#include "../COMMON.h" + +void texture_init(SDL_Texture** _x, SDL_Renderer* _r, char* _p); +void texture_free(SDL_Texture* _x); +s32 texture_w_get(SDL_Texture* _x); +s32 texture_h_get(SDL_Texture* _x); diff --git a/src/engine/tick.c b/src/engine/tick.c new file mode 100755 index 0000000..6e1f018 --- /dev/null +++ b/src/engine/tick.c @@ -0,0 +1,11 @@ +#include "tick.h" + +/* Update ticks. */ +void +tick_update(Tick* _x) +{ + _x->prev = _x->curr; + _x->curr = SDL_GetTicks64(); + _x->delta = _x->curr - _x->prev; + _x->cum += _x->delta; +} diff --git a/src/engine/tick.h b/src/engine/tick.h new file mode 100755 index 0000000..ca8ff61 --- /dev/null +++ b/src/engine/tick.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "../COMMON.h" + +typedef struct Tick +{ + u64 curr; + u64 prev; + u64 delta; + u64 cum; +} Tick; + +void tick_update(Tick* _x); diff --git a/src/engine/window.c b/src/engine/window.c new file mode 100755 index 0000000..568cc50 --- /dev/null +++ b/src/engine/window.c @@ -0,0 +1,85 @@ +#include "window.h" + +/* + * Initializes a window, given a window title, + * a width, a height, and window flags. +*/ +void +window_init(SDL_Window** _x, char* _s, s32 _w, s32 _h, s32 _f) +{ + *_x = SDL_CreateWindow + ( + _s, + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + _w, + _h, + _f + ); +} + +/* Frees a given window. */ +void +window_free(SDL_Window* _x) +{ + SDL_DestroyWindow(_x); +} + +/* Gets the window's width. */ +s32 +window_w_get(SDL_Window* _x) +{ + s32 w; + + SDL_GetWindowSize(_x, &w, NULL); + + return w; +} + +/* Get's the window's height. */ +s32 +window_h_get(SDL_Window* _x) +{ + s32 h; + + SDL_GetWindowSize(_x, NULL, &h); + + return h; +} + +/* Gets the window's rectangle. */ +SDL_Rect +window_rect_get(SDL_Window* _x) +{ + SDL_Rect r; + + memset(&r, '\0', sizeof(SDL_Rect)); + + r.x = 0; + r.y = 0; + r.w = window_w_get(_x); + r.h = window_h_get(_x); + + return r; +} + +/* Sets video mode to fullscreen. */ +void +window_fullscreen_enter(SDL_Window* _x) +{ + SDL_SetWindowFullscreen(_x, SDL_WINDOW_FULLSCREEN); +} + +/* Entering fake fullscreen (desktop). */ +void +window_fullscreen_desktop_enter(SDL_Window* _x) +{ + SDL_SetWindowFullscreen(_x, SDL_WINDOW_FULLSCREEN_DESKTOP); +} + +/* Exiting fullscreen. */ +void +window_fullscreen_exit(SDL_Window* _x) +{ + SDL_SetWindowFullscreen(_x, 0); +} diff --git a/src/engine/window.h b/src/engine/window.h new file mode 100755 index 0000000..61de8f6 --- /dev/null +++ b/src/engine/window.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#include "../COMMON.h" + +void window_init(SDL_Window** _x, char* _s, s32 _w, s32 _h, s32 _f); +void window_free(SDL_Window* _x); +void window_fullscreen_enter(SDL_Window* _x); +void window_fullscreen_desktop_enter(SDL_Window* _x); +void window_fullscreen_exit(SDL_Window* _x); +s32 window_w_get(SDL_Window* _x); +s32 window_h_get(SDL_Window* _x); +SDL_Rect window_rect_get(SDL_Window* _x); diff --git a/src/game/GAME_COMMON.h b/src/game/GAME_COMMON.h new file mode 100755 index 0000000..490ed54 --- /dev/null +++ b/src/game/GAME_COMMON.h @@ -0,0 +1,509 @@ +/* + * DESCRIPTION: + * Common file for "game" directory. +*/ + +#pragma once + +#include "../engine/circle.h" +#include "../engine/event.h" +#include "../engine/font.h" +#include "../engine/keyboard.h" +#include "../engine/mouse.h" +#include "../engine/music.h" +#include "../engine/rectangle.h" +#include "../engine/renderer.h" +#include "../engine/sound.h" +#include "../engine/text.h" +#include "../engine/tick.h" +#include "../engine/window.h" +#include "../util/list.h" +#include "../util/math.h" +#include "../util/random.h" + +#define TEXTURE_COUNT 46 +#define SOUND_COUNT 25 +#define FONT_COUNT 6 +#define MUSIC_COUNT 8 + +#define PLAYER_FATNESS_MAX 3 +#define PLAYER_FATNESS_THRESHOLD 2 + +typedef enum TextureType +{ + TEXTURE_NONE, + TEXTURE_LOGO, + TEXTURE_GUI_CURSOR, + TEXTURE_GUI_SNAKE, + TEXTURE_GUI_REDIRECT, + TEXTURE_GUI_CASH, + TEXTURE_GUI_UPGRADE_SNAKE, + TEXTURE_GUI_UPGRADE_LIFETIME, + TEXTURE_GUI_UPGRADE_REDIRECT, + TEXTURE_GUI_SIGN_SMALL, + TEXTURE_GUI_SIGN_BIG, + TEXTURE_SHADOW, + TEXTURE_ARC, + TEXTURE_PLAY_BG, + TEXTURE_PLAYER_STAGE_ONE, + TEXTURE_PLAYER_STAGE_TWO, + TEXTURE_PLAYER_STAGE_THREE, + TEXTURE_PLAYER_TAIL_STAGE_ONE, + TEXTURE_PLAYER_TAIL_STAGE_TWO, + TEXTURE_PLAYER_TAIL_STAGE_THREE, + TEXTURE_PLAYER_SNAKE, + TEXTURE_ENEMY_CUPPER, + TEXTURE_ENEMY_CHIPPER, + TEXTURE_ENEMY_CHIP, + TEXTURE_ENEMY_CRUMBLER, + TEXTURE_COLLECTIBLE_DOLLARS, + TEXTURE_COLLECTIBLE_SACK, + TEXTURE_COLLECTIBLE_BULLIONS, + TEXTURE_COLLECTIBLE_MIRACLE_CURE, + TEXTURE_COLLECTIBLE_DYNAMITE, + TEXTURE_COLLECTIBLE_BERSERK, + TEXTURE_INFO_THEME, + TEXTURE_INFO_WILDCARD_ADMIN, + TEXTURE_INFO_WILDCARD_PATREON, + TEXTURE_INFO_WILDCARD_COMMUNITY, + TEXTURE_INTRO_SCENE_ONE, + TEXTURE_INTRO_SCENE_TWO, + TEXTURE_INTRO_SCENE_THREE, + TEXTURE_INTRO_SCENE_FOUR, + TEXTURE_INTRO_SCENE_FIVE, + TEXTURE_ENDING_FAIL, + TEXTURE_ENDING_ONE, + TEXTURE_ENDING_TWO, + TEXTURE_ENDING_THREE, + TEXTURE_TITLE_BACKGROUND, + TEXTURE_RESULTS, +} TextureType; + +typedef enum SoundType +{ + SOUND_NONE, + SOUND_MENU, + SOUND_BLIP, + SOUND_SNAKE_FIRE, + SOUND_SNAKE_REDIRECT, + SOUND_SNAKE_RETURN, + SOUND_ENEMY_HURT, + SOUND_CUPPER_KILL, + SOUND_CHIPPER_KILL, + SOUND_CRUMBLER_KILL, + SOUND_CHIP_KILL, + SOUND_RUMBLE, + SOUND_COLLECTIBLE, + SOUND_DOLLARS, + SOUND_SACK, + SOUND_BULLIONS, + SOUND_PURCHASE, + SOUND_MIRACLE_CURE, + SOUND_DYNAMITE, + SOUND_BERSERK, + SOUND_SWALLOW, + SOUND_GROWTH, + SOUND_ERROR, + SOUND_GO, + SOUND_SPIT, +} SoundType; + +typedef enum MusicType +{ + MUSIC_NONE, + MUSIC_WIND, + MUSIC_PLAY, + MUSIC_WAVE_CLEAR, + MUSIC_WAVE_FAIL, + MUSIC_RESULTS, + MUSIC_TITLE, + MUSIC_INTRO, +} MusicType; + +typedef enum FontType +{ + FONT_NONE, + FONT_TINY, + FONT_SMALL, + FONT_MEDIUM, + FONT_BIG, + FONT_MEGA, +} FontType; + +typedef enum StateType +{ + STATE_NONE, + STATE_TITLE, + STATE_INTRO, + STATE_PLAY, + STATE_INFO, + STATE_ENDING_FAIL, + STATE_ENDING_ONE, + STATE_ENDING_TWO, + STATE_ENDING_THREE, + STATE_RESULTS +} StateType; + +typedef enum EnemyType +{ + ENEMY_NONE, + ENEMY_CUPPER, + ENEMY_CHIPPER, + ENEMY_CRUMBLER, + ENEMY_CHIP +} EnemyType; + +typedef enum CollectibleType +{ + COLLECTIBLE_NONE, + COLLECTIBLE_DOLLARS, + COLLECTIBLE_SACK, + COLLECTIBLE_BULLIONS, + COLLECTIBLE_MIRACLE_CURE, + COLLECTIBLE_DYNAMITE, + COLLECTIBLE_BERSERK +} CollectibleType; + +typedef enum PowerupType +{ + POWERUP_NONE, + +} PowerupType; + +typedef enum Direction +{ + DIRECTION_LEFT, + DIRECTION_RIGHT, + DIRECTION_UP, + DIRECTION_DOWN +} Direction; + +typedef enum SnakeFrameType +{ + SNAKE_NONE, + SNAKE_REDIRECT, + SNAKE_RETURN +} SnakeFrameType; + +typedef enum ButtonStateType +{ + BUTTON_STATE_NONE, + BUTTON_STATE_HOVER, + BUTTON_STATE_HOVERING, + BUTTON_STATE_PRESS, + BUTTON_STATE_HELD +} ButtonStateType; + +typedef enum EndingType +{ + ENDING_FAIL, + ENDING_ONE, + ENDING_TWO, + ENDING_THREE +} EndingType; + +typedef struct Game Game; + +typedef struct Button +{ + Sprite sprite; + ButtonStateType state; +} Button; + +typedef struct Cursor +{ + Sprite sprite; +} Cursor; + +typedef struct Snake +{ + Sprite sprite; + Circle shadow; + Sprite arc; + f64 dX; + f64 dY; + s32 damage; + s32 lifetime; + s32 animationTimer; + s32 returnTimer; + s32 redirectCount; + s32 redirectTimer; + bool_u8 isReturning; +} Snake; + +typedef struct Collectible +{ + Sprite sprite; + Circle shadow; + s32 lifetime; + bool_u8 isActive; + s32 activeTimer; + f64 dX; + f64 dY; + CollectibleType type; + bool_u8 isAttached; + Snake* attachedSnake; +} Collectible; + +typedef struct Enemy +{ + EnemyType type; + Sprite sprite; + Circle shadow; + bool_u8 isActing; + bool_u8 isActive; + List snakesHit; /* Snake* */ + List returnSnakesHit; /* Snake* */ + f64 range; + s32 attackTimer; + s32 animationTimer; + s32 lifetime; + s32 hitTimer; + s32 hitTimerMax; + s32 health; + f64 dX; + f64 dY; +} Enemy; + +typedef struct Message +{ + Sprite sprite; + f64 x; + f64 y; + f64 dX; + f64 dY; + f64 dR; + s32 lifetime; +} Message; + +typedef struct Player +{ + Circle shadow; + Direction direction; + List snakes; + Sprite sprite; + Sprite tailSprite; + bool_u8 isAbleToFire; + bool_u8 isAbleToMove; + bool_u8 isBeingFattened; + bool_u8 isCashUpdate; + bool_u8 isBeingDefeated; + bool_u8 isDefeat; + bool_u8 isFatnessUpdate; + bool_u8 isLifetimeUpgradeUpdate; + bool_u8 isPowerUp; + bool_u8 isRedirectUpdate; + bool_u8 isRedirectUpgradeUpdate; + bool_u8 isSnakeUpdate; + bool_u8 isSnakeUpgradeUpdate; + bool_u8 isTailAnimate; + bool_u8 isStationary; + bool_u8 isKeyboardMove; + f64 dX; + f64 dY; + s32 cash; + s32 enemiesKilled; + s32 fatness; + s32 fatnessTimer; + s32 cashCollected; + s32 lifetime; + s32 lifetimeUpgradeLevel; + s32 powerUpTimer; + s32 redirectMax; + s32 redirectTimer; + s32 redirectUpgradeLevel; + s32 redirects; + s32 snakeMax; + s32 snakeReserve; + s32 snakeUpgradeLevel; + s32 defeatTimer; + s32 tailTimer; + s32 waveEnemiesKilled; + s32 redirectRechargeTimer; + s32 redirectUpgradePrice; + s32 snakeUpgradePrice; + s32 lifetimeUpgradePrice; +} Player; + +typedef struct GUI +{ + Cursor cursor; + Sprite snakeSprite; + Sprite snakeText; + Sprite redirectSprite; + Sprite redirectText; + Sprite cashSprite; + Sprite cashText; + Sprite snakeUpgradeText; + Sprite lifetimeUpgradeText; + Sprite redirectUpgradeText; + Sprite upgradeText; + Button snakeUpgradeButton; + Button lifetimeUpgradeButton; + Button redirectUpgradeButton; + Button readyButton; + Sprite waveText; + Sprite snakeUpgradeBackgroundSprite; + Sprite redirectUpgradeBackgroundSprite; + Sprite lifetimeUpgradeBackgroundSprite; + Sprite upgradeBackgroundSprite; + Sprite readyBackgroundSprite; + Sprite cashBackgroundSprite; + Sprite cashTextBackgroundSprite; + Sprite waveBackgroundSprite; + Sprite bountyBackgroundSprite; + Sprite bountyText; +} GUI; + +typedef struct Play +{ + struct Game* g; + Sprite pauseText; + GUI gui; + Player player; + bool_u8 isWaveFailed; + bool_u8 isWaveCleared; + bool_u8 isGameCleared; + bool_u8 isPaused; + bool_u8 isBetweenWaves; + bool_u8 isReady; + bool_u8 isCountdown; + bool_u8 isCountdownComplete; + bool_u8 isWaveClearMusicComplete; + bool_u8 isWaveUpdate; + bool_u8 isUpgradesActive; + bool_u8 isUpgradeUpdate; + bool_u8 isBountyUpdate; + bool_u8 isGameClearing; + bool_u8 isGracePeriod; + bool_u8 isTutorial; + Sprite tutorialText; + Sprite bg; + s32 gameClearTimer; + s32 countdownTimer; + s32 tutorialTimer; + s32 wave; + s32 game; + s32 spawnTimer; + s32 enemyMax; + s32 gracePeriodTimer; + List enemies; /* Enemy */ + List spawnEnemies; /* EnemyType */ + List collectibles; /* Collectible */ + List messages; /* Message */ +} Play; + +typedef struct Title +{ + struct Game* g; + Cursor cursor; + Sprite startBackgroundSprite; + Sprite infoBackgroundSprite; + Sprite backgroundSprite; + Button startButton; + Button infoButton; + bool_u8 isInfoPressed; + bool_u8 isStartPressed; + Sprite logoSprite; +} Title; + +typedef struct Scene +{ + char* text; + TextureType textureType; +} Scene; + +typedef struct SceneHandler +{ + struct Game* g; + Scene* scenes; + Sprite sprite; + Sprite text; + bool_u8 isTextEnd; + bool_u8 isEnd; + s32 updateTimer; + s32 sceneCount; + s32 sceneIndex; + s32 textIndex; + s32 textLength; +} SceneHandler; + +typedef struct Intro +{ + Game* g; + SceneHandler sceneHandler; +} Intro; + +typedef struct Info +{ + struct Game* g; + bool_u8 isBackPressed; + Sprite licenseText; + Sprite themeText; + Sprite themeSprite; + Sprite wildcardAdminSprite; + Sprite wildcardAdminText; + Sprite wildcardPatreonSprite; + Sprite wildcardPatreonText; + Sprite wildcardCommunitySprite; + Sprite wildcardCommunityText; + Sprite backBackgroundSprite; + Button backButton; + Cursor cursor; +} Info; + +typedef struct Ending +{ + Game* g; + SceneHandler sceneHandler; + EndingType type; + Button plusButton; + Button endButton; + bool_u8 isActualEnding; + bool_u8 isNewGamePlus; + bool_u8 isResults; + Cursor cursor; +} Ending; + +typedef struct Results +{ + struct Game* g; + bool_u8 isEnd; + Sprite highestWave; + Sprite cashCollected; + Sprite cashHeld; + Sprite bountiesHunted; + Sprite thanksText; + Sprite resultsText; + Sprite sprite; +} Results; + +typedef struct Game +{ + Ending ending; + Event event; + Info info; + Intro intro; + Keyboard keyboard; + Mix_Chunk* sounds[SOUND_COUNT]; + Mix_Music* music[MUSIC_COUNT]; + Mouse mouse; + Play play; + Results results; + SDL_Renderer* renderer; + SDL_Texture* textures[TEXTURE_COUNT]; + SDL_Window* window; + StateType state; + StateType stateNext; + TTF_Font* fonts[FONT_COUNT]; + Tick tick; + Title title; + bool_u8 isStateTransition; + bool_u8 isStateTransitioning; + bool_u8 isNextStateWillBeInit; + bool_u8 isPrevStateWillBeFreed; + bool_u8 isNextStateResourcesWillBeInit; + bool_u8 isPrevStateResourcesWillBeFreed; + bool_u8 isFullScreen; + s32 stateTimer; +} Game; diff --git a/src/game/GAME_STRINGS.h b/src/game/GAME_STRINGS.h new file mode 100755 index 0000000..f09bb64 --- /dev/null +++ b/src/game/GAME_STRINGS.h @@ -0,0 +1,71 @@ +/* + * DESCRIPTION: + * Game's strings, in English. +*/ + +#pragma once + +#define STRING_WINDOW_TITLE "Calamity Cobra in \"A Sweet Surprise\"" + +#define STRING_PLAY_WAVE "Wave " +#define STRING_PLAY_WAVE_PLUS "+" + +#define STRING_INTRO_SCENE_ONE "On an dry, summer evening in the hottest dunes of the West, a lone silhouette clashes against the gradient of the skyline." +#define STRING_INTRO_SCENE_TWO "Calamity Cobra, rugged sharpshooter and bounty hunter extraordinaire, the fiestiest femme fatale in the region, is on the prowl for a new bounty. Clutching a wanted poster within her claws, her shielded eyes read the page...just one more time. Its message is vague; it says something about some troublesome pests that oddly resemble...baked goods?" +#define STRING_INTRO_SCENE_THREE "Peering out into the distance, Calamity Cobra spies a lonesome wooden sign with nothing but the words \"Dessert Desert\". The place was covered in assorted sweet goods, and giant towers of pudding adorned the horizon. It was just where the poster said those sugary bastards would be!!" +#define STRING_INTRO_SCENE_FOUR "Suddenly, without a moment's notice...herds of those high-calorie vermin burst out into the scene and surround Calamity Cobra!!" +#define STRING_INTRO_SCENE_FIVE "With the quickest draw in all of the land, Calamity Cobra arms herself with her very own Snekgun (patent pending), and prepares to bring justice to all the waistlines those high-calorie miscreants have ruined and to remind those not-so-sweet sweets why her name is CALAMITY COBRA!!" + +#define STRING_ENDING_FAIL "Calamity Cobra had misjudged how vicious the denizens of the Dessert Desert would be. Held captive by a thick, mountainous layer of flab, Calamity Cobra lays immobile in the midst of the cream-covered land, helplessly awaiting further feedings...(Try again!!)" +#define STRING_ENDING_ONE "Having cleared out all the troublesome treats in her sight, Calamity Cobra departs from the cursed cream-covered lands, with a nice haul to boot. She just wishes that she wouldn't have to spend some of it on a new tailor... (Ending 1. Shoot for $50,000 on hand!!)" +#define STRING_ENDING_TWO "After destroying an army's worth of those conniving confections, creamy viscera coated Calamity Cobra's shredded outfit. Absolutely exhausted from her relentless slaughter, Calamity Cobra begins the long trip back to town with her bundles of cash. It's not going to be easy, seeing how she's also got to carry the weight of her sized-up body... (Ending 2. Shoot for $100,000 on hand!)" +#define STRING_ENDING_THREE "Did Cecillia do it for the money? Partly. For the fame, for the glory? Sure. But this was the bounty she'd been looking for her whole life, and it brought her something else that those two could hardly bring: immense pleasure. Having suffered the consequences of her vicious, unrelenting slaughter, Cecillia languished her new enormity with pride. Plucking the lollipop from her mouth, Cecilla decided the reward could wait...enjoying her new size was priority number one. (Best Ending. Congratulations!!!)" + +#define STRING_PLAY_UPGRADE "UPGRADES - Use the dough you've earned and buy shweet upgrades for your Snekgun!!" +#define STRING_PLAY_UPGRADE_SNAKES "SNAKES+ - You can now have more snakes on screen." +#define STRING_PLAY_UPGRADE_LIFETIME "LIFETIME+ - Your snakes will last longer before tiring out." +#define STRING_PLAY_UPGRADE_REDIRECT "REDIRECTS+ - You can now redirect your snakes an additional time, for more damage." + +#define STRING_INFO_LICENSES "Thank you for playing Calamity Cobra in \"A Sweet Surprise\"! 2022 Weightgaming.com Gain Jam. Software by @ShweetMagnet licensed under GNU GPLv3. Art licensed under CC0, by @ShweetMagnet. Sound and music licensed under CC0, by @bugsinplanters. \"Jackwrite\" font licensed under OFL, by Jack Daw from dafont.com. \"Sheet of old paper\" graphic licensed under CC0, by Q-x from opengameart.org" + +#define STRING_INFO_THEME "Theme: \"Greed\"\nThe game centers around a greedy miscreant bounty hunter on the hunt. Money can drop from enemies, and greed can be your downfall if you waltz into a hoard of them to collect some chump change or carelessly fire a snake to collect goodies. You can use said money to pay for shweet upgrades, and the endings certainly highlight Calamity Cobra's avarice." + +#define STRING_INFO_WILDCARD_ADMIN "Wildcard (Admin): \"New Game+\"\nAfter beating the first batch of waves, you can start over the beginning, keeping your cash and upgrades. Be wary though, as enemies will be beefier, faster and spawn sooner!! Luckily, you get a cash multiplier and goodies are more common!!" + +#define STRING_INFO_WILDCARD_PATREON "Wildcard (Patreon): \"Progress Through Failure\"\nIf you either screw up (or intentionally get hit), the wave will be instantly cleared, allowing you to proceed to the next one. However, you'll be punished with a fattening, meaning you move slower and become easier to hit!!" + +#define STRING_INFO_WILDCARD_COMMUNITY "Wildcard (Community): \"Minions\"\nCalamity Cobra's patented Snekgun fires friendly serpent minions that can be redirected on her command. Use them to dispatch your foes, and collect goodies, just as good minions should!!" + +#define STRING_INFO_BACK "Back" + +#define STRING_PLAY_TUTORIAL_MOUSE "Hold RIGHT MOUSE to move. Press LEFT MOUSE to fire snakes, and press RIGHT MOUSE/R to redirect snakes, which deal more damage. Collect cash, hunt bounties, and watch your waistline!!" +#define STRING_PLAY_TUTORIAL_KEYBOARD "Use arrow keys/WASD to move. Press LEFT MOUSE to fire snakes, and press RIGHT MOUSE/R to redirect snakes, which deal more damage. Collect cash, hunt bounties, and watch your waistline!!" + +#define STRING_PLAY_PAUSE "PAUSE!!" +#define STRING_PLAY_GUI_SNAKE_POWER_UP "INFINITE" +#define STRING_PLAY_MESSAGE_WAVE_CLEAR "WAVE CLEARED!!" +#define STRING_PLAY_MESSAGE_WAVE_FAIL "WAVE FAILED..." +#define STRING_PLAY_MESSAGE_PURCHASE_NO_CASH "YOU'RE BROKE!!" +#define STRING_PLAY_MESSAGE_PURCHASE_MAXED "MAXED!!" +#define STRING_PLAY_MESSAGE_GAME_CLEAR "GAME CLEARED!!" +#define STRING_PLAY_MESSAGE_POWER_UP "BERSERK!!" +#define STRING_PLAY_MESSAGE_POWER_DOWN "BERSERK OVER..." +#define STRING_PLAY_MESSAGE_MIRACLE_CURE "-1 FATNESS!!" +#define STRING_PLAY_MESSAGE_DYNAMITE "KABOOM!!" +#define STRING_PLAY_GUI_WAVE "WAVE " +#define STRING_PLAY_GUI_READY "LET'S WRASSLE!!" +#define STRING_PLAY_GUI_BOUNTY_NONE "..." +#define STRING_PLAY_GUI_BOUNTY_DEAD "X-X" + +#define STRING_ENDING_NEW_GAME_PLUS "New Game+" +#define STRING_ENDING_RESULTS "Forfeit" + +#define STRING_RESULTS "RESULTS" +#define STRING_RESULTS_BOUNTIES_HUNTED "Bounties Hunted: " +#define STRING_RESULTS_CASH_COLLECTED "Cash Collected: " +#define STRING_RESULTS_CASH_HELD "Cash Held:" +#define STRING_RESULTS_HIGHEST_WAVE "Highest Wave: " +#define STRING_RESULTS_THANKS "Thanks for playing!! c:" + +#define STRING_TITLE_START "Start!!" +#define STRING_TITLE_INFO "Credits / Info" diff --git a/src/game/ending/ending.c b/src/game/ending/ending.c new file mode 100755 index 0000000..daf8b43 --- /dev/null +++ b/src/game/ending/ending.c @@ -0,0 +1,164 @@ +/* + * DESCRIPTION: + * Fail ending code. +*/ + +#include "ending.h" + +/* Initializes ending. */ +void +ending_init(Ending* _e, Game* _g, EndingType _t) +{ + Sprite plusButtonSprite; + Sprite endButtonSprite; + + memset(_e, '\0', sizeof(Intro)); + + _e->g = _g; + _e->type = _t; + + switch (_e->type) + { + case ENDING_FAIL: + scene_init + ( + &_e->sceneHandler, + _e->g, + (Scene*)ENDING_FAIL_SCENES, + ENDING_FAIL_SCENE_COUNT + ); + break; + case ENDING_ONE: + scene_init + ( + &_e->sceneHandler, + _e->g, + (Scene*)ENDING_ONE_SCENES, + ENDING_ONE_SCENE_COUNT + ); + _e->isActualEnding = TRUE; + break; + case ENDING_TWO: + scene_init + ( + &_e->sceneHandler, + _e->g, + (Scene*)ENDING_TWO_SCENES, + ENDING_TWO_SCENE_COUNT + ); + _e->isActualEnding = TRUE; + break; + case ENDING_THREE: + scene_init + ( + &_e->sceneHandler, + _e->g, + (Scene*)ENDING_THREE_SCENES, + ENDING_THREE_SCENE_COUNT + ); + _e->isActualEnding = TRUE; + break; + default: + break; + } + + text_sprite_init + ( + &plusButtonSprite, + _e->g->renderer, + _e->g->fonts[FONT_BIG], + ENDING_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_ENDING_NEW_GAME_PLUS, + 0 + ); + + plusButtonSprite.x = ENDING_PLUS_BUTTON_OFFSET_X; + plusButtonSprite.y = ENDING_PLUS_BUTTON_OFFSET_Y; + + text_sprite_init + ( + &endButtonSprite, + _e->g->renderer, + _e->g->fonts[FONT_BIG], + ENDING_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_ENDING_RESULTS, + 0 + ); + + endButtonSprite.x = ENDING_END_BUTTON_OFFSET_X; + endButtonSprite.y = ENDING_END_BUTTON_OFFSET_Y; + + button_init(&_e->plusButton, &plusButtonSprite); + button_init(&_e->endButton, &endButtonSprite); + + cursor_init(&_e->cursor, &_e->g->mouse, _e->g->textures[TEXTURE_GUI_CURSOR]); + + music_play(_e->g->music[MUSIC_WIND], TRUE); +} + +/* Frees ending. */ +void +ending_free(Ending* _e) +{ + if (_e->plusButton.sprite.atlas.texture) + texture_free(_e->plusButton.sprite.atlas.texture); + + if (_e->endButton.sprite.atlas.texture) + texture_free(_e->endButton.sprite.atlas.texture); + + scene_free(&_e->sceneHandler, _e->g); +} + +/* Updates ending. */ +void +ending_update(Ending* _e) +{ + scene_update(&_e->sceneHandler, _e->g); + + cursor_update(&_e->cursor, &_e->g->mouse); + + if (_e->sceneHandler.isEnd) + { + if (_e->isActualEnding) + { + button_update(&_e->plusButton, &_e->g->mouse); + button_update(&_e->endButton, &_e->g->mouse); + + if (_e->plusButton.state == BUTTON_STATE_PRESS) + { + _e->isNewGamePlus = TRUE; + sound_play(_e->g->sounds[SOUND_MENU], -1); + } + else if (_e->endButton.state == BUTTON_STATE_PRESS) + { + _e->isResults = TRUE; + sound_play(_e->g->sounds[SOUND_MENU], -1); + } + } + else + { + if (mouse_press(&_e->g->mouse, MOUSE_LEFT)) + { + _e->isResults = TRUE; + sound_play(_e->g->sounds[SOUND_MENU], -1); + } + } + } +} + +/* Draws ending. */ +void +ending_draw(Ending* _e) +{ + scene_draw(&_e->sceneHandler, _e->g); + + cursor_draw(&_e->cursor, _e->g->renderer); + + if (_e->isActualEnding && _e->sceneHandler.isEnd) + { + button_draw(&_e->plusButton, _e->g->renderer); + button_draw(&_e->endButton, _e->g->renderer); + } +} diff --git a/src/game/ending/ending.h b/src/game/ending/ending.h new file mode 100755 index 0000000..cb1baf9 --- /dev/null +++ b/src/game/ending/ending.h @@ -0,0 +1,63 @@ +/* + * DESCRIPTION: + * Header file for ending_fail.c. +*/ + +#pragma once + +#include "../gui/button.h" +#include "../gui/cursor.h" +#include "../scene/scene.h" +#include "../GAME_STRINGS.h" + +#define ENDING_FAIL_SCENE_COUNT 1 +#define ENDING_ONE_SCENE_COUNT 1 +#define ENDING_TWO_SCENE_COUNT 1 +#define ENDING_THREE_SCENE_COUNT 1 + +#define ENDING_PLUS_BUTTON_OFFSET_X 150 +#define ENDING_PLUS_BUTTON_OFFSET_Y 550 +#define ENDING_END_BUTTON_OFFSET_X 600 +#define ENDING_END_BUTTON_OFFSET_Y 550 + +#define ENDING_TWO_CASH_THRESHOLD 50000 +#define ENDING_THREE_CASH_THRESHOLD 100000 + +static const Scene ENDING_FAIL_SCENES[ENDING_FAIL_SCENE_COUNT] = +{ + { + .textureType = TEXTURE_ENDING_FAIL, + .text = STRING_ENDING_FAIL + } +}; + +static const Scene ENDING_ONE_SCENES[ENDING_ONE_SCENE_COUNT] = +{ + { + .textureType = TEXTURE_ENDING_ONE, + .text = STRING_ENDING_ONE + } +}; + +static const Scene ENDING_TWO_SCENES[ENDING_TWO_SCENE_COUNT] = +{ + { + .textureType = TEXTURE_ENDING_TWO, + .text = STRING_ENDING_TWO + } +}; + +static const Scene ENDING_THREE_SCENES[ENDING_THREE_SCENE_COUNT] = +{ + { + .textureType = TEXTURE_ENDING_THREE, + .text = STRING_ENDING_THREE + } +}; + +static const SDL_Color ENDING_TEXT_COLOR = {255, 255, 255, 255}; + +void ending_update(Ending* _e); +void ending_init(Ending* _e, Game* _g, EndingType _t); +void ending_draw(Ending* _e); +void ending_free(Ending* _e); diff --git a/src/game/game.c b/src/game/game.c new file mode 100755 index 0000000..f0e122a --- /dev/null +++ b/src/game/game.c @@ -0,0 +1,100 @@ +/* + * DESCRIPTION: + * Main game functions. +*/ + +#include "game.h" + +static void game_update(Game* _g); +static void game_draw(Game* _g); + +/* Updates the game. */ +static void +game_update(Game* _g) +{ + event_update(&_g->event); + mouse_update(&_g->mouse); + keyboard_update(&_g->keyboard); + + game_state_update(_g); + + /* + if (keyboard_press(&_g->keyboard, KEYBOARD_F)) + { + if (_g->isFullScreen) + window_fullscreen_enter(_g->window); + else + window_fullscreen_exit(_g->window); + + + _g->isFullScreen = !_g->isFullScreen; + } + */ +} + +/* Draws the game. */ +static void +game_draw(Game* _g) +{ + renderer_color_set(_g->renderer, RENDERER_COLOR); + renderer_clear(_g->renderer); + + game_state_draw(_g); + + renderer_present(_g->renderer); +} + +/* Initializes game. */ +void +game_init(Game* _g) +{ + memset(_g, '\0', sizeof(Game)); + + window_init + ( + &_g->window, + WINDOW_TITLE, + WINDOW_WIDTH, + WINDOW_HEIGHT, + WINDOW_FLAGS + ); + + renderer_init + ( + &_g->renderer, + _g->window, + RENDERER_FLAGS + ); + + SDL_ShowCursor(SDL_DISABLE); + + game_state_transition(_g, STATE_TITLE, TRUE, TRUE, TRUE, TRUE); + + _g->stateTimer = STATE_TRANSITION_TIMER_MAX; +} + +/* Frees game resources. */ +void +game_free(Game* _g) +{ + game_state_free(_g); + game_music_clear(_g); + game_font_clear(_g); + game_sound_clear(_g); + game_texture_clear(_g); +} + +/* Main game loop. */ +void +game_loop(Game* _g) +{ + tick_update(&_g->tick); + + while (_g->tick.cum > FRAME_DELAY) + { + game_update(_g); + _g->tick.cum -= FRAME_DELAY; + } + + game_draw(_g); +} diff --git a/src/game/game.h b/src/game/game.h new file mode 100755 index 0000000..c472e7e --- /dev/null +++ b/src/game/game.h @@ -0,0 +1,24 @@ +/* + * DESCRIPTION: + * Header file for game.c. +*/ + +#pragma once + +#include "GAME_STRINGS.h" + +#include "game_state.h" + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define WINDOW_FLAGS 0 +#define WINDOW_TITLE STRING_WINDOW_TITLE +#define RENDERER_FLAGS SDL_RENDERER_ACCELERATED +#define FPS 60 +#define FRAME_DELAY (SECOND / FPS) + +static const SDL_Color RENDERER_COLOR = {0, 0, 0, 255}; + +void game_free(Game* _g); +void game_init(Game* _g); +void game_loop(Game* _g); diff --git a/src/game/game_ending.c b/src/game/game_ending.c new file mode 100755 index 0000000..3f0f7a5 --- /dev/null +++ b/src/game/game_ending.c @@ -0,0 +1,102 @@ +/* + * DESCRIPTION: + * The "Title" state's meta functions. +*/ + +#include "game_ending.h" + +/* Initializes the Intro state's resources. */ +void +game_ending_init(Game* _g) +{ + for (s32 i = 0; i < ENDING_TEXTURE_COUNT; i++) + { + s32 index; + + index = ENDING_TEXTURES[i]; + + game_texture_init(_g, (TextureType)index); + } + + for (s32 i = 0; i < ENDING_SOUND_COUNT; i++) + { + s32 index; + + index = ENDING_SOUNDS[i]; + + game_sound_init(_g, (SoundType)index); + } + + for (s32 i = 0; i < ENDING_FONT_COUNT; i++) + { + s32 index; + + index = ENDING_FONTS[i]; + + game_font_init(_g, (FontType)index); + } + + for (s32 i = 0; i < ENDING_MUSIC_COUNT; i++) + { + s32 index; + + index = ENDING_MUSIC[i]; + + game_music_init(_g, (MusicType)index); + } +} + +/* Frees the Intro state's resources. */ +void +game_ending_free(Game* _g) +{ + for (s32 i = 0; i < ENDING_TEXTURE_COUNT; i++) + { + s32 index; + + index = ENDING_TEXTURES[i]; + + game_texture_free(_g, (TextureType)index); + } + + for (s32 i = 0; i < ENDING_SOUND_COUNT; i++) + { + s32 index; + + index = ENDING_SOUNDS[i]; + + game_sound_free(_g, (SoundType)index); + } + + for (s32 i = 0; i < ENDING_FONT_COUNT; i++) + { + s32 index; + + index = ENDING_FONTS[i]; + + game_font_free(_g, (FontType)index); + } + + for (s32 i = 0; i < ENDING_MUSIC_COUNT; i++) + { + s32 index; + + index = ENDING_MUSIC[i]; + + game_music_free(_g, (MusicType)index); + } +} + +/* Updates the Intro state. */ +void +game_ending_update(Game* _g) +{ + ending_update(&_g->ending); +} + +/* Draws the Intro state. */ +void +game_ending_draw(Game* _g) +{ + ending_draw(&_g->ending); +} diff --git a/src/game/game_ending.h b/src/game/game_ending.h new file mode 100755 index 0000000..cdb8b7b --- /dev/null +++ b/src/game/game_ending.h @@ -0,0 +1,49 @@ +/* + * DESCRIPTION: + * Header file for game_ending.c. +*/ + +#pragma once + +#include "ending/ending.h" + +#include "game_font.h" +#include "game_music.h" +#include "game_sound.h" +#include "game_texture.h" + +#define ENDING_TEXTURE_COUNT 5 +#define ENDING_SOUND_COUNT 2 +#define ENDING_MUSIC_COUNT 1 +#define ENDING_FONT_COUNT 2 + +static const TextureType ENDING_TEXTURES[ENDING_TEXTURE_COUNT] = +{ + TEXTURE_ENDING_FAIL, + TEXTURE_ENDING_ONE, + TEXTURE_ENDING_TWO, + TEXTURE_ENDING_THREE, + TEXTURE_GUI_CURSOR +}; + +static const SoundType ENDING_SOUNDS[ENDING_SOUND_COUNT] = +{ + SOUND_BLIP, + SOUND_MENU +}; + +static const MusicType ENDING_MUSIC[ENDING_MUSIC_COUNT] = +{ + MUSIC_WIND +}; + +static const FontType ENDING_FONTS[ENDING_FONT_COUNT] = +{ + FONT_SMALL, + FONT_BIG +}; + +void game_ending_draw(Game* _g); +void game_ending_free(Game* _g); +void game_ending_init(Game* _g); +void game_ending_update(Game* _g); diff --git a/src/game/game_font.c b/src/game/game_font.c new file mode 100755 index 0000000..cd5ccc3 --- /dev/null +++ b/src/game/game_font.c @@ -0,0 +1,37 @@ +/* + * DESCRIPTION: + * Loads/frees game fonts. +*/ + +#include "game_font.h" + +/* Loads a game font. */ +void +game_font_init(Game* _g, FontType _f) +{ + if (!_g->fonts[_f]) + font_init(&_g->fonts[_f], (char*)FONT_PATHS[_f], (s32)FONT_SIZES[_f]); +} + +/* Frees a game font. */ +void +game_font_free(Game* _g, FontType _f) +{ + if (_g->fonts[_f]) + { + font_free(_g->fonts[_f]); + _g->fonts[_f] = NULL; + } +} + +/* Clears all fonts. */ +void +game_font_clear(Game* _g) +{ + for (s32 i = 0; i < FONT_COUNT; i++) + { + if (_g->fonts[i]) + game_font_free(_g, (FontType)i); + } +} + diff --git a/src/game/game_font.h b/src/game/game_font.h new file mode 100755 index 0000000..23d7efb --- /dev/null +++ b/src/game/game_font.h @@ -0,0 +1,32 @@ +/* + * DESCRIPTION: + * Header file for game_font.h. +*/ + +#pragma once + +#include "GAME_COMMON.h" + +static const char* FONT_PATHS[FONT_COUNT] = +{ + "", + "res/font/fontJackwrite.ttf", + "res/font/fontJackwrite.ttf", + "res/font/fontJackwrite.ttf", + "res/font/fontJackwrite.ttf", + "res/font/fontJackwrite.ttf", +}; + +static const s32 FONT_SIZES[FONT_COUNT] = +{ + 0, + 12, + 16, + 24, + 32, + 48 +}; + +void game_font_init(Game* _g, FontType _f); +void game_font_free(Game* _g, FontType _f); +void game_font_clear(Game* _g); diff --git a/src/game/game_info.c b/src/game/game_info.c new file mode 100755 index 0000000..0130890 --- /dev/null +++ b/src/game/game_info.c @@ -0,0 +1,106 @@ +/* + * DESCRIPTION: + * The "Info" state's meta functions. +*/ + +#include "game_info.h" + +/* Initializes the Play state's resources. */ +void +game_info_init(Game* _g) +{ + for (s32 i = 0; i < INFO_TEXTURE_COUNT; i++) + { + s32 index; + + index = INFO_TEXTURES[i]; + + game_texture_init(_g, (TextureType)index); + } + + for (s32 i = 0; i < INFO_SOUND_COUNT; i++) + { + s32 index; + + index = INFO_SOUNDS[i]; + + game_sound_init(_g, (SoundType)index); + } + + for (s32 i = 0; i < INFO_FONT_COUNT; i++) + { + s32 index; + + index = INFO_FONTS[i]; + + game_font_init(_g, (FontType)index); + } + + for (s32 i = 0; i < INFO_MUSIC_COUNT; i++) + { + s32 index; + + index = INFO_MUSIC[i]; + + game_music_init(_g, (MusicType)index); + } + + info_init(&_g->info, _g); +} + +/* Frees the Play state's resources. */ +void +game_info_free(Game* _g) +{ + info_free(&_g->info); + + for (s32 i = 0; i < INFO_TEXTURE_COUNT; i++) + { + s32 index; + + index = INFO_TEXTURES[i]; + + game_texture_free(_g, (TextureType)index); + } + + for (s32 i = 0; i < INFO_SOUND_COUNT; i++) + { + s32 index; + + index = INFO_SOUNDS[i]; + + game_sound_free(_g, (SoundType)index); + } + + for (s32 i = 0; i < INFO_FONT_COUNT; i++) + { + s32 index; + + index = INFO_FONTS[i]; + + game_font_free(_g, (FontType)index); + } + + for (s32 i = 0; i < INFO_MUSIC_COUNT; i++) + { + s32 index; + + index = INFO_MUSIC[i]; + + game_music_free(_g, (MusicType)index); + } +} + +/* Updates the Play state. */ +void +game_info_update(Game* _g) +{ + info_update(&_g->info); +} + +/* Draws the Play state. */ +void +game_info_draw(Game* _g) +{ + info_draw(&_g->info); +} diff --git a/src/game/game_info.h b/src/game/game_info.h new file mode 100755 index 0000000..e121404 --- /dev/null +++ b/src/game/game_info.h @@ -0,0 +1,51 @@ +/* + * DESCRIPTION: + * Header file for game_info.c. +*/ + +#pragma once + +#include "info/info.h" + +#include "game_font.h" +#include "game_music.h" +#include "game_sound.h" +#include "game_texture.h" + +#define INFO_TEXTURE_COUNT 6 +#define INFO_SOUND_COUNT 1 +#define INFO_MUSIC_COUNT 1 +#define INFO_FONT_COUNT 4 + +static const TextureType INFO_TEXTURES[INFO_TEXTURE_COUNT] = +{ + TEXTURE_GUI_SIGN_SMALL, + TEXTURE_GUI_CURSOR, + TEXTURE_INFO_THEME, + TEXTURE_INFO_WILDCARD_ADMIN, + TEXTURE_INFO_WILDCARD_PATREON, + TEXTURE_INFO_WILDCARD_COMMUNITY +}; + +static const SoundType INFO_SOUNDS[INFO_SOUND_COUNT] = +{ + SOUND_MENU +}; + +static const MusicType INFO_MUSIC[INFO_MUSIC_COUNT] = +{ + MUSIC_TITLE +}; + +static const FontType INFO_FONTS[INFO_FONT_COUNT] = +{ + FONT_TINY, + FONT_SMALL, + FONT_MEDIUM, + FONT_BIG +}; + +void game_info_draw(Game* _g); +void game_info_free(Game* _g); +void game_info_init(Game* _g); +void game_info_update(Game* _g); diff --git a/src/game/game_intro.c b/src/game/game_intro.c new file mode 100755 index 0000000..5df5633 --- /dev/null +++ b/src/game/game_intro.c @@ -0,0 +1,102 @@ +/* + * DESCRIPTION: + * The "Title" state's meta functions. +*/ + +#include "game_intro.h" + +/* Initializes the Intro state's resources. */ +void +game_intro_init(Game* _g) +{ + for (s32 i = 0; i < INTRO_TEXTURE_COUNT; i++) + { + s32 index; + + index = INTRO_TEXTURES[i]; + + game_texture_init(_g, (TextureType)index); + } + + for (s32 i = 0; i < INTRO_SOUND_COUNT; i++) + { + s32 index; + + index = INTRO_SOUNDS[i]; + + game_sound_init(_g, (SoundType)index); + } + + for (s32 i = 0; i < INTRO_FONT_COUNT; i++) + { + s32 index; + + index = INTRO_FONTS[i]; + + game_font_init(_g, (FontType)index); + } + + for (s32 i = 0; i < INTRO_MUSIC_COUNT; i++) + { + s32 index; + + index = INTRO_MUSIC[i]; + + game_music_init(_g, (MusicType)index); + } +} + +/* Frees the Intro state's resources. */ +void +game_intro_free(Game* _g) +{ + for (s32 i = 0; i < INTRO_TEXTURE_COUNT; i++) + { + s32 index; + + index = INTRO_TEXTURES[i]; + + game_texture_free(_g, (TextureType)index); + } + + for (s32 i = 0; i < INTRO_SOUND_COUNT; i++) + { + s32 index; + + index = INTRO_SOUNDS[i]; + + game_sound_free(_g, (SoundType)index); + } + + for (s32 i = 0; i < INTRO_FONT_COUNT; i++) + { + s32 index; + + index = INTRO_FONTS[i]; + + game_font_free(_g, (FontType)index); + } + + for (s32 i = 0; i < INTRO_MUSIC_COUNT; i++) + { + s32 index; + + index = INTRO_MUSIC[i]; + + game_music_free(_g, (MusicType)index); + } +} + +/* Updates the Intro state. */ +void +game_intro_update(Game* _g) +{ + intro_update(&_g->intro); +} + +/* Draws the Intro state. */ +void +game_intro_draw(Game* _g) +{ + intro_draw(&_g->intro); +} diff --git a/src/game/game_intro.h b/src/game/game_intro.h new file mode 100755 index 0000000..9914fe7 --- /dev/null +++ b/src/game/game_intro.h @@ -0,0 +1,48 @@ +/* + * DESCRIPTION: + * Header file for game_intro.c. +*/ + +#pragma once + +#include "intro/intro.h" + +#include "game_font.h" +#include "game_music.h" +#include "game_sound.h" +#include "game_texture.h" + +#define INTRO_TEXTURE_COUNT 5 +#define INTRO_SOUND_COUNT 2 +#define INTRO_MUSIC_COUNT 1 +#define INTRO_FONT_COUNT 1 + +static const TextureType INTRO_TEXTURES[INTRO_TEXTURE_COUNT] = +{ + TEXTURE_INTRO_SCENE_ONE, + TEXTURE_INTRO_SCENE_TWO, + TEXTURE_INTRO_SCENE_THREE, + TEXTURE_INTRO_SCENE_FOUR, + TEXTURE_INTRO_SCENE_FIVE, +}; + +static const SoundType INTRO_SOUNDS[INTRO_SOUND_COUNT] = +{ + SOUND_BLIP, + SOUND_MENU +}; + +static const MusicType INTRO_MUSIC[INTRO_MUSIC_COUNT] = +{ + MUSIC_INTRO +}; + +static const FontType INTRO_FONTS[INTRO_FONT_COUNT] = +{ + FONT_SMALL +}; + +void game_intro_draw(Game* _g); +void game_intro_free(Game* _g); +void game_intro_init(Game* _g); +void game_intro_update(Game* _g); diff --git a/src/game/game_music.c b/src/game/game_music.c new file mode 100755 index 0000000..40184bf --- /dev/null +++ b/src/game/game_music.c @@ -0,0 +1,36 @@ +/* + * DESCRIPTION: + * Loads/frees game music. +*/ + +#include "game_music.h" + +/* Loads a game music. */ +void +game_music_init(Game* _g, MusicType _m) +{ + if (!_g->music[_m]) + music_init(&_g->music[_m], (char*)MUSIC_PATHS[_m]); +} + +/* Frees a game music. */ +void +game_music_free(Game* _g, MusicType _m) +{ + if (_g->music[_m]) + { + music_free(_g->music[_m]); + _g->music[_m] = NULL; + } +} + +/* Clears all music. */ +void +game_music_clear(Game* _g) +{ + for (s32 i = 0; i < MUSIC_COUNT; i++) + { + if (_g->music[i]) + game_music_free(_g, (MusicType)i); + } +} diff --git a/src/game/game_music.h b/src/game/game_music.h new file mode 100755 index 0000000..6f5fdfd --- /dev/null +++ b/src/game/game_music.h @@ -0,0 +1,24 @@ +/* + * DESCRIPTION: + * Header file for game_music.h +*/ + +#pragma once + +#include "GAME_COMMON.h" + +static const char* MUSIC_PATHS[MUSIC_COUNT] = +{ + "", + "res/audio/music/musWind.ogg", + "res/audio/music/musPlay.ogg", + "res/audio/music/musWaveClear.ogg", + "res/audio/music/musWaveFail.ogg", + "res/audio/music/musResults.ogg", + "res/audio/music/musTitle.ogg", + "res/audio/music/musIntro.ogg", +}; + +void game_music_init(Game* _g, MusicType _m); +void game_music_free(Game* _g, MusicType _m); +void game_music_clear(Game* _g); diff --git a/src/game/game_play.c b/src/game/game_play.c new file mode 100755 index 0000000..544d0fa --- /dev/null +++ b/src/game/game_play.c @@ -0,0 +1,103 @@ +/* + * DESCRIPTION: + * The "Play" state's meta functions. +*/ + +#include "game_play.h" + +/* Initializes the Play state's resources. */ +void +game_play_init(Game* _g) +{ + for (s32 i = 0; i < PLAY_TEXTURE_COUNT; i++) + { + s32 index; + + index = PLAY_TEXTURES[i]; + + game_texture_init(_g, (TextureType)index); + } + + for (s32 i = 0; i < PLAY_SOUND_COUNT; i++) + { + s32 index; + + index = PLAY_SOUNDS[i]; + + game_sound_init(_g, (SoundType)index); + } + + for (s32 i = 0; i < PLAY_FONT_COUNT; i++) + { + s32 index; + + index = PLAY_FONTS[i]; + + game_font_init(_g, (FontType)index); + } + + for (s32 i = 0; i < PLAY_MUSIC_COUNT; i++) + { + s32 index; + + index = PLAY_MUSIC[i]; + + game_music_init(_g, (MusicType)index); + } +} + +/* Frees the Play state's resources. */ +void +game_play_free(Game* _g) +{ + for (s32 i = 0; i < PLAY_TEXTURE_COUNT; i++) + { + s32 index; + + index = PLAY_TEXTURES[i]; + + game_texture_free(_g, (TextureType)index); + } + + for (s32 i = 0; i < PLAY_SOUND_COUNT; i++) + { + s32 index; + + index = PLAY_SOUNDS[i]; + + game_sound_free(_g, (SoundType)index); + } + + for (s32 i = 0; i < PLAY_FONT_COUNT; i++) + { + s32 index; + + index = PLAY_FONTS[i]; + + game_font_free(_g, (FontType)index); + } + + for (s32 i = 0; i < PLAY_MUSIC_COUNT; i++) + { + s32 index; + + index = PLAY_MUSIC[i]; + + game_music_free(_g, (MusicType)index); + } + +} + +/* Updates the Play state. */ +void +game_play_update(Game* _g) +{ + play_update(&_g->play); +} + +/* Draws the Play state. */ +void +game_play_draw(Game* _g) +{ + play_draw(&_g->play); +} diff --git a/src/game/game_play.h b/src/game/game_play.h new file mode 100755 index 0000000..487aa31 --- /dev/null +++ b/src/game/game_play.h @@ -0,0 +1,100 @@ +/* + * DESCRIPTION: + * Header file for game_play.c. +*/ + +#pragma once + +#include "play/play.h" + +#include "game_font.h" +#include "game_music.h" +#include "game_sound.h" +#include "game_texture.h" + +#define PLAY_TEXTURE_COUNT 29 +#define PLAY_SOUND_COUNT 23 +#define PLAY_MUSIC_COUNT 4 +#define PLAY_FONT_COUNT 5 + +static const TextureType PLAY_TEXTURES[PLAY_TEXTURE_COUNT] = +{ + TEXTURE_GUI_CURSOR, + TEXTURE_GUI_SNAKE, + TEXTURE_GUI_REDIRECT, + TEXTURE_GUI_CASH, + TEXTURE_GUI_UPGRADE_SNAKE, + TEXTURE_GUI_UPGRADE_LIFETIME, + TEXTURE_GUI_UPGRADE_REDIRECT, + TEXTURE_GUI_SIGN_SMALL, + TEXTURE_GUI_SIGN_BIG, + TEXTURE_SHADOW, + TEXTURE_ARC, + TEXTURE_PLAY_BG, + TEXTURE_PLAYER_STAGE_ONE, + TEXTURE_PLAYER_STAGE_TWO, + TEXTURE_PLAYER_STAGE_THREE, + TEXTURE_PLAYER_TAIL_STAGE_ONE, + TEXTURE_PLAYER_TAIL_STAGE_TWO, + TEXTURE_PLAYER_TAIL_STAGE_THREE, + TEXTURE_PLAYER_SNAKE, + TEXTURE_ENEMY_CUPPER, + TEXTURE_ENEMY_CHIPPER, + TEXTURE_ENEMY_CHIP, + TEXTURE_ENEMY_CRUMBLER, + TEXTURE_COLLECTIBLE_DOLLARS, + TEXTURE_COLLECTIBLE_SACK, + TEXTURE_COLLECTIBLE_BULLIONS, + TEXTURE_COLLECTIBLE_MIRACLE_CURE, + TEXTURE_COLLECTIBLE_DYNAMITE, + TEXTURE_COLLECTIBLE_BERSERK +}; + +static const SoundType PLAY_SOUNDS[PLAY_SOUND_COUNT] = +{ + SOUND_MENU, + SOUND_SNAKE_FIRE, + SOUND_SNAKE_REDIRECT, + SOUND_SNAKE_RETURN, + SOUND_ENEMY_HURT, + SOUND_CUPPER_KILL, + SOUND_CHIPPER_KILL, + SOUND_CRUMBLER_KILL, + SOUND_CHIP_KILL, + SOUND_RUMBLE, + SOUND_COLLECTIBLE, + SOUND_DOLLARS, + SOUND_SACK, + SOUND_BULLIONS, + SOUND_PURCHASE, + SOUND_MIRACLE_CURE, + SOUND_DYNAMITE, + SOUND_BERSERK, + SOUND_SWALLOW, + SOUND_GROWTH, + SOUND_ERROR, + SOUND_GO, + SOUND_SPIT +}; + +static const MusicType PLAY_MUSIC[PLAY_MUSIC_COUNT] = +{ + MUSIC_WIND, + MUSIC_PLAY, + MUSIC_WAVE_CLEAR, + MUSIC_WAVE_FAIL +}; + +static const FontType PLAY_FONTS[PLAY_FONT_COUNT] = +{ + FONT_TINY, + FONT_SMALL, + FONT_MEDIUM, + FONT_BIG, + FONT_MEGA +}; + +void game_play_draw(Game* _g); +void game_play_free(Game* _g); +void game_play_init(Game* _g); +void game_play_update(Game* _g); diff --git a/src/game/game_results.c b/src/game/game_results.c new file mode 100755 index 0000000..b9a0bd6 --- /dev/null +++ b/src/game/game_results.c @@ -0,0 +1,102 @@ +/* + * DESCRIPTION: + * The "Title" state's meta functions. +*/ + +#include "game_results.h" + +/* Initializes the Intro state's resources. */ +void +game_results_init(Game* _g) +{ + for (s32 i = 0; i < RESULTS_TEXTURE_COUNT; i++) + { + s32 index; + + index = RESULTS_TEXTURES[i]; + + game_texture_init(_g, (TextureType)index); + } + + for (s32 i = 0; i < RESULTS_SOUND_COUNT; i++) + { + s32 index; + + index = RESULTS_SOUNDS[i]; + + game_sound_init(_g, (SoundType)index); + } + + for (s32 i = 0; i < RESULTS_FONT_COUNT; i++) + { + s32 index; + + index = RESULTS_FONTS[i]; + + game_font_init(_g, (FontType)index); + } + + for (s32 i = 0; i < RESULTS_MUSIC_COUNT; i++) + { + s32 index; + + index = RESULTS_MUSIC[i]; + + game_music_init(_g, (MusicType)index); + } +} + +/* Frees the Intro state's resources. */ +void +game_results_free(Game* _g) +{ + for (s32 i = 0; i < RESULTS_TEXTURE_COUNT; i++) + { + s32 index; + + index = RESULTS_TEXTURES[i]; + + game_texture_free(_g, (TextureType)index); + } + + for (s32 i = 0; i < RESULTS_SOUND_COUNT; i++) + { + s32 index; + + index = RESULTS_SOUNDS[i]; + + game_sound_free(_g, (SoundType)index); + } + + for (s32 i = 0; i < RESULTS_FONT_COUNT; i++) + { + s32 index; + + index = RESULTS_FONTS[i]; + + game_font_free(_g, (FontType)index); + } + + for (s32 i = 0; i < RESULTS_MUSIC_COUNT; i++) + { + s32 index; + + index = RESULTS_MUSIC[i]; + + game_music_free(_g, (MusicType)index); + } +} + +/* Updates the Intro state. */ +void +game_results_update(Game* _g) +{ + results_update(&_g->results); +} + +/* Draws the Intro state. */ +void +game_results_draw(Game* _g) +{ + results_draw(&_g->results); +} diff --git a/src/game/game_results.h b/src/game/game_results.h new file mode 100755 index 0000000..9285166 --- /dev/null +++ b/src/game/game_results.h @@ -0,0 +1,44 @@ +/* + * DESCRIPTION: + * Header file for game_results.c. +*/ + +#pragma once + +#include "results/results.h" + +#include "game_font.h" +#include "game_music.h" +#include "game_sound.h" +#include "game_texture.h" + +#define RESULTS_TEXTURE_COUNT 1 +#define RESULTS_SOUND_COUNT 1 +#define RESULTS_MUSIC_COUNT 1 +#define RESULTS_FONT_COUNT 2 + +static const TextureType RESULTS_TEXTURES[RESULTS_TEXTURE_COUNT] = +{ + TEXTURE_RESULTS +}; + +static const SoundType RESULTS_SOUNDS[RESULTS_SOUND_COUNT] = +{ + SOUND_MENU +}; + +static const MusicType RESULTS_MUSIC[RESULTS_MUSIC_COUNT] = +{ + MUSIC_RESULTS +}; + +static const FontType RESULTS_FONTS[RESULTS_FONT_COUNT] = +{ + FONT_BIG, + FONT_MEGA +}; + +void game_results_draw(Game* _g); +void game_results_free(Game* _g); +void game_results_init(Game* _g); +void game_results_update(Game* _g); diff --git a/src/game/game_sound.c b/src/game/game_sound.c new file mode 100755 index 0000000..d9a0b3f --- /dev/null +++ b/src/game/game_sound.c @@ -0,0 +1,37 @@ +/* + * DESCRIPTION: + * Loads/frees game sounds. +*/ + +#include "game_sound.h" + +/* Loads a game sound. */ +void +game_sound_init(Game* _g, SoundType _s) +{ + if (!_g->sounds[_s]) + sound_init(&_g->sounds[_s], (char*)SOUND_PATHS[_s]); +} + +/* Frees a game sound. */ +void +game_sound_free(Game* _g, SoundType _s) +{ + if (_g->sounds[_s]) + { + sound_free(_g->sounds[_s]); + _g->sounds[_s] = NULL; + } +} + +/* Clears all sounds. */ +void +game_sound_clear(Game* _g) +{ + for (s32 i = 0; i < SOUND_COUNT; i++) + { + if (_g->sounds[i]) + game_sound_free(_g, (SoundType)i); + } +} + diff --git a/src/game/game_sound.h b/src/game/game_sound.h new file mode 100755 index 0000000..7276d9f --- /dev/null +++ b/src/game/game_sound.h @@ -0,0 +1,41 @@ +/* + * DESCRIPTION: + * Header file for game_sound.h +*/ + +#pragma once + +#include "GAME_COMMON.h" + +static const char* SOUND_PATHS[SOUND_COUNT] = +{ + "", + "res/audio/sfx/sfxMenu.ogg", + "res/audio/sfx/sfxBlip.ogg", + "res/audio/sfx/sfxSnakeFire.ogg", + "res/audio/sfx/sfxSnakeRedirect.ogg", + "res/audio/sfx/sfxSnakeReturn.ogg", + "res/audio/sfx/sfxEnemyHurt.ogg", + "res/audio/sfx/sfxCupperKill.ogg", + "res/audio/sfx/sfxChipperKill.ogg", + "res/audio/sfx/sfxCrumblerKill.ogg", + "res/audio/sfx/sfxChipKill.ogg", + "res/audio/sfx/sfxRumble.ogg", + "res/audio/sfx/sfxCollectible.ogg", + "res/audio/sfx/sfxDollars.ogg", + "res/audio/sfx/sfxSack.ogg", + "res/audio/sfx/sfxBullions.ogg", + "res/audio/sfx/sfxPurchase.ogg", + "res/audio/sfx/sfxMiracleCure.ogg", + "res/audio/sfx/sfxDynamite.ogg", + "res/audio/sfx/sfxBerserk.ogg", + "res/audio/sfx/sfxSwallow.ogg", + "res/audio/sfx/sfxGrowth.ogg", + "res/audio/sfx/sfxError.ogg", + "res/audio/sfx/sfxGo.ogg", + "res/audio/sfx/sfxSpit.ogg", +}; + +void game_sound_init(Game* _g, SoundType _s); +void game_sound_free(Game* _g, SoundType _s); +void game_sound_clear(Game* _g); diff --git a/src/game/game_state.c b/src/game/game_state.c new file mode 100755 index 0000000..9e46e10 --- /dev/null +++ b/src/game/game_state.c @@ -0,0 +1,381 @@ +/* + * DESCRIPTION: + * Game states. +*/ + +#include "game_state.h" + +static void game_state_transition_update(Game* _g); +static void game_state_set(Game* _g, StateType _s); + +/* Update state change transition. */ +static void +game_state_transition_update(Game* _g) +{ + if (_g->isStateTransitioning) + { + _g->stateTimer++; + + if (_g->stateTimer >= STATE_TRANSITION_TIMER_MAX) + { + game_state_set(_g, _g->stateNext); + _g->stateNext = STATE_NONE; + _g->isStateTransitioning = FALSE; + } + } + else + { + _g->stateTimer--; + + if (_g->stateTimer <= 0) + _g->isStateTransition = FALSE; + } +} + +/* Sets the game's state to a specified one. */ +static void +game_state_set(Game* _g, StateType _s) +{ + game_state_free(_g); + + _g->state = _s; + + if (_g->isNextStateResourcesWillBeInit) + { + switch (_g->state) + { + case STATE_TITLE: + game_title_init(_g); + break; + case STATE_INTRO: + game_intro_init(_g); + break; + case STATE_PLAY: + game_play_init(_g); + break; + case STATE_INFO: + game_info_init(_g); + break; + case STATE_ENDING_FAIL: + case STATE_ENDING_ONE: + case STATE_ENDING_TWO: + case STATE_ENDING_THREE: + game_ending_init(_g); + break; + case STATE_RESULTS: + game_results_init(_g); + break; + default: + break; + } + + _g->isNextStateResourcesWillBeInit = FALSE; + } + + if (_g->isNextStateWillBeInit) + { + switch (_g->state) + { + case STATE_TITLE: + title_init(&_g->title, _g); + break; + case STATE_INTRO: + intro_init(&_g->intro, _g); + break; + case STATE_PLAY: + play_init(&_g->play, _g); + break; + case STATE_INFO: + info_init(&_g->info, _g); + break; + case STATE_ENDING_FAIL: + ending_init(&_g->ending, _g, ENDING_FAIL); + break; + case STATE_ENDING_ONE: + ending_init(&_g->ending, _g, ENDING_ONE); + break; + case STATE_ENDING_TWO: + ending_init(&_g->ending, _g, ENDING_TWO); + break; + case STATE_ENDING_THREE: + ending_init(&_g->ending, _g, ENDING_THREE); + break; + case STATE_RESULTS: + results_init(&_g->results, _g); + break; + default: + break; + } + + _g->isNextStateWillBeInit = FALSE; + } +} + +/* Transition the a state type with a fade-in/fade-out. */ +void +game_state_transition(Game* _g, StateType _s, bool_u8 _i, bool_u8 _f, bool_u8 _rI, bool_u8 _rF) +{ + _g->stateTimer = 0; + _g->stateNext = _s; + _g->isNextStateWillBeInit = _i; + _g->isPrevStateWillBeFreed = _f; + _g->isNextStateResourcesWillBeInit = _rI; + _g->isPrevStateResourcesWillBeFreed = _rF; + _g->isStateTransitioning = TRUE; + _g->isStateTransition = TRUE; +} + +/* Frees everything related to the game's current state. */ +void +game_state_free(Game* _g) +{ + if (_g->isPrevStateResourcesWillBeFreed) + { + switch (_g->state) + { + case STATE_TITLE: + game_title_free(_g); + break; + case STATE_INTRO: + game_intro_free(_g); + break; + case STATE_PLAY: + game_play_free(_g); + break; + case STATE_INFO: + game_info_free(_g); + break; + case STATE_ENDING_FAIL: + case STATE_ENDING_ONE: + case STATE_ENDING_TWO: + case STATE_ENDING_THREE: + game_ending_free(_g); + break; + case STATE_RESULTS: + game_results_free(_g); + break; + default: + break; + + } + + _g->isPrevStateResourcesWillBeFreed = FALSE; + } + + if (_g->isPrevStateWillBeFreed) + { + switch (_g->state) + { + case STATE_TITLE: + title_free(&_g->title); + break; + case STATE_INTRO: + intro_free(&_g->intro); + break; + case STATE_PLAY: + play_free(&_g->play); + break; + case STATE_INFO: + info_free(&_g->info); + break; + case STATE_ENDING_FAIL: + case STATE_ENDING_ONE: + case STATE_ENDING_TWO: + case STATE_ENDING_THREE: + ending_free(&_g->ending); + break; + case STATE_RESULTS: + results_free(&_g->results); + break; + default: + break; + } + + _g->isPrevStateWillBeFreed = FALSE; + } +} + +/* Updates the game's state. */ +void +game_state_update(Game* _g) +{ + if (_g->isStateTransition) + game_state_transition_update(_g); + + switch (_g->state) + { + case STATE_TITLE: + game_title_update(_g); + + if + ( + _g->title.isStartPressed && + !_g->isStateTransition + ) + { + _g->title.isStartPressed = FALSE; + game_state_transition(_g, STATE_INTRO, TRUE, TRUE, TRUE, TRUE); + } + + if + ( + _g->title.isInfoPressed && + !_g->isStateTransition + ) + { + _g->title.isInfoPressed = FALSE; + game_state_transition(_g, STATE_INFO, TRUE, TRUE, TRUE, TRUE); + } + break; + case STATE_INTRO: + game_intro_update(_g); + + if + ( + _g->intro.sceneHandler.isEnd && + !_g->isStateTransition + ) + game_state_transition(_g, STATE_PLAY, TRUE, TRUE, TRUE, TRUE); + break; + case STATE_PLAY: + game_play_update(_g); + + + if + ( + keyboard_press(&_g->keyboard, KEYBOARD_ESCAPE) && + _g->play.isPaused && + !_g->isStateTransition + ) + game_state_transition(_g, STATE_TITLE, TRUE, TRUE, TRUE, TRUE); + else if + ( + _g->play.player.isDefeat && + !_g->isStateTransition + ) + game_state_transition(_g, STATE_ENDING_FAIL, TRUE, TRUE, TRUE, TRUE); + else if + ( + _g->play.isGameCleared && + !_g->isStateTransition + ) + { + if (_g->play.player.cash >= ENDING_THREE_CASH_THRESHOLD) + game_state_transition(_g, STATE_ENDING_THREE, TRUE, FALSE, TRUE, FALSE); + else if (_g->play.player.cash >= ENDING_TWO_CASH_THRESHOLD) + game_state_transition(_g, STATE_ENDING_TWO, TRUE, FALSE, TRUE, FALSE); + else + game_state_transition(_g, STATE_ENDING_ONE, TRUE, FALSE, TRUE, FALSE); + } + break; + case STATE_INFO: + game_info_update(_g); + + if + ( + _g->info.isBackPressed && + !_g->isStateTransition + ) + { + _g->info.isBackPressed = FALSE; + game_state_transition(_g, STATE_TITLE, TRUE, TRUE, TRUE, TRUE); + } + break; + case STATE_ENDING_FAIL: + game_ending_update(_g); + + if + ( + _g->ending.sceneHandler.isEnd && + !_g->isStateTransition + ) + game_state_transition(_g, STATE_RESULTS, TRUE, TRUE, TRUE, TRUE); + break; + case STATE_ENDING_ONE: + case STATE_ENDING_TWO: + case STATE_ENDING_THREE: + game_ending_update(_g); + + if (!_g->isStateTransition) + { + if (_g->ending.isNewGamePlus) + { + game_state_transition(_g, STATE_PLAY, FALSE, TRUE, FALSE, FALSE); + wave_new_game_plus(&_g->play); + _g->ending.isNewGamePlus = FALSE; + _g->ending.isActualEnding = FALSE; + } + else if (_g->ending.isResults) + { + game_state_transition(_g, STATE_RESULTS, TRUE, TRUE, TRUE, TRUE); + _g->ending.isResults = FALSE; + _g->ending.isActualEnding = FALSE; + } + } + + break; + case STATE_RESULTS: + game_results_update(_g); + + if + ( + _g->results.isEnd && + !_g->isStateTransition + ) + game_state_transition(_g, STATE_TITLE, TRUE, TRUE, TRUE, TRUE); + break; + default: + break; + } +} + +/* Draws the game's state. */ +void +game_state_draw(Game* _g) +{ + switch (_g->state) + { + case STATE_TITLE: + game_title_draw(_g); + break; + case STATE_INTRO: + game_intro_draw(_g); + break; + case STATE_PLAY: + game_play_draw(_g); + break; + case STATE_INFO: + game_info_draw(_g); + break; + case STATE_ENDING_FAIL: + case STATE_ENDING_ONE: + case STATE_ENDING_TWO: + case STATE_ENDING_THREE: + game_ending_draw(_g); + break; + case STATE_RESULTS: + game_results_draw(_g); + break; + default: + break; + } + + if (_g->isStateTransition) + { + f64 progress; + SDL_Rect rect; + SDL_Color color; + + progress = (f64)_g->stateTimer / STATE_TRANSITION_TIMER_MAX; + + rect.x = 0; + rect.y = 0; + rect.w = STATE_TRANSITION_RECTANGLE_WIDTH; + rect.h = STATE_TRANSITION_RECTANGLE_HEIGHT; + + color = STATE_TRANSITION_COLOR; + color.a = SDL_ALPHA_OPAQUE * progress; + + rectangle_draw(&rect, _g->renderer, color); + } +} diff --git a/src/game/game_state.h b/src/game/game_state.h new file mode 100755 index 0000000..692d57d --- /dev/null +++ b/src/game/game_state.h @@ -0,0 +1,24 @@ +/* + * DESCRIPTION: + * Header file for game_state.c. +*/ + +#pragma once + +#include "game_ending.h" +#include "game_info.h" +#include "game_play.h" +#include "game_intro.h" +#include "game_title.h" +#include "game_results.h" + +#define STATE_TRANSITION_TIMER_MAX 30 +#define STATE_TRANSITION_RECTANGLE_WIDTH 800 +#define STATE_TRANSITION_RECTANGLE_HEIGHT 600 + +static const SDL_Color STATE_TRANSITION_COLOR = {0, 0, 0, 255}; + +void game_state_free(Game* _g); +void game_state_update(Game* _g); +void game_state_draw(Game* _g); +void game_state_transition(Game* _g, StateType _s, bool_u8 _i, bool_u8 _f, bool_u8 _rI, bool_u8 _rF); diff --git a/src/game/game_texture.c b/src/game/game_texture.c new file mode 100755 index 0000000..e8a11f8 --- /dev/null +++ b/src/game/game_texture.c @@ -0,0 +1,37 @@ +/* + * DESCRIPTION: + * Loads/frees game textures. +*/ + +#include "game_texture.h" + +/* Loads a game texture. */ +void +game_texture_init(Game* _g, TextureType _t) +{ + if (!_g->textures[_t]) + texture_init(&_g->textures[_t], _g->renderer, (char*)TEXTURE_PATHS[_t]); +} + +/* Frees a game texture. */ +void +game_texture_free(Game* _g, TextureType _t) +{ + if (_g->textures[_t]) + { + texture_free(_g->textures[_t]); + _g->textures[_t] = NULL; + } +} + +/* Clears all textures. */ +void +game_texture_clear(Game* _g) +{ + for (s32 i = 0; i < TEXTURE_COUNT; i++) + { + if (_g->textures[i]) + game_texture_free(_g, (TextureType)i); + } +} + diff --git a/src/game/game_texture.h b/src/game/game_texture.h new file mode 100755 index 0000000..beaa760 --- /dev/null +++ b/src/game/game_texture.h @@ -0,0 +1,62 @@ +/* + * DESCRIPTION: + * Header file for game_texture.c. +*/ + +#pragma once + +#include "GAME_COMMON.h" + +static const char* TEXTURE_PATHS[TEXTURE_COUNT] = +{ + "", + "res/gfx/title/gfxLogo.png", + "res/gfx/gui/gfxGuiCursor.png", + "res/gfx/play/gui/gfxGuiSnake.png", + "res/gfx/play/gui/gfxGuiRedirect.png", + "res/gfx/play/gui/gfxGuiCash.png", + "res/gfx/play/gui/gfxGuiUpgradeSnake.png", + "res/gfx/play/gui/gfxGuiUpgradeLifetime.png", + "res/gfx/play/gui/gfxGuiUpgradeRedirect.png", + "res/gfx/gui/gfxGuiSignSmall.png", + "res/gfx/gui/gfxGuiSignBig.png", + "res/gfx/play/gfxShadow.png", + "res/gfx/play/player/gfxArc.png", + "res/gfx/play/bg/gfxPlayBG.png", + "res/gfx/play/player/gfxPlayerStageOne.png", + "res/gfx/play/player/gfxPlayerStageTwo.png", + "res/gfx/play/player/gfxPlayerStageThree.png", + "res/gfx/play/player/gfxPlayerTailStageOne.png", + "res/gfx/play/player/gfxPlayerTailStageTwo.png", + "res/gfx/play/player/gfxPlayerTailStageThree.png", + "res/gfx/play/player/gfxPlayerSnake.png", + "res/gfx/play/enemy/gfxEnemyCupper.png", + "res/gfx/play/enemy/gfxEnemyChipper.png", + "res/gfx/play/enemy/gfxEnemyChip.png", + "res/gfx/play/enemy/gfxEnemyCrumbler.png", + "res/gfx/play/collectible/gfxCollectibleDollars.png", + "res/gfx/play/collectible/gfxCollectibleSack.png", + "res/gfx/play/collectible/gfxCollectibleBullions.png", + "res/gfx/play/collectible/gfxCollectibleMiracleCure.png", + "res/gfx/play/collectible/gfxCollectibleDynamite.png", + "res/gfx/play/collectible/gfxCollectibleBerserk.png", + "res/gfx/info/gfxTheme.png", + "res/gfx/info/gfxWildcardAdmin.png", + "res/gfx/info/gfxWildcardPatreon.png", + "res/gfx/info/gfxWildcardCommunity.png", + "res/gfx/intro/gfxIntroSceneOne.png", + "res/gfx/intro/gfxIntroSceneTwo.png", + "res/gfx/intro/gfxIntroSceneThree.png", + "res/gfx/intro/gfxIntroSceneFour.png", + "res/gfx/intro/gfxIntroSceneFive.png", + "res/gfx/ending/gfxEndingFail.png", + "res/gfx/ending/gfxEndingOne.png", + "res/gfx/ending/gfxEndingTwo.png", + "res/gfx/ending/gfxEndingThree.png", + "res/gfx/title/gfxTitleBG.png", + "res/gfx/results/gfxResults.png", +}; + +void game_texture_init(Game* _g, TextureType _t); +void game_texture_free(Game* _g, TextureType _t); +void game_texture_clear(Game* _g); diff --git a/src/game/game_title.c b/src/game/game_title.c new file mode 100755 index 0000000..3e64abe --- /dev/null +++ b/src/game/game_title.c @@ -0,0 +1,102 @@ +/* + * DESCRIPTION: + * The "Title" state's meta functions. +*/ + +#include "game_title.h" + +/* Initializes the Play state's resources. */ +void +game_title_init(Game* _g) +{ + for (s32 i = 0; i < TITLE_TEXTURE_COUNT; i++) + { + s32 index; + + index = TITLE_TEXTURES[i]; + + game_texture_init(_g, (TextureType)index); + } + + for (s32 i = 0; i < TITLE_SOUND_COUNT; i++) + { + s32 index; + + index = TITLE_SOUNDS[i]; + + game_sound_init(_g, (SoundType)index); + } + + for (s32 i = 0; i < TITLE_FONT_COUNT; i++) + { + s32 index; + + index = TITLE_FONTS[i]; + + game_font_init(_g, (FontType)index); + } + + for (s32 i = 0; i < TITLE_MUSIC_COUNT; i++) + { + s32 index; + + index = TITLE_MUSIC[i]; + + game_music_init(_g, (MusicType)index); + } +} + +/* Frees the Play state's resources. */ +void +game_title_free(Game* _g) +{ + for (s32 i = 0; i < TITLE_TEXTURE_COUNT; i++) + { + s32 index; + + index = TITLE_TEXTURES[i]; + + game_texture_free(_g, (TextureType)index); + } + + for (s32 i = 0; i < TITLE_SOUND_COUNT; i++) + { + s32 index; + + index = TITLE_SOUNDS[i]; + + game_sound_free(_g, (SoundType)index); + } + + for (s32 i = 0; i < TITLE_FONT_COUNT; i++) + { + s32 index; + + index = TITLE_FONTS[i]; + + game_font_free(_g, (FontType)index); + } + + for (s32 i = 0; i < TITLE_MUSIC_COUNT; i++) + { + s32 index; + + index = TITLE_MUSIC[i]; + + game_music_free(_g, (MusicType)index); + } +} + +/* Updates the Play state. */ +void +game_title_update(Game* _g) +{ + title_update(&_g->title); +} + +/* Draws the Play state. */ +void +game_title_draw(Game* _g) +{ + title_draw(&_g->title); +} diff --git a/src/game/game_title.h b/src/game/game_title.h new file mode 100755 index 0000000..5e0c386 --- /dev/null +++ b/src/game/game_title.h @@ -0,0 +1,48 @@ +/* + * DESCRIPTION: + * Header file for game_title.c. +*/ + +#pragma once + +#include "title/title.h" + +#include "game_font.h" +#include "game_music.h" +#include "game_sound.h" +#include "game_texture.h" + +#define TITLE_TEXTURE_COUNT 5 +#define TITLE_SOUND_COUNT 1 +#define TITLE_MUSIC_COUNT 1 +#define TITLE_FONT_COUNT 2 + +static const TextureType TITLE_TEXTURES[TITLE_TEXTURE_COUNT] = +{ + TEXTURE_LOGO, + TEXTURE_GUI_SIGN_SMALL, + TEXTURE_GUI_SIGN_BIG, + TEXTURE_GUI_CURSOR, + TEXTURE_TITLE_BACKGROUND +}; + +static const SoundType TITLE_SOUNDS[TITLE_SOUND_COUNT] = +{ + SOUND_MENU +}; + +static const MusicType TITLE_MUSIC[TITLE_MUSIC_COUNT] = +{ + MUSIC_TITLE +}; + +static const FontType TITLE_FONTS[TITLE_FONT_COUNT] = +{ + FONT_MEDIUM, + FONT_BIG +}; + +void game_title_draw(Game* _g); +void game_title_free(Game* _g); +void game_title_init(Game* _g); +void game_title_update(Game* _g); diff --git a/src/game/gui/button.c b/src/game/gui/button.c new file mode 100755 index 0000000..2e7c192 --- /dev/null +++ b/src/game/gui/button.c @@ -0,0 +1,67 @@ +/* + * DESCRIPTION: + * Basic GUI button. +*/ + +#include "button.h" + +/* Initializes a button. */ +void +button_init(Button* _b, Sprite* _s) +{ + memset(_b, '\0', sizeof(Button)); + + _b->sprite = *_s; +} + +/* Updates a button. */ +void +button_update(Button* _b, Mouse* _m) +{ + SDL_Rect rect; + + rect = sprite_rect_get(&_b->sprite); + + _b->state = BUTTON_STATE_NONE; + + if (rectangle_point_collide(&rect, _m->x, _m->y)) + { + if (_b->state == BUTTON_STATE_HOVER) + _b->state = BUTTON_STATE_HOVERING; + else + _b->state = BUTTON_STATE_HOVER; + + if (mouse_held(_m, MOUSE_LEFT)) + _b->state = BUTTON_STATE_HELD; + else if (mouse_press(_m, MOUSE_LEFT)) + _b->state = BUTTON_STATE_PRESS; + + } + else + _b->state = BUTTON_STATE_NONE; + + switch (_b->state) + { + case BUTTON_STATE_PRESS: + _b->sprite.color = BUTTON_PRESS_COLOR; + break; + case BUTTON_STATE_HELD: + _b->sprite.color = BUTTON_HELD_COLOR; + break; + case BUTTON_STATE_HOVER: + case BUTTON_STATE_HOVERING: + _b->sprite.color = BUTTON_HOVER_COLOR; + break; + case BUTTON_STATE_NONE: + default: + _b->sprite.color = BUTTON_NONE_COLOR; + break; + } +} + +/* Draws a button. */ +void +button_draw(Button* _b, SDL_Renderer* _r) +{ + sprite_draw(&_b->sprite, _r); +} diff --git a/src/game/gui/button.h b/src/game/gui/button.h new file mode 100755 index 0000000..c1e519c --- /dev/null +++ b/src/game/gui/button.h @@ -0,0 +1,17 @@ +/* + * DESCRIPTION: + * Header file for button.c. +*/ + +#pragma once + +#include "../GAME_COMMON.h" + +static const SDL_Color BUTTON_NONE_COLOR = {255, 255, 255, 255}; +static const SDL_Color BUTTON_HOVER_COLOR = {192, 192, 192, 255}; +static const SDL_Color BUTTON_PRESS_COLOR = {128, 128, 128, 255}; +static const SDL_Color BUTTON_HELD_COLOR = {64, 64, 6, 255}; + +void button_init(Button* _b, Sprite* _s); +void button_update(Button* _b, Mouse* _m); +void button_draw(Button* _b, SDL_Renderer* _r); diff --git a/src/game/gui/cursor.c b/src/game/gui/cursor.c new file mode 100755 index 0000000..75f3e76 --- /dev/null +++ b/src/game/gui/cursor.c @@ -0,0 +1,73 @@ +/* + * DESCRIPTION: + * Gameplay's cursor. +*/ + +#include "cursor.h" + +/* Initializes the cursor. */ +void +cursor_init(Cursor* _c, Mouse* _m, SDL_Texture* _t) +{ + Atlas atlas; + + atlas_init + ( + &atlas, + _t, + CURSOR_ROWS, + CURSOR_COLUMNS, + CURSOR_FRAME_WIDTH, + CURSOR_FRAME_HEIGHT + ); + + sprite_atlas_init + ( + &_c->sprite, + &atlas, + ORIGIN_MIDDLE, + CURSOR_WIDTH, + CURSOR_HEIGHT + ); + + _c->sprite.frame = 0; + _c->sprite.x = _m->x; + _c->sprite.y = _m->y; + + _c->sprite.color.a = CURSOR_ALPHA; +} + +/* Frees cursor resources. */ +void +cursor_free(Cursor* _c) +{ + +} + +/* Updates the cursor. */ +void +cursor_update(Cursor* _c, Mouse* _m) +{ + if (mouse_held(_m, MOUSE_LEFT)) + _c->sprite.frame = 1; + else if (mouse_held(_m, MOUSE_RIGHT)) + _c->sprite.frame = 2; + else + _c->sprite.frame = 0; + + _c->sprite.x = _m->x; + _c->sprite.y = _m->y; + + _c->sprite.x = _c->sprite.x < CURSOR_X_MIN ? CURSOR_X_MIN : _c->sprite.x; + _c->sprite.x = _c->sprite.x > CURSOR_X_MAX ? CURSOR_X_MAX : _c->sprite.x; + _c->sprite.y = _c->sprite.y < CURSOR_Y_MIN ? CURSOR_Y_MIN : _c->sprite.y; + _c->sprite.y = _c->sprite.y > CURSOR_Y_MAX ? CURSOR_Y_MAX : _c->sprite.y; +} + +/* Draws the cursor. */ +void +cursor_draw(Cursor* _c, SDL_Renderer* _r) +{ + sprite_draw(&_c->sprite, _r); + +} diff --git a/src/game/gui/cursor.h b/src/game/gui/cursor.h new file mode 100755 index 0000000..45f0bdd --- /dev/null +++ b/src/game/gui/cursor.h @@ -0,0 +1,25 @@ +/* + * DESCRIPTION: + * Header file for cursor.c. +*/ + +#pragma once + +#include "../GAME_COMMON.h" + +#define CURSOR_WIDTH 32 +#define CURSOR_HEIGHT 32 +#define CURSOR_FRAME_WIDTH 32 +#define CURSOR_FRAME_HEIGHT 32 +#define CURSOR_ROWS 1 +#define CURSOR_COLUMNS 3 +#define CURSOR_ALPHA 128 +#define CURSOR_X_MAX 800 +#define CURSOR_X_MIN 0 +#define CURSOR_Y_MAX 600 +#define CURSOR_Y_MIN 0 + +void cursor_draw(Cursor* _c, SDL_Renderer* _r); +void cursor_free(Cursor* _c); +void cursor_init(Cursor* _c, Mouse* _m, SDL_Texture* _t); +void cursor_update(Cursor* _c, Mouse* _m); diff --git a/src/game/info/info.c b/src/game/info/info.c new file mode 100755 index 0000000..a97e327 --- /dev/null +++ b/src/game/info/info.c @@ -0,0 +1,239 @@ +/* + * DESCRIPTION: + * Displays game info (credits/licenses). +*/ + +#include "info.h" + +/* Initialize info. */ +void +info_init(Info* _i, Game* _g) +{ + Sprite buttonSprite; + + memset(_i, '\0', sizeof(Info)); + + _i->g = _g; + + cursor_init(&_i->cursor, &_i->g->mouse, _i->g->textures[TEXTURE_GUI_CURSOR]); + + text_sprite_init + ( + &_i->licenseText, + _i->g->renderer, + _i->g->fonts[FONT_SMALL], + INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_INFO_LICENSES, + INFO_LICENSE_TEXT_WRAP + ); + + _i->licenseText.x = INFO_LICENSE_TEXT_OFFSET_X; + _i->licenseText.y = INFO_LICENSE_TEXT_OFFSET_Y; + + sprite_init + ( + &_i->themeSprite, + _i->g->textures[TEXTURE_INFO_THEME], + ORIGIN_MIDDLE, + INFO_THEME_WIDTH, + INFO_THEME_HEIGHT + ); + + _i->themeSprite.x = INFO_THEME_OFFSET_X; + _i->themeSprite.y = INFO_THEME_OFFSET_Y; + + sprite_init + ( + &_i->wildcardAdminSprite, + _i->g->textures[TEXTURE_INFO_WILDCARD_ADMIN], + ORIGIN_MIDDLE, + INFO_WILDCARD_WIDTH, + INFO_WILDCARD_HEIGHT + ); + + _i->wildcardAdminSprite.x = INFO_WILDCARD_ADMIN_OFFSET_X; + _i->wildcardAdminSprite.y = INFO_WILDCARD_ADMIN_OFFSET_Y; + + sprite_init + ( + &_i->wildcardPatreonSprite, + _i->g->textures[TEXTURE_INFO_WILDCARD_PATREON], + ORIGIN_MIDDLE, + INFO_WILDCARD_WIDTH, + INFO_WILDCARD_HEIGHT + ); + + _i->wildcardPatreonSprite.x = INFO_WILDCARD_PATREON_OFFSET_X; + _i->wildcardPatreonSprite.y = INFO_WILDCARD_PATREON_OFFSET_Y; + + sprite_init + ( + &_i->wildcardCommunitySprite, + _i->g->textures[TEXTURE_INFO_WILDCARD_COMMUNITY], + ORIGIN_MIDDLE, + INFO_WILDCARD_WIDTH, + INFO_WILDCARD_HEIGHT + ); + + _i->wildcardCommunitySprite.x = INFO_WILDCARD_COMMUNITY_OFFSET_X; + _i->wildcardCommunitySprite.y = INFO_WILDCARD_COMMUNITY_OFFSET_Y; + + sprite_init + ( + &_i->backBackgroundSprite, + _i->g->textures[TEXTURE_GUI_SIGN_SMALL], + ORIGIN_MIDDLE, + INFO_BUTTON_BACKGROUND_WIDTH, + INFO_BUTTON_BACKGROUND_HEIGHT + ); + + _i->backBackgroundSprite.x = INFO_BUTTON_OFFSET_X; + _i->backBackgroundSprite.y = INFO_BUTTON_OFFSET_Y; + + text_sprite_init + ( + &buttonSprite, + _i->g->renderer, + _i->g->fonts[FONT_MEDIUM], + INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_INFO_BACK, + 0 + ); + + buttonSprite.rotation = INFO_BUTTON_TEXT_ROTATION; + + buttonSprite.x = _i->backBackgroundSprite.x; + buttonSprite.y = _i->backBackgroundSprite.y; + + button_init(&_i->backButton, &buttonSprite); + + text_sprite_init + ( + &_i->themeText, + _i->g->renderer, + _i->g->fonts[FONT_TINY], + INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_INFO_THEME, + INFO_THEME_WRAP + ); + + _i->themeText.x = INFO_THEME_TEXT_OFFSET_X; + _i->themeText.y = INFO_THEME_TEXT_OFFSET_Y; + + text_sprite_init + ( + &_i->wildcardAdminText, + _i->g->renderer, + _i->g->fonts[FONT_TINY], + INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_INFO_WILDCARD_ADMIN, + INFO_WILDCARD_WRAP + ); + + _i->wildcardAdminText.x = INFO_WILDCARD_ADMIN_TEXT_OFFSET_X; + _i->wildcardAdminText.y = INFO_WILDCARD_ADMIN_TEXT_OFFSET_Y; + + text_sprite_init + ( + &_i->wildcardPatreonText, + _i->g->renderer, + _i->g->fonts[FONT_TINY], + INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_INFO_WILDCARD_PATREON, + INFO_WILDCARD_WRAP + ); + + _i->wildcardPatreonText.x = INFO_WILDCARD_PATREON_TEXT_OFFSET_X; + _i->wildcardPatreonText.y = INFO_WILDCARD_PATREON_TEXT_OFFSET_Y; + + text_sprite_init + ( + &_i->wildcardCommunityText, + _i->g->renderer, + _i->g->fonts[FONT_TINY], + INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_INFO_WILDCARD_COMMUNITY, + INFO_WILDCARD_WRAP + ); + + _i->wildcardCommunityText.x = INFO_WILDCARD_COMMUNITY_TEXT_OFFSET_X; + _i->wildcardCommunityText.y = INFO_WILDCARD_COMMUNITY_TEXT_OFFSET_Y; +} + +/* Frees info. */ +void +info_free(Info* _i) +{ + if (_i->licenseText.atlas.texture) + texture_free(_i->licenseText.atlas.texture); + + if (_i->backButton.sprite.atlas.texture) + texture_free(_i->backButton.sprite.atlas.texture); + + if (_i->themeText.atlas.texture) + texture_free(_i->themeText.atlas.texture); + + if (_i->wildcardAdminText.atlas.texture) + texture_free(_i->wildcardAdminText.atlas.texture); + + if (_i->wildcardPatreonText.atlas.texture) + texture_free(_i->wildcardPatreonText.atlas.texture); + + if (_i->wildcardCommunityText.atlas.texture) + texture_free(_i->wildcardCommunityText.atlas.texture); + + cursor_free(&_i->cursor); +} + +/* Updates info. */ +void +info_update(Info* _i) +{ + cursor_update(&_i->cursor, &_i->g->mouse); + + button_update(&_i->backButton, &_i->g->mouse); + + if (_i->backButton.state == BUTTON_STATE_PRESS) + { + _i->isBackPressed = TRUE; + + sound_play(_i->g->sounds[SOUND_MENU], -1); + } +} + +/* Draw info. */ +void +info_draw(Info* _i) +{ + SDL_Rect r; + + r.x = 0; + r.y = 0; + r.w = INFO_RECTANGLE_WIDTH; + r.h = INFO_RECTANGLE_HEIGHT; + + rectangle_draw(&r, _i->g->renderer, INFO_BACKGROUND_COLOR); + + sprite_draw(&_i->licenseText, _i->g->renderer); + + sprite_draw(&_i->themeSprite, _i->g->renderer); + sprite_draw(&_i->wildcardAdminSprite, _i->g->renderer); + sprite_draw(&_i->wildcardPatreonSprite, _i->g->renderer); + sprite_draw(&_i->wildcardCommunitySprite, _i->g->renderer); + + sprite_draw(&_i->themeText, _i->g->renderer); + sprite_draw(&_i->wildcardAdminText, _i->g->renderer); + sprite_draw(&_i->wildcardPatreonText, _i->g->renderer); + sprite_draw(&_i->wildcardCommunityText, _i->g->renderer); + + sprite_draw(&_i->backBackgroundSprite, _i->g->renderer); + button_draw(&_i->backButton, _i->g->renderer); + + cursor_draw(&_i->cursor, _i->g->renderer); +} diff --git a/src/game/info/info.h b/src/game/info/info.h new file mode 100755 index 0000000..e111195 --- /dev/null +++ b/src/game/info/info.h @@ -0,0 +1,52 @@ +/* + * DESCRIPTION: + * Header file for info.c. +*/ + +#pragma once + +#include "../GAME_STRINGS.h" +#include "../gui/button.h" +#include "../gui/cursor.h" + +#define INFO_LICENSE_TEXT_OFFSET_X 400 +#define INFO_BUTTON_OFFSET_X 750 +#define INFO_BUTTON_OFFSET_Y 50 +#define INFO_LICENSE_TEXT_OFFSET_X 400 +#define INFO_LICENSE_TEXT_OFFSET_Y 80 +#define INFO_BUTTON_TEXT_ROTATION -5 +#define INFO_BUTTON_BACKGROUND_WIDTH 96 +#define INFO_BUTTON_BACKGROUND_HEIGHT 96 +#define INFO_WILDCARD_WIDTH 175 +#define INFO_WILDCARD_HEIGHT 225 +#define INFO_THEME_WIDTH 225 +#define INFO_THEME_HEIGHT 60 +#define INFO_THEME_OFFSET_X 175 +#define INFO_THEME_OFFSET_Y 200 +#define INFO_WILDCARD_ADMIN_OFFSET_X 100 +#define INFO_WILDCARD_PATREON_OFFSET_X 475 +#define INFO_WILDCARD_COMMUNITY_OFFSET_X 475 +#define INFO_WILDCARD_ADMIN_OFFSET_Y 480 +#define INFO_WILDCARD_PATREON_OFFSET_Y 255 +#define INFO_WILDCARD_COMMUNITY_OFFSET_Y 480 +#define INFO_WILDCARD_ADMIN_TEXT_OFFSET_X 290 +#define INFO_WILDCARD_ADMIN_TEXT_OFFSET_Y 480 +#define INFO_WILDCARD_COMMUNITY_TEXT_OFFSET_X 675 +#define INFO_WILDCARD_COMMUNITY_TEXT_OFFSET_Y 480 +#define INFO_WILDCARD_PATREON_TEXT_OFFSET_X 675 +#define INFO_WILDCARD_PATREON_TEXT_OFFSET_Y 255 +#define INFO_THEME_TEXT_OFFSET_X 200 +#define INFO_THEME_TEXT_OFFSET_Y 300 +#define INFO_THEME_WRAP 350 +#define INFO_WILDCARD_WRAP 200 +#define INFO_LICENSE_TEXT_WRAP 500 +#define INFO_RECTANGLE_WIDTH 800 +#define INFO_RECTANGLE_HEIGHT 600 + +static const SDL_Color INFO_BACKGROUND_COLOR = {0, 0, 0, 255}; +static const SDL_Color INFO_TEXT_COLOR = {255, 255, 255, 255}; + +void info_draw(Info* _i); +void info_free(Info* _i); +void info_init(Info* _i, Game* _g); +void info_update(Info* _i); diff --git a/src/game/intro/intro.c b/src/game/intro/intro.c new file mode 100755 index 0000000..7b5f4f4 --- /dev/null +++ b/src/game/intro/intro.c @@ -0,0 +1,46 @@ +/* + * DESCRIPTION: + * Intro code. +*/ + +#include "intro.h" + +/* Initializes intro. */ +void +intro_init(Intro* _i, Game* _g) +{ + memset(_i, '\0', sizeof(Intro)); + + _i->g = _g; + + scene_init + ( + &_i->sceneHandler, + _i->g, + (Scene*)INTRO_SCENES, + INTRO_SCENE_COUNT + ); + + music_play(_i->g->music[MUSIC_INTRO], TRUE); +} + +/* Frees intro. */ +void +intro_free(Intro* _i) +{ + scene_free(&_i->sceneHandler, _i->g); +} + +/* Updates intro. */ +void +intro_update(Intro* _i) +{ + scene_update(&_i->sceneHandler, _i->g); +} + +/* Draws intro. */ +void +intro_draw(Intro* _i) +{ + scene_draw(&_i->sceneHandler, _i->g); +} diff --git a/src/game/intro/intro.h b/src/game/intro/intro.h new file mode 100755 index 0000000..3afacb2 --- /dev/null +++ b/src/game/intro/intro.h @@ -0,0 +1,40 @@ +/* + * DESCRIPTION: + * Header file for intro.c. +*/ + +#pragma once + +#include "../scene/scene.h" +#include "../GAME_STRINGS.h" + +#define INTRO_SCENE_COUNT 5 + +static const Scene INTRO_SCENES[INTRO_SCENE_COUNT] = +{ + { + .textureType = TEXTURE_INTRO_SCENE_ONE, + .text = STRING_INTRO_SCENE_ONE + }, + { + .textureType = TEXTURE_INTRO_SCENE_TWO, + .text = STRING_INTRO_SCENE_TWO + }, + { + .textureType = TEXTURE_INTRO_SCENE_THREE, + .text = STRING_INTRO_SCENE_THREE + }, + { + .textureType = TEXTURE_INTRO_SCENE_FOUR, + .text = STRING_INTRO_SCENE_FOUR + }, + { + .textureType = TEXTURE_INTRO_SCENE_FIVE, + .text = STRING_INTRO_SCENE_FIVE + } +}; + +void intro_update(Intro* _i); +void intro_init(Intro* _i, Game* _g); +void intro_draw(Intro* _i); +void intro_free(Intro* _i); diff --git a/src/game/play/collectible/berserk.c b/src/game/play/collectible/berserk.c new file mode 100755 index 0000000..24a51dd --- /dev/null +++ b/src/game/play/collectible/berserk.c @@ -0,0 +1,35 @@ +/* + * DESCRIPTION: + * Berserk powerup. +*/ + +#include "berserk.h" + +/* Initializes the berserk. */ +void +berserk_init(Collectible* _c, List* _l, Play* _p) +{ + sprite_init + ( + &_c->sprite, + _p->g->textures[TEXTURE_COLLECTIBLE_BERSERK], + ORIGIN_MIDDLE, + BERSERK_WIDTH, + BERSERK_HEIGHT + ); +} + +/* What happens when you collect the berserk? */ +void +berserk_collect(Collectible* _c, List* _l, Play* _p) +{ + _p->player.isPowerUp = TRUE; + + _p->player.isSnakeUpdate = TRUE; + + _p->player.powerUpTimer = BERSERK_PLAYER_TIMER_MAX; + + sound_play(_p->g->sounds[SOUND_BERSERK], -1); + + message_power_up_add(_c->sprite.x, _c->sprite.y, _p); +} diff --git a/src/game/play/collectible/berserk.h b/src/game/play/collectible/berserk.h new file mode 100755 index 0000000..6708294 --- /dev/null +++ b/src/game/play/collectible/berserk.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for berserk.c. +*/ + +#include "../message/message.h" + +#define BERSERK_WIDTH 64 +#define BERSERK_HEIGHT 64 +#define BERSERK_PLAYER_TIMER_MAX 600 + +void berserk_init(Collectible* _c, List* _l, Play* _p); +void berserk_collect(Collectible* _c, List* _l, Play* _p); diff --git a/src/game/play/collectible/bullions.c b/src/game/play/collectible/bullions.c new file mode 100755 index 0000000..02e157d --- /dev/null +++ b/src/game/play/collectible/bullions.c @@ -0,0 +1,29 @@ +/* + * DESCRIPTION: + * Large money collectible. +*/ + +#include "bullions.h" + +/* Initializes the bullions. */ +void +bullions_init(Collectible* _c, List* _l, Play* _p) +{ + sprite_init + ( + &_c->sprite, + _p->g->textures[TEXTURE_COLLECTIBLE_BULLIONS], + ORIGIN_MIDDLE, + BULLIONS_WIDTH, + BULLIONS_HEIGHT + ); +} + +/* What happens when you collect the bullions? */ +void +bullions_collect(Collectible* _c, List* _l, Play* _p) +{ + player_cash_acquire(&_p->player, BULLIONS_CASH_VALUE, _p); + + sound_play(_p->g->sounds[SOUND_BULLIONS], -1); +} diff --git a/src/game/play/collectible/bullions.h b/src/game/play/collectible/bullions.h new file mode 100755 index 0000000..0701cb1 --- /dev/null +++ b/src/game/play/collectible/bullions.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for bullions.c. +*/ + +#include "../player/player_cash.h" + +#define BULLIONS_WIDTH 64 +#define BULLIONS_HEIGHT 64 +#define BULLIONS_CASH_VALUE 500 + +void bullions_init(Collectible* _c, List* _l, Play* _p); +void bullions_collect(Collectible* _c, List* _l, Play* _p); diff --git a/src/game/play/collectible/collectible.c b/src/game/play/collectible/collectible.c new file mode 100755 index 0000000..f3f36cc --- /dev/null +++ b/src/game/play/collectible/collectible.c @@ -0,0 +1,158 @@ +/* + * DESCRIPTION: + * Collectible code. +*/ + +#include "collectible.h" + +/* Adds a new collectible. */ +Collectible* +collectible_add(List* _l, CollectibleType _c, f64 _x, f64 _y, Play* _p) +{ + Collectible collectible; + f64 angle; + + memset(&collectible, '\0', sizeof(Collectible)); + + collectible.type = _c; + + switch (collectible.type) + { + case COLLECTIBLE_DOLLARS: + dollars_init(&collectible, _l, _p); + break; + case COLLECTIBLE_SACK: + sack_init(&collectible, _l, _p); + break; + case COLLECTIBLE_BULLIONS: + bullions_init(&collectible, _l, _p); + break; + case COLLECTIBLE_MIRACLE_CURE: + miracle_cure_init(&collectible, _l, _p); + break; + case COLLECTIBLE_DYNAMITE: + dynamite_init(&collectible, _l, _p); + break; + case COLLECTIBLE_BERSERK: + berserk_init(&collectible, _l, _p); + break; + default: + break; + } + + + collectible.sprite.x = _x; + collectible.sprite.y = _y; + + collectible.shadow.x = collectible.sprite.x + COLLECTIBLE_SHADOW_OFFSET_Y; + collectible.shadow.y = collectible.sprite.y + COLLECTIBLE_SHADOW_OFFSET_Y; + + collectible.shadow.r = COLLECTIBLE_SHADOW_RADIUS; + + collectible.lifetime = COLLECTIBLE_LIFETIME; + + collectible.isActive = FALSE; + collectible.activeTimer = COLLECTIBLE_ACTIVE_TIMER_MAX; + + angle = random_f64_in_range_get(0, ROTATION_MAX_RAD); + + collectible.dX = cos(angle) * COLLECTIBLE_VELOCITY; + collectible.dY = sin(angle) * COLLECTIBLE_VELOCITY; + + sound_play(_p->g->sounds[SOUND_COLLECTIBLE], -1); + + return (Collectible*)list_append_new(_l, (void*)&collectible, sizeof(Collectible)); +} + +/* Removes a collectible. */ +void +collectible_remove(Collectible* _c, List* _l, Play* _p) +{ + list_node_from_data_remove(_l, _c, sizeof(Collectible)); +} + +/* A collectible is collected. */ +void +collectible_collect(Collectible* _c, List* _l, Play* _p) +{ + switch (_c->type) + { + case COLLECTIBLE_DOLLARS: + dollars_collect(_c, _l, _p); + break; + case COLLECTIBLE_SACK: + sack_collect(_c, _l, _p); + break; + case COLLECTIBLE_BULLIONS: + bullions_collect(_c, _l, _p); + break; + case COLLECTIBLE_MIRACLE_CURE: + miracle_cure_collect(_c, _l, _p); + break; + case COLLECTIBLE_DYNAMITE: + dynamite_collect(_c, _l, _p); + break; + case COLLECTIBLE_BERSERK: + berserk_collect(_c, _l, _p); + break; + default: + break; + } + + collectible_remove(_c, _l, _p); +} + +/* Updates a collectible. */ +void +collectible_update(Collectible* _c, List* _l, Play* _p) +{ + _c->sprite.x += _c->dX; + _c->sprite.y += _c->dY; + + _c->shadow.x = _c->sprite.x + COLLECTIBLE_SHADOW_OFFSET_X; + _c->shadow.y = _c->sprite.y + COLLECTIBLE_SHADOW_OFFSET_Y; + + _c->dX *= COLLECTIBLE_FRICTION; + _c->dY *= COLLECTIBLE_FRICTION; + + _c->lifetime--; + + if (!_c->isActive) + { + f64 progress; + + _c->activeTimer--; + + progress = (f64)_c->activeTimer / COLLECTIBLE_ACTIVE_TIMER_MAX; + + _c->sprite.color.a = SDL_ALPHA_OPAQUE * (1 - progress); + + if (_c->activeTimer <= 0) + { + _c->sprite.color.a = SDL_ALPHA_OPAQUE; + _c->activeTimer = 0; + _c->isActive = TRUE; + } + } + + if (_c->attachedSnake) + { + _c->sprite.x = _c->attachedSnake->sprite.x; + _c->sprite.y = _c->attachedSnake->sprite.y; + } + + if (circle_collide(&_c->shadow, &_p->player.shadow)) + collectible_collect(_c, _l, _p); + else + { + if (_c->lifetime <= 0) + collectible_remove(_c, _l, _p); + else if (_c->lifetime <= COLLECTIBLE_DISAPPEAR_THRESHOLD) + { + if (_c->lifetime % COLLECTIBLE_DISAPPEAR_MODULO == 0) + _c->sprite.color.a = 0; + else + _c->sprite.color.a = SDL_ALPHA_OPAQUE; + } + } +} diff --git a/src/game/play/collectible/collectible.h b/src/game/play/collectible/collectible.h new file mode 100755 index 0000000..8aaa00b --- /dev/null +++ b/src/game/play/collectible/collectible.h @@ -0,0 +1,28 @@ +/* + * DESCRIPTION: + * Header file for collectible.c. +*/ + +#pragma once + +#include "bullions.h" +#include "dollars.h" +#include "sack.h" +#include "berserk.h" +#include "dynamite.h" +#include "miracle_cure.h" + +#define COLLECTIBLE_ACTIVE_TIMER_MAX 30 +#define COLLECTIBLE_SHADOW_RADIUS 16 +#define COLLECTIBLE_SHADOW_OFFSET_X 0 +#define COLLECTIBLE_SHADOW_OFFSET_Y 16 +#define COLLECTIBLE_FRICTION 0.90 +#define COLLECTIBLE_VELOCITY 3 +#define COLLECTIBLE_DISAPPEAR_THRESHOLD 60 +#define COLLECTIBLE_DISAPPEAR_MODULO 5 +#define COLLECTIBLE_LIFETIME 500 + +Collectible* collectible_add(List* _l, CollectibleType _c, f64 _x, f64 _y, Play* _p); +void collectible_remove(Collectible* _c, List* _l, Play* _p); +void collectible_update(Collectible* _c, List* _l, Play* _p); +void collectible_collect(Collectible* _c, List* _l, Play* _p); diff --git a/src/game/play/collectible/collectibles.c b/src/game/play/collectible/collectibles.c new file mode 100755 index 0000000..b0625b7 --- /dev/null +++ b/src/game/play/collectible/collectibles.c @@ -0,0 +1,73 @@ +/* + * DESCRIPTION: + * Collectbles; the money/powerups that come out of enemies. +*/ + +#include "collectibles.h" + +/* Initializes collectibles. */ +void +collectibles_init(List* _l, Play* _p) +{ + list_init(_l); +} + +/* Initializes collectibles. */ +void +collectibles_free(List* _l, Play* _p) +{ + list_free(_l); +} + +/* Updates collectibles. */ +void +collectibles_update(List* _l, Play* _p) +{ + Collectible** collectibles; + s32 collectibleCount; + + collectibles = (Collectible**)list_data_get(_l); + collectibleCount = _l->count; + + for (s32 i = 0; i < collectibleCount; i++) + { + Collectible* c; + + c = collectibles[i]; + + collectible_update(c, _l, _p); + } + + free(collectibles); +} + +/* Draws all collectibles. */ +void +collectibles_draw(List* _l, Play* _p) +{ + Collectible** collectibles; + s32 collectibleCount; + + collectibles = (Collectible**)list_data_get(_l); + collectibleCount = _l->count; + + for (s32 i = 0; i < collectibleCount; i++) + { + Collectible* c; + + c = collectibles[i]; + + shadow_draw(&c->shadow, _p); + } + + for (s32 i = 0; i < collectibleCount; i++) + { + Collectible* c; + + c = collectibles[i]; + + sprite_draw(&c->sprite, _p->g->renderer); + } + + free(collectibles); +} diff --git a/src/game/play/collectible/collectibles.h b/src/game/play/collectible/collectibles.h new file mode 100755 index 0000000..ee8101f --- /dev/null +++ b/src/game/play/collectible/collectibles.h @@ -0,0 +1,16 @@ +/* + * DESCRIPTION: + * Header file for collectibles.c. +*/ + +#pragma once + +#include "collectible.h" +#include "../shadow/shadow.h" + +static const SDL_Color COLLECTIBLE_SHADOW_COLOR = {0, 0, 0, 64}; + +void collectibles_init(List* _l, Play* _p); +void collectibles_free(List* _l, Play* _p); +void collectibles_update(List* _l, Play* _p); +void collectibles_draw(List* _l, Play* _p); diff --git a/src/game/play/collectible/dollars.c b/src/game/play/collectible/dollars.c new file mode 100755 index 0000000..d0c207d --- /dev/null +++ b/src/game/play/collectible/dollars.c @@ -0,0 +1,29 @@ +/* + * DESCRIPTION: + * Small money collectible. +*/ + +#include "dollars.h" + +/* Initializes the dollars. */ +void +dollars_init(Collectible* _c, List* _l, Play* _p) +{ + sprite_init + ( + &_c->sprite, + _p->g->textures[TEXTURE_COLLECTIBLE_DOLLARS], + ORIGIN_MIDDLE, + DOLLARS_WIDTH, + DOLLARS_HEIGHT + ); +} + +/* What happens when you collect the dollars? */ +void +dollars_collect(Collectible* _c, List* _l, Play* _p) +{ + player_cash_acquire(&_p->player, DOLLARS_CASH_VALUE, _p); + + sound_play(_p->g->sounds[SOUND_DOLLARS], -1); +} diff --git a/src/game/play/collectible/dollars.h b/src/game/play/collectible/dollars.h new file mode 100755 index 0000000..030ea57 --- /dev/null +++ b/src/game/play/collectible/dollars.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for dollars.c. +*/ + +#include "../player/player_cash.h" + +#define DOLLARS_WIDTH 32 +#define DOLLARS_HEIGHT 32 +#define DOLLARS_CASH_VALUE 100 + +void dollars_init(Collectible* _c, List* _l, Play* _p); +void dollars_collect(Collectible* _c, List* _l, Play* _p); diff --git a/src/game/play/collectible/dynamite.c b/src/game/play/collectible/dynamite.c new file mode 100755 index 0000000..88d6b0f --- /dev/null +++ b/src/game/play/collectible/dynamite.c @@ -0,0 +1,46 @@ +/* + * DESCRIPTION: + * Dynamite collectible; blows stuff up. +*/ + +#include "dynamite.h" + +/* Initializes the dynamite. */ +void +dynamite_init(Collectible* _c, List* _l, Play* _p) +{ + sprite_init + ( + &_c->sprite, + _p->g->textures[TEXTURE_COLLECTIBLE_DYNAMITE], + ORIGIN_MIDDLE, + DYNAMITE_WIDTH, + DYNAMITE_HEIGHT + ); +} + +/* What happens when you collect the dynamite? */ +void +dynamite_collect(Collectible* _c, List* _l, Play* _p) +{ + Enemy** enemies; + s32 enemyCount; + + enemies = (Enemy**)list_data_get(&_p->enemies); + enemyCount = _p->enemies.count; + + for (s32 i = 0; i < enemyCount; i++) + { + Enemy* e; + + e = enemies[i]; + + enemy_damage(e, DYNAMITE_DAMAGE, &_p->enemies, _p); + } + + free(enemies); + + message_dynamite_add(_c->sprite.x, _c->sprite.y, _p); + + sound_play(_p->g->sounds[SOUND_DYNAMITE], 3); +} diff --git a/src/game/play/collectible/dynamite.h b/src/game/play/collectible/dynamite.h new file mode 100755 index 0000000..280e363 --- /dev/null +++ b/src/game/play/collectible/dynamite.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for dynamite.c. +*/ + +#include "../enemy/enemies.h" + +#define DYNAMITE_WIDTH 64 +#define DYNAMITE_HEIGHT 64 +#define DYNAMITE_DAMAGE 10 + +void dynamite_init(Collectible* _c, List* _l, Play* _p); +void dynamite_collect(Collectible* _c, List* _l, Play* _p); diff --git a/src/game/play/collectible/miracle_cure.c b/src/game/play/collectible/miracle_cure.c new file mode 100755 index 0000000..6f06e47 --- /dev/null +++ b/src/game/play/collectible/miracle_cure.c @@ -0,0 +1,44 @@ +/* + * DESCRIPTION: + * "Miracle Cure", heals you for one fatness. +*/ + +#include "miracle_cure.h" + +/* Initializes the miracle cure. */ +void +miracle_cure_init(Collectible* _c, List* _l, Play* _p) +{ + sprite_init + ( + &_c->sprite, + _p->g->textures[TEXTURE_COLLECTIBLE_MIRACLE_CURE], + ORIGIN_MIDDLE, + MIRACLE_CURE_WIDTH, + MIRACLE_CURE_HEIGHT + ); +} + +/* What happens when you collect the miracle cure? */ +void +miracle_cure_collect(Collectible* _c, List* _l, Play* _p) +{ + _p->player.fatness--; + + _p->player.isFatnessUpdate = TRUE; + + if (_p->player.fatness < 0) + { + _p->player.fatness = 0; + + player_cash_acquire(&_p->player, MIRACLE_CURE_CASH_VALUE, _p); + + sound_play(_p->g->sounds[SOUND_BULLIONS], -1); + } + else + { + message_miracle_cure_add(_c->sprite.x, _c->sprite.y, _p); + + sound_play(_p->g->sounds[SOUND_MIRACLE_CURE], -1); + } +} diff --git a/src/game/play/collectible/miracle_cure.h b/src/game/play/collectible/miracle_cure.h new file mode 100755 index 0000000..eac2a2b --- /dev/null +++ b/src/game/play/collectible/miracle_cure.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for miracle_cure.c. +*/ + +#include "../player/player_cash.h" + +#define MIRACLE_CURE_WIDTH 64 +#define MIRACLE_CURE_HEIGHT 64 +#define MIRACLE_CURE_CASH_VALUE 1000 + +void miracle_cure_init(Collectible* _c, List* _l, Play* _p); +void miracle_cure_collect(Collectible* _c, List* _l, Play* _p); diff --git a/src/game/play/collectible/sack.c b/src/game/play/collectible/sack.c new file mode 100755 index 0000000..bf40375 --- /dev/null +++ b/src/game/play/collectible/sack.c @@ -0,0 +1,29 @@ +/* + * DESCRIPTION: + * Medium money collectible. +*/ + +#include "sack.h" + +/* Initializes the sack. */ +void +sack_init(Collectible* _c, List* _l, Play* _p) +{ + sprite_init + ( + &_c->sprite, + _p->g->textures[TEXTURE_COLLECTIBLE_SACK], + ORIGIN_MIDDLE, + SACK_WIDTH, + SACK_HEIGHT + ); +} + +/* What happens when you collect the sack? */ +void +sack_collect(Collectible* _c, List* _l, Play* _p) +{ + player_cash_acquire(&_p->player, SACK_CASH_VALUE, _p); + + sound_play(_p->g->sounds[SOUND_SACK], -1); +} diff --git a/src/game/play/collectible/sack.h b/src/game/play/collectible/sack.h new file mode 100755 index 0000000..0d477e1 --- /dev/null +++ b/src/game/play/collectible/sack.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for sack.c. +*/ + +#include "../player/player_cash.h" + +#define SACK_WIDTH 64 +#define SACK_HEIGHT 64 +#define SACK_CASH_VALUE 250 + +void sack_init(Collectible* _c, List* _l, Play* _p); +void sack_collect(Collectible* _c, List* _l, Play* _p); diff --git a/src/game/play/enemy/chip.c b/src/game/play/enemy/chip.c new file mode 100755 index 0000000..cb75238 --- /dev/null +++ b/src/game/play/enemy/chip.c @@ -0,0 +1,116 @@ +/* + * DESCRIPTION: + * AI for the "Chip" enemy. +*/ + +#include "chip.h" + +static s32 chip_health_get(Enemy* _e, List* _l, Play* _p); +static f64 chip_velocity_get(Enemy* _e, List* _l, Play* _p); + +/* Gets the health of the Chip. */ +static s32 +chip_health_get(Enemy* _e, List* _l, Play* _p) +{ + s32 health; + + health = CHIP_HEALTH_BASE + ((_p->game - 1) * CHIP_HEALTH_GAME_MULTIPLIER); + + health = CEIL(health, CHIP_HEALTH_MAX); + + return health; +} + +/* Gets the velocity of the Chip. */ +static f64 +chip_velocity_get(Enemy* _e, List* _l, Play* _p) +{ + f64 velocity; + + velocity = CHIP_VELOCITY_BASE + ((_p->game - 1) * CHIP_VELOCITY_GAME_MULTIPLIER); + + velocity = CEIL(velocity, CHIP_VELOCITY_MAX); + + return velocity; +} + +/* Initializes Chip. */ +void +chip_init(Enemy* _e, List* _l, Play* _p) +{ + sprite_init + ( + &_e->sprite, + _p->g->textures[TEXTURE_ENEMY_CHIP], + ORIGIN_MIDDLE, + CHIP_WIDTH, + CHIP_HEIGHT + ); + + _e->shadow.r = CHIP_SHADOW_RADIUS; +} + +/* Sets the Chip. */ +void +chip_set(Enemy* _e, List* _l, f64 _x, f64 _y, Play* _p) +{ + f64 angle; + f64 velocity; + + _e->sprite.x = _x; + _e->sprite.y = _y; + + _e->shadow.x = _e->sprite.x + CHIP_SHADOW_OFFSET_X; + _e->shadow.y = _e->sprite.y + CHIP_SHADOW_OFFSET_Y; + + _e->sprite.rotation = random_f64_in_range_get(0, ROTATION_MAX_DEG); + + velocity = chip_velocity_get(_e, _l, _p); + + angle = math_atan + ( + _e->shadow.x, + _e->shadow.y, + _p->player.shadow.x, + _p->player.shadow.y + ); + + _e->dX = -cos(angle) * velocity; + _e->dY = -sin(angle) * velocity; + + _e->health = chip_health_get(_e, _l, _p); + _e->isActive = TRUE; + _e->lifetime = CHIP_LIFETIME; +} + +/* + * Updates Chip. + * Just flies towards the player, and will remove + * itself if enough time has passed. +*/ +void +chip_update(Enemy* _e, List* _l, Play* _p) +{ + SDL_Rect rect; + SDL_Rect windowRect; + + rect = sprite_rect_get(&_e->sprite); + windowRect = window_rect_get(_p->g->window); + + _e->lifetime--; + + _e->sprite.x += _e->dX; + _e->sprite.y += _e->dY; + + _e->sprite.rotation += CHIP_ROTATION_DELTA; + + _e->shadow.x = _e->sprite.x + CHIP_SHADOW_OFFSET_X; + _e->shadow.y = _e->sprite.y + CHIP_SHADOW_OFFSET_Y; + + if + ( + _e->lifetime <= 0 || + (_e->lifetime < CHIP_LIFETIME / 2 && !SDL_HasIntersection(&rect, &windowRect)) + ) + enemy_remove(_l, _e, _p); +} diff --git a/src/game/play/enemy/chip.h b/src/game/play/enemy/chip.h new file mode 100755 index 0000000..f57ddbd --- /dev/null +++ b/src/game/play/enemy/chip.h @@ -0,0 +1,27 @@ +/* + * DESCRIPTION: + * Header file for chip.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "enemy.h" + +#define CHIP_HEALTH_BASE 1 +#define CHIP_HEALTH_GAME_MULTIPLIER 0 +#define CHIP_HEALTH_MAX 99 +#define CHIP_VELOCITY_GAME_MULTIPLIER 1 +#define CHIP_VELOCITY_MAX 10 +#define CHIP_WIDTH 16 +#define CHIP_HEIGHT 16 +#define CHIP_VELOCITY_BASE 3 +#define CHIP_LIFETIME 300 +#define CHIP_SHADOW_OFFSET_X 0 +#define CHIP_SHADOW_OFFSET_Y 32 +#define CHIP_SHADOW_RADIUS 8 +#define CHIP_ROTATION_DELTA 5 + +void chip_init(Enemy* _e, List* _l, Play* _p); +void chip_set(Enemy* _e, List* _l, f64 _x, f64 _y, Play* _p); +void chip_update(Enemy* _e, List* _l, Play* _p); diff --git a/src/game/play/enemy/chipper.c b/src/game/play/enemy/chipper.c new file mode 100755 index 0000000..88a208d --- /dev/null +++ b/src/game/play/enemy/chipper.c @@ -0,0 +1,209 @@ +/* + * DESCRIPTION: + * AI for the "Chipper" enemy. +*/ + +#include "chipper.h" + +static s32 chipper_health_get(Enemy* _e, List* _l, Play* _p); +static s32 chipper_attack_timer_max_get(Enemy* _e, List* _l, Play* _p); + +/* Gets the Chipper health. */ +static s32 +chipper_health_get(Enemy* _e, List* _l, Play* _p) +{ + s32 health; + + health = (s32)(CHIPPER_HEALTH_BASE + ((_p->game - 1) * CHIPPER_HEALTH_GAME_MULTIPLIER)); + + health = CEIL(health, CHIPPER_HEALTH_MAX); + + return health; +} + +/* Gets the Chipper attack timer max. */ +static s32 +chipper_attack_timer_max_get(Enemy* _e, List* _l, Play* _p) +{ + s32 attackTimerMax; + + attackTimerMax = (s32)(CHIPPER_ATTACK_TIMER_MAX_BASE - ((_p->game - 1) * CHIPPER_ATTACK_TIMER_MAX_GAME_MULTIPLIER)); + + attackTimerMax = FLOOR(attackTimerMax, CHIPPER_ATTACK_TIMER_MAX_MIN); + + return attackTimerMax; +} + +/* Initializes Chipper. */ +void +chipper_init(Enemy* _e, List* _l, Play* _p) +{ + Atlas atlas; + + atlas_init + ( + &atlas, + _p->g->textures[TEXTURE_ENEMY_CHIPPER], + CHIPPER_ROWS, + CHIPPER_COLUMNS, + CHIPPER_WIDTH, + CHIPPER_HEIGHT + ); + + sprite_atlas_init + ( + &_e->sprite, + &atlas, + ORIGIN_MIDDLE, + CHIPPER_WIDTH, + CHIPPER_HEIGHT + ); + + _e->shadow.r = CHIPPER_SHADOW_RADIUS; + + _e->sprite.frame = 2; +} + +/* Sets the Chipper. */ +void +chipper_set(Enemy* _e, List* _l, f64 _x, f64 _y, Play* _p) +{ + _e->sprite.x = _x; + _e->sprite.y = _y; + + _e->shadow.x = _e->sprite.x + CHIPPER_SHADOW_OFFSET_X; + _e->shadow.y = _e->sprite.y + CHIPPER_SHADOW_OFFSET_Y; + + _e->isActive = TRUE; + _e->isActing = FALSE; + _e->range = random_f64_in_range_get(CHIPPER_RANGE_MIN, CHIPPER_RANGE_MAX); + _e->attackTimer = chipper_attack_timer_max_get(_e, _l, _p); + _e->health = chipper_health_get(_e, _l, _p); +} + +/* + * Updates Chipper. + * Basically, it's going to slowly approach the player, + * get into range of them, + * and fire projectiles (chips) at the player. +*/ +void +chipper_update(Enemy* _e, List* _l, Play* _p) +{ + f64 distance; + f64 angle; + SDL_Rect rect; + SDL_Rect windowRect; + + rect = sprite_rect_get(&_e->sprite); + windowRect = window_rect_get(_p->g->window); + + distance = math_distance + ( + _e->shadow.x, + _e->shadow.y, + _p->player.shadow.x, + _p->player.shadow.y + ); + + angle = math_atan + ( + _e->shadow.x, + _e->shadow.y, + _p->player.shadow.x, + _p->player.shadow.y + ); + + if (_p->player.sprite.x < _e->sprite.x) + _e->sprite.flip = FLIP_HORIZONTAL; + else + _e->sprite.flip = FLIP_NONE; + + /* Is this Chipper within range, and is fully on screen? */ + if + ( + distance <= _e->range && + SDL_HasIntersection(&rect, &windowRect) + ) + { + if (!_e->isActing) + _e->animationTimer = 0; + _e->isActing = TRUE; + } + else + { + if (_e->isActing) + _e->animationTimer = 0; + _e->isActing = FALSE; + } + + _e->animationTimer--; + + if (_e->isActing) /* Waiting to shoot */ + { + _e->dX = 0; + _e->dY = 0; + + _e->sprite.frame = 1; + + _e->attackTimer--; + + if (_e->animationTimer > 0) + { + f64 progress; + + _e->sprite.frame = 3; + + progress = 1 - ((f64)_e->animationTimer / CHIPPER_ANIMATION_TIMER_MAX); + + _e->sprite.squash = SPRITE_SQUASH_DEFAULT + (cos(ROTATION_MAX_RAD * progress) * CHIPPER_SQUASH_MULTIPLIER); + _e->sprite.stretch = SPRITE_STRETCH_DEFAULT + (cos(ROTATION_MAX_RAD * progress) * CHIPPER_STRETCH_MULTIPLIER); + + if (_e->animationTimer <= 0) + { + _e->sprite.squash = SPRITE_SQUASH_DEFAULT; + _e->sprite.stretch = SPRITE_STRETCH_DEFAULT; + } + } + + if (_e->attackTimer <= 0) /* Just started shooting */ + { + Enemy* e; + + e = enemy_add(_l, ENEMY_CHIP, _p); + enemy_set(_l, e, _e->shadow.x, _e->shadow.y, _p); + + _e->attackTimer = chipper_attack_timer_max_get(_e, _l, _p); + + _e->animationTimer = CHIPPER_ANIMATION_TIMER_MAX; + + _e->sprite.frame = 3; + + _p->enemyMax++; + + sound_play(_p->g->sounds[SOUND_SPIT], -1); + } + } + else /* Walk */ + { + _e->dX = -cos(angle) * CHIPPER_VELOCITY; + _e->dY = -sin(angle) * CHIPPER_VELOCITY; + + if (_e->animationTimer % CHIPPER_ANIMATION_MODULO == 0) + { + _e->sprite.frame++; + + if (_e->sprite.frame > 2) + _e->sprite.frame = 0; + } + + if (_e->animationTimer <= 0) + _e->animationTimer = CHIPPER_ANIMATION_TIMER_MAX; + } + + _e->sprite.x += _e->dX; + _e->sprite.y += _e->dY; + + _e->shadow.x = _e->sprite.x + CHIPPER_SHADOW_OFFSET_X; + _e->shadow.y = _e->sprite.y + CHIPPER_SHADOW_OFFSET_Y; +} diff --git a/src/game/play/enemy/chipper.h b/src/game/play/enemy/chipper.h new file mode 100755 index 0000000..afaae8a --- /dev/null +++ b/src/game/play/enemy/chipper.h @@ -0,0 +1,34 @@ +/* + * DESCRIPTION: + * Header file for chipper.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "enemy.h" + +#define CHIPPER_ANIMATION_MODULO 5 +#define CHIPPER_ANIMATION_TIMER_MAX 15 +#define CHIPPER_STRETCH_MULTIPLIER 0.05 +#define CHIPPER_SQUASH_MULTIPLIER 0.05 +#define CHIPPER_ATTACK_TIMER_MAX_BASE 120 +#define CHIPPER_ATTACK_TIMER_MAX_GAME_MULTIPLIER 1 +#define CHIPPER_ATTACK_TIMER_MAX_MIN 30 +#define CHIPPER_SHADOW_OFFSET_X 0 +#define CHIPPER_SHADOW_OFFSET_Y 16 +#define CHIPPER_SHADOW_RADIUS 24 +#define CHIPPER_HEALTH_BASE 5 +#define CHIPPER_HEALTH_GAME_MULTIPLIER 2 +#define CHIPPER_HEALTH_MAX 99 +#define CHIPPER_HEIGHT 48 +#define CHIPPER_ROWS 1 +#define CHIPPER_COLUMNS 4 +#define CHIPPER_RANGE_MAX 300 +#define CHIPPER_RANGE_MIN 200 +#define CHIPPER_VELOCITY 1 +#define CHIPPER_WIDTH 64 + +void chipper_init(Enemy* _e, List* _l, Play* _p); +void chipper_set(Enemy* _e, List* _l, f64 _x, f64 _y, Play* _p); +void chipper_update(Enemy* _e, List* _l, Play* _p); diff --git a/src/game/play/enemy/crumbler.c b/src/game/play/enemy/crumbler.c new file mode 100755 index 0000000..82e8f5a --- /dev/null +++ b/src/game/play/enemy/crumbler.c @@ -0,0 +1,125 @@ +/* + * DESCRIPTION: + * AI for the "Crumbler" enemy. +*/ + +#include "crumbler.h" + +static s32 crumbler_health_get(Enemy* _e, List* _l, Play* _p); +static s32 crumbler_attack_timer_max_get(Enemy* _e, List* _l, Play* _p); + +/* Gets the Crumbler health. */ +static s32 +crumbler_health_get(Enemy* _e, List* _l, Play* _p) +{ + s32 health; + + health = CRUMBLER_HEALTH_BASE + ((_p->game - 1) * CRUMBLER_HEALTH_GAME_MULTIPLIER); + + health = CEIL(health, CRUMBLER_HEALTH_MAX); + + return health; +} + +/* Gets the Crumbler attack timer max. */ +static s32 +crumbler_attack_timer_max_get(Enemy* _e, List* _l, Play* _p) +{ + s32 attackTimerMax; + + attackTimerMax = CRUMBLER_ATTACK_TIMER_MAX_BASE - ((_p->game - 1) * CRUMBLER_ATTACK_TIMER_MAX_GAME_MULTIPLIER); + + attackTimerMax = FLOOR(attackTimerMax, CRUMBLER_ATTACK_TIMER_MAX_MIN); + + return attackTimerMax; +} + +/* Initializes the Crumbler. */ +void +crumbler_init(Enemy* _e, List* _l, Play* _p) +{ + Atlas atlas; + + atlas_init + ( + &atlas, + _p->g->textures[TEXTURE_ENEMY_CRUMBLER], + CRUMBLER_ROWS, + CRUMBLER_COLUMNS, + CRUMBLER_WIDTH, + CRUMBLER_HEIGHT + ); + + sprite_atlas_init + ( + &_e->sprite, + &atlas, + ORIGIN_MIDDLE, + CRUMBLER_WIDTH, + CRUMBLER_HEIGHT + ); + + _e->shadow.r = 0; + _e->sprite.frame = 1; +} + +/* Sets the Crumbler. */ +void +crumbler_set(Enemy* _e, List* _l, f64 _x, f64 _y, Play* _p) +{ + _e->shadow.x = _x + CRUMBLER_SHADOW_OFFSET_X; + _e->shadow.y = _y + CRUMBLER_SHADOW_OFFSET_Y; + + _e->sprite.x = _e->shadow.x; + _e->sprite.y = _e->shadow.y - (CRUMBLER_ATTACK_OFFSET_Y + CRUMBLER_SHADOW_OFFSET_Y); + + _e->isActive = FALSE; + _e->isActing = TRUE; + _e->attackTimer = crumbler_attack_timer_max_get(_e, _l, _p); + _e->health = crumbler_health_get(_e, _l, _p); +} + +/* + * Updates Crumbler. + * Falls into the arena and then remains stationary. +*/ +void +crumbler_update(Enemy* _e, List* _l, Play* _p) +{ + if (_e->isActing) + { + f64 progress; + + _e->attackTimer--; + + progress = 1 - ((f64)_e->attackTimer / crumbler_attack_timer_max_get(_e, _l, _p)); + + _e->shadow.r = CRUMBLER_SHADOW_RADIUS * progress; + + _e->sprite.y = _e->shadow.y - CRUMBLER_SHADOW_OFFSET_Y - (CRUMBLER_ATTACK_OFFSET_Y * (1 - progress)); + + if (_e->attackTimer <= 0) + { + _e->isActing = FALSE; + _e->isActive = TRUE; + _e->shadow.r = CRUMBLER_SHADOW_RADIUS; + _e->sprite.frame = 0; + + sound_play(_p->g->sounds[SOUND_RUMBLE], -1); + } + } + else + { + f64 progress; + + _e->animationTimer--; + + progress = 1 - ((f64)_e->animationTimer / CRUMBLER_ANIMATION_TIMER_MAX); + + _e->sprite.squash = (SPRITE_SQUASH_DEFAULT + cos((ROTATION_MAX_RAD * progress)) * CRUMBLER_SQUASH_MULTIPLIER); + _e->sprite.stretch = (SPRITE_STRETCH_DEFAULT + cos((ROTATION_MAX_RAD * progress)) * CRUMBLER_STRETCH_MULTIPLIER); + + if (_e->animationTimer <= 0) + _e->animationTimer = CRUMBLER_ANIMATION_TIMER_MAX; + } +} diff --git a/src/game/play/enemy/crumbler.h b/src/game/play/enemy/crumbler.h new file mode 100755 index 0000000..b1df469 --- /dev/null +++ b/src/game/play/enemy/crumbler.h @@ -0,0 +1,31 @@ +/* + * DESCRIPTION: + * Header file for crumbler.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "enemy.h" + +#define CRUMBLER_ANIMATION_TIMER_MAX 60 +#define CRUMBLER_ATTACK_TIMER_MAX_BASE 120 +#define CRUMBLER_ATTACK_TIMER_MAX_MIN 60 +#define CRUMBLER_SHADOW_OFFSET_X 0 +#define CRUMBLER_SHADOW_OFFSET_Y 16 +#define CRUMBLER_SHADOW_RADIUS 48 +#define CRUMBLER_HEALTH_BASE 10 +#define CRUMBLER_HEALTH_MAX 99 +#define CRUMBLER_HEIGHT 96 +#define CRUMBLER_ATTACK_OFFSET_Y 1000 +#define CRUMBLER_SQUASH_MULTIPLIER 0.015 +#define CRUMBLER_STRETCH_MULTIPLIER 0.025 +#define CRUMBLER_WIDTH 128 +#define CRUMBLER_HEALTH_GAME_MULTIPLIER 2 +#define CRUMBLER_ATTACK_TIMER_MAX_GAME_MULTIPLIER 5 +#define CRUMBLER_ROWS 1 +#define CRUMBLER_COLUMNS 2 + +void crumbler_init(Enemy* _e, List* _l, Play* _p); +void crumbler_set(Enemy* _e, List* _l, f64 _x, f64 _y, Play* _p); +void crumbler_update(Enemy* _e, List* _l, Play* _p); diff --git a/src/game/play/enemy/cupper.c b/src/game/play/enemy/cupper.c new file mode 100755 index 0000000..5dd1a53 --- /dev/null +++ b/src/game/play/enemy/cupper.c @@ -0,0 +1,151 @@ +/* + * DESCRIPTION: + * AI for the "Cupper" enemy. +*/ + +#include "cupper.h" + +static s32 cupper_health_get(Enemy* _e, List* _l, Play* _p); +static s32 cupper_attack_timer_max_get(Enemy* _e, List* _l, Play* _p); + +/* Returns health of a Cupper. */ +static s32 +cupper_health_get(Enemy* _e, List* _l, Play* _p) +{ + s32 health; + + health = CUPPER_HEALTH_BASE + ((_p->game - 1) * CUPPER_HEALTH_GAME_MULTIPLIER); + + health = CEIL(health, CUPPER_HEALTH_MAX); + + return health; +} + +/* Returns the attack timer max (period in which an attack is performed) of a Cupper. */ +static s32 +cupper_attack_timer_max_get(Enemy* _e, List* _l, Play* _p) +{ + s32 attackTimerMax; + + attackTimerMax = CUPPER_ATTACK_TIMER_MAX_BASE - (_p->game * CUPPER_ATTACK_TIMER_MAX_GAME_MULTIPLIER); + + attackTimerMax = FLOOR(attackTimerMax, CUPPER_ATTACK_TIMER_MAX_MIN); + + return attackTimerMax; +} + +/* Initializes the Cupper. */ +void +cupper_init(Enemy* _e, List* _l, Play* _p) +{ + Atlas atlas; + + atlas_init + ( + &atlas, + _p->g->textures[TEXTURE_ENEMY_CUPPER], + CUPPER_ROWS, + CUPPER_COLUMNS, + CUPPER_WIDTH, + CUPPER_HEIGHT + ); + + sprite_atlas_init + ( + &_e->sprite, + &atlas, + ORIGIN_MIDDLE, + CUPPER_WIDTH, + CUPPER_HEIGHT + ); + + _e->shadow.r = CUPPER_SHADOW_RADIUS; +} + +/* Sets the Cupper. */ +void +cupper_set(Enemy* _e, List* _l, f64 _x, f64 _y, Play* _p) +{ + _e->sprite.x = _x; + _e->sprite.y = _y; + + _e->shadow.x = _e->sprite.x + CUPPER_SHADOW_OFFSET_X; + _e->shadow.y = _e->sprite.y + CUPPER_SHADOW_OFFSET_Y; + + _e->isActive = TRUE; + + _e->health = cupper_health_get(_e, _l, _p); + _e->attackTimer = cupper_attack_timer_max_get(_e, _l, _p); +} + +/* + * Updates Cupper. + * Slowly hops towards the player. +*/ +void +cupper_update(Enemy* _e, List* _l, Play* _p) +{ + f64 angle; + f64 progress; + + _e->attackTimer--; + _e->animationTimer--; + + progress = 1 - ((f64)_e->animationTimer / CUPPER_ANIMATION_TIMER_MAX); + + if (_e->animationTimer <= 0) + _e->animationTimer = CUPPER_ANIMATION_TIMER_MAX; + + if (_e->isActing) + { + if (_e->attackTimer <= 0) + { + _e->dX = 0; + _e->dY = 0; + _e->isActing = FALSE; + _e->attackTimer = cupper_attack_timer_max_get(_e, _l, _p); + } + } + else + { + if (_e->attackTimer <= 0) + { + f64 angle; + + angle = math_atan + ( + _e->shadow.x, + _e->shadow.y, + _p->player.shadow.x, + _p->player.shadow.y + ); + + _e->dX = -cos(angle) * CUPPER_VELOCITY; + _e->dY = -sin(angle) * CUPPER_VELOCITY; + + _e->isActing = TRUE; + + _e->attackTimer = CUPPER_JUMP_TIMER_MAX_BASE; + + if (_p->player.sprite.x < _e->sprite.x) + _e->sprite.flip = FLIP_HORIZONTAL; + else + _e->sprite.flip = FLIP_NONE; + + _e->sprite.frame = 0; + } + else if (_e->attackTimer <= CUPPER_ATTACK_TIMER_THRESHOLD) + _e->sprite.frame = 1; + else + { + _e->sprite.squash = (SPRITE_SQUASH_DEFAULT + cos((ROTATION_MAX_RAD * progress)) * CUPPER_SQUASH_MULTIPLIER); + _e->sprite.stretch = (SPRITE_STRETCH_DEFAULT + cos((ROTATION_MAX_RAD * progress)) * CUPPER_STRETCH_MULTIPLIER); + } + } + + _e->sprite.x += _e->dX; + _e->sprite.y += _e->dY; + + _e->shadow.x = _e->sprite.x + CUPPER_SHADOW_OFFSET_X; + _e->shadow.y = _e->sprite.y + CUPPER_SHADOW_OFFSET_Y; +} diff --git a/src/game/play/enemy/cupper.h b/src/game/play/enemy/cupper.h new file mode 100755 index 0000000..6882f05 --- /dev/null +++ b/src/game/play/enemy/cupper.h @@ -0,0 +1,33 @@ +/* + * DESCRIPTION: + * Header file for cupper.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "enemy.h" + +#define CUPPER_ANIMATION_TIMER_MAX 30 +#define CUPPER_ATTACK_TIMER_MAX_BASE 120 +#define CUPPER_ATTACK_TIMER_MAX_GAME_MULTIPLIER 10 +#define CUPPER_ATTACK_TIMER_MAX_MIN 30 +#define CUPPER_ATTACK_TIMER_THRESHOLD 30 +#define CUPPER_SHADOW_OFFSET_X 0 +#define CUPPER_SHADOW_OFFSET_Y 16 +#define CUPPER_SHADOW_RADIUS 24 +#define CUPPER_COLUMNS 2 +#define CUPPER_HEALTH_BASE 3 +#define CUPPER_HEALTH_GAME_MULTIPLIER 2 +#define CUPPER_HEALTH_MAX 99 +#define CUPPER_HEIGHT 48 +#define CUPPER_JUMP_TIMER_MAX_BASE 30 +#define CUPPER_ROWS 1 +#define CUPPER_SQUASH_MULTIPLIER 0.05 +#define CUPPER_STRETCH_MULTIPLIER 0.05 +#define CUPPER_VELOCITY 3 +#define CUPPER_WIDTH 48 + +void cupper_init(Enemy* _e, List* _l, Play* _p); +void cupper_set(Enemy* _e, List* _l, f64 _x, f64 _y, Play* _p); +void cupper_update(Enemy* _e, List* _l, Play* _p); diff --git a/src/game/play/enemy/enemies.c b/src/game/play/enemy/enemies.c new file mode 100755 index 0000000..fddeae7 --- /dev/null +++ b/src/game/play/enemy/enemies.c @@ -0,0 +1,95 @@ +/* + * DESCRIPTION: + * Game enemies. +*/ + +#include "enemy.h" + +/* Initializes enemy list. */ +void +enemies_init(List* _l, Play* _p) +{ + list_init(_l); +} + +/* Frees enemy list. */ +void +enemies_free(List* _l, Play* _p) +{ + list_free(_l); +} + +/* Brutally murders all enemies. */ +void +enemies_defeat(List* _l, Play* _p) +{ + Enemy** enemies; + s32 count; + + enemies = (Enemy**)list_data_get(_l); + count = _l->count; + + for (s32 i = 0; i < count; i++) + { + Enemy* e; + + e = enemies[i]; + + enemy_defeat(e, _l, _p); + } + + free(enemies); +} + +/* Updates enemies. */ +void +enemies_update(List* _l, Play* _p) +{ + Enemy** enemies; + s32 count; + + enemies = (Enemy**)list_data_get(_l); + count = _l->count; + + for (s32 i = 0; i < count; i++) + { + Enemy* e; + + e = enemies[i]; + + enemy_update(e, _l, _p); + } + + free(enemies); +} + +/* Draws enemies. */ +void +enemies_draw(List* _l, Play* _p) +{ + Enemy** enemies; + s32 count; + + enemies = (Enemy**)list_data_get(_l); + count = _l->count; + + for (s32 i = 0; i < count; i++) + { + Enemy* e; + + e = enemies[i]; + + shadow_draw(&e->shadow, _p); + } + + for (s32 i = 0; i < count; i++) + { + Enemy* e; + + e = enemies[i]; + + sprite_draw(&e->sprite, _p->g->renderer); + } + + free(enemies); +} diff --git a/src/game/play/enemy/enemies.h b/src/game/play/enemy/enemies.h new file mode 100755 index 0000000..b477937 --- /dev/null +++ b/src/game/play/enemy/enemies.h @@ -0,0 +1,15 @@ +/* + * DESCRIPTION: + * Header file for enemy.h +*/ + +#pragma once + +#include "enemy.h" +#include "../shadow/shadow.h" + +void enemies_init(List* _l, Play* _p); +void enemies_free(List* _l, Play* _p); +void enemies_update(List* _l, Play* _p); +void enemies_defeat(List* _l, Play* _p); +void enemies_draw(List* _l, Play* _p); diff --git a/src/game/play/enemy/enemy.c b/src/game/play/enemy/enemy.c new file mode 100755 index 0000000..17388ad --- /dev/null +++ b/src/game/play/enemy/enemy.c @@ -0,0 +1,245 @@ +/* + * DESCRIPTION: + * Game enemies. +*/ + +#include "enemy.h" + +static CollectibleType enemy_collectible_type_get(Enemy* _e, List* _l, Play* _p); +static f64 enemy_collectible_drop_chance_get(Enemy* _e, List* _l, Play* _p); +static void enemy_collectible_drop(Enemy* _e, List* _l, Play* _p); + +/* The enemy has dropped a collectible; what type is it? */ +static CollectibleType +enemy_collectible_type_get(Enemy* _e, List* _l, Play* _p) +{ + f64 random; + CollectibleType type; + + random = random_f64_get(); + + if (random - ENEMY_COLLECTIBLE_POWERUP_DROP_CHANCE < 0) + type = (CollectibleType)random_s32_in_range_get(COLLECTIBLE_MIRACLE_CURE, COLLECTIBLE_BERSERK); + else if (random - ENEMY_COLLECTIBLE_BULLIONS_DROP_CHANCE < 0) + type = COLLECTIBLE_BULLIONS; + else if (random - ENEMY_COLLECTIBLE_SACK_DROP_CHANCE < 0) + type = COLLECTIBLE_SACK; + else + type = COLLECTIBLE_DOLLARS; + + return type; +} + +/* % of enemy dropping a collectible */ +static f64 +enemy_collectible_drop_chance_get(Enemy* _e, List* _l, Play* _p) +{ + f64 chance; + + chance = ENEMY_COLLECTIBLE_DROP_CHANCE_BASE + ((_p->game - 1) * ENEMY_COLLECTIBLE_DROP_CHANCE_GAME_MULTIPLIER); + + switch (_e->type) + { + case ENEMY_CHIPPER: + chance += ENEMY_CHIPPER_DROP_CHANCE_BONUS; + break; + case ENEMY_CRUMBLER: + chance += ENEMY_CRUMBLER_DROP_CHANCE_BONUS; + break; + case ENEMY_NONE: + case ENEMY_CUPPER: + default: + break; + } + + chance = CEIL(chance, ENEMY_COLLECTIBLE_DROP_CHANCE_MAX); + + return chance; +} + +/* Will the enemy drop a collectible? */ +static void +enemy_collectible_drop(Enemy* _e, List* _l, Play* _p) +{ + f64 chanceMax; + f64 chance; + + chanceMax = enemy_collectible_drop_chance_get(_e, _l, _p); + + chance = random_f64_get(); + + if (chance <= chanceMax) + { + CollectibleType type; + + type = enemy_collectible_type_get(_e, _l, _p); + + collectible_add(&_p->collectibles, type, _e->shadow.x, _e->shadow.y, _p); + } +} + +/* Adds an enemy to the game. */ +Enemy* +enemy_add(List* _l, EnemyType _t, Play* _p) +{ + Enemy enemy; + + memset(&enemy, '\0', sizeof(Enemy)); + + enemy.type = _t; + + list_init(&enemy.snakesHit); + list_init(&enemy.returnSnakesHit); + + switch (enemy.type) + { + case ENEMY_CUPPER: + cupper_init(&enemy, _l, _p); + break; + case ENEMY_CHIPPER: + chipper_init(&enemy, _l, _p); + break; + case ENEMY_CHIP: + chip_init(&enemy, _l, _p); + break; + case ENEMY_CRUMBLER: + crumbler_init(&enemy, _l, _p); + break; + case ENEMY_NONE: + default: + break; + } + + _p->isBountyUpdate = TRUE; + + return (Enemy*)list_append_new(_l, (void*)&enemy, sizeof(Enemy)); +} + +/* Removes an enemy from the game. */ +void +enemy_remove(List* _l, Enemy* _e, Play* _p) +{ + list_free(&_e->snakesHit); + list_free(&_e->returnSnakesHit); + + _p->isBountyUpdate = TRUE; + _p->enemyMax--; + + list_node_from_data_remove(_l, _e, sizeof(Enemy)); +} + +/* Positions the enemy and prepares it for battle. */ +void +enemy_set(List* _l, Enemy* _e, f64 _x, f64 _y, Play* _p) +{ + switch (_e->type) + { + case ENEMY_CUPPER: + cupper_set(_e, _l, _x, _y, _p); + break; + case ENEMY_CHIPPER: + chipper_set(_e, _l, _x, _y, _p); + break; + case ENEMY_CHIP: + chip_set(_e, _l, _x, _y, _p); + break; + case ENEMY_CRUMBLER: + crumbler_set(_e, _l, _x, _y, _p); + break; + case ENEMY_NONE: + default: + break; + } +} + +/* The enemy takes the specified amount of damage. */ +void +enemy_damage(Enemy* _e, s32 _d, List* _l, Play* _p) +{ + _e->health -= _d; + _e->hitTimerMax += _d * ENEMY_HIT_TIMER_MAX_MULTIPLIER; + _e->hitTimer += _d * ENEMY_HIT_TIMER_MAX_MULTIPLIER; + + message_damage_add(_e->sprite.x, _e->sprite.y, _d, _p); + sound_play(_p->g->sounds[SOUND_ENEMY_HURT], 0); + + if (_e->health <= 0) + { + _e->health = 0; + enemy_defeat(_e, _l, _p); + } +} + +/* The enemy is defeated.. */ +void +enemy_defeat(Enemy* _e, List* _l, Play* _p) +{ + if (_e->type != ENEMY_CHIP) + enemy_collectible_drop(_e, _l, _p); + + switch (_e->type) + { + case ENEMY_CUPPER: + sound_play(_p->g->sounds[SOUND_CUPPER_KILL], -1); + break; + case ENEMY_CHIPPER: + sound_play(_p->g->sounds[SOUND_CHIPPER_KILL], -1); + break; + case ENEMY_CRUMBLER: + sound_play(_p->g->sounds[SOUND_CRUMBLER_KILL], -1); + break; + case ENEMY_CHIP: + sound_play(_p->g->sounds[SOUND_CHIP_KILL], -1); + break; + default: + break; + } + + _p->player.waveEnemiesKilled++; + _p->player.enemiesKilled++; + + enemy_remove(_l, _e, _p); +} + +/* Updates enemy. */ +void +enemy_update(Enemy* _e, List* _l, Play* _p) +{ + switch (_e->type) + { + case ENEMY_CUPPER: + cupper_update(_e, _l, _p); + break; + case ENEMY_CHIPPER: + chipper_update(_e, _l, _p); + break; + case ENEMY_CHIP: + chip_update(_e, _l, _p); + break; + case ENEMY_CRUMBLER: + crumbler_update(_e, _l, _p); + break; + default: + break; + } + + if (_e->hitTimer > 0) + { + f64 progress; + + progress = 1 - ((f64)_e->hitTimer / _e->hitTimerMax); + + _e->sprite.color.r = SDL_ALPHA_OPAQUE * progress; + _e->sprite.color.g = 0; + _e->sprite.color.b = 0; + + _e->hitTimer--; + + if (_e->hitTimer <= 0) + { + _e->hitTimer = 0; + _e->hitTimerMax = 0; + _e->sprite.color = SPRITE_COLOR_DEFAULT; + } + } +} diff --git a/src/game/play/enemy/enemy.h b/src/game/play/enemy/enemy.h new file mode 100755 index 0000000..2034a47 --- /dev/null +++ b/src/game/play/enemy/enemy.h @@ -0,0 +1,32 @@ +/* + * DESCRIPTION: + * Header file for enemy.h +*/ + +#pragma once + +#include "../collectible/collectible.h" +#include "chip.h" +#include "chipper.h" +#include "crumbler.h" +#include "cupper.h" +#include "../message/message.h" + +#define ENEMY_COLLECTIBLE_DROP_CHANCE_BASE 0.33 +#define ENEMY_CHIPPER_DROP_CHANCE_BONUS 0.25 +#define ENEMY_CRUMBLER_DROP_CHANCE_BONUS 0.50 +#define ENEMY_COLLECTIBLE_DROP_CHANCE_GAME_MULTIPLIER 0.1 +#define ENEMY_COLLECTIBLE_DROP_CHANCE_MAX 1.0 +#define ENEMY_HIT_TIMER_MAX_MULTIPLIER 5 +#define ENEMY_COLLECTIBLE_BULLIONS_DROP_CHANCE 0.10 +#define ENEMY_COLLECTIBLE_SACK_DROP_CHANCE 0.25 +#define ENEMY_COLLECTIBLE_POWERUP_DROP_CHANCE 0.05 + +static const SDL_Color ENEMY_SHADOW_COLOR = {0, 0, 0, 64}; + +Enemy* enemy_add(List* _l, EnemyType _t, Play* _p); +void enemy_set(List* _l, Enemy* _e, f64 _x, f64 _y, Play* _p); +void enemy_update(Enemy* _e, List* _l, Play* _p); +void enemy_damage(Enemy* _e, s32 _d, List* _l, Play* _p); +void enemy_defeat(Enemy* _e, List* _l, Play* _p); +void enemy_remove(List* _l, Enemy* _e, Play* _p); diff --git a/src/game/play/gui/gui.c b/src/game/play/gui/gui.c new file mode 100755 index 0000000..eb023fa --- /dev/null +++ b/src/game/play/gui/gui.c @@ -0,0 +1,62 @@ +/* + * DESCRIPTION: + * Gameplay GUI. +*/ + +#include "gui.h" + +/* Initializes GUI. */ +void +gui_init(GUI* _g, Play* _p) +{ + gui_cash_init(_g, _p); + gui_wave_init(_g, _p); + gui_bounty_init(_g, _p); + gui_upgrade_init(_g, _p); + gui_ready_init(_g, _p); + gui_snake_init(_g, _p); + gui_redirect_init(_g, _p); + cursor_init(&_g->cursor, &_p->g->mouse, _p->g->textures[TEXTURE_GUI_CURSOR]); +} + +/* Frees GUI. */ +void +gui_free(GUI* _g, Play* _p) +{ + gui_cash_free(_g, _p); + gui_snake_free(_g, _p); + gui_bounty_free(_g, _p); + gui_redirect_free(_g, _p); + gui_ready_free(_g, _p); + gui_wave_free(_g, _p); + gui_upgrade_free(_g, _p); + cursor_free(&_g->cursor); +} + +/* Updates GUI. */ +void +gui_update(GUI* _g, Play* _p) +{ + gui_cash_update(_g, _p); + gui_wave_update(_g, _p); + gui_bounty_update(_g, _p); + gui_upgrade_update(_g, _p); + gui_ready_update(_g, _p); + gui_snake_update(_g, _p); + gui_redirect_update(_g, _p); + cursor_update(&_g->cursor, &_p->g->mouse); +} + +/* Draws GUI. */ +void +gui_draw(GUI* _g, Play* _p) +{ + gui_cash_draw(_g, _p); + gui_wave_draw(_g, _p); + gui_bounty_draw(_g, _p); + gui_upgrade_draw(_g, _p); + gui_ready_draw(_g, _p); + gui_snake_draw(_g, _p); + gui_redirect_draw(_g, _p); + cursor_draw(&_g->cursor, _p->g->renderer); +} diff --git a/src/game/play/gui/gui.h b/src/game/play/gui/gui.h new file mode 100755 index 0000000..8f17ebc --- /dev/null +++ b/src/game/play/gui/gui.h @@ -0,0 +1,21 @@ +/* + * DESCRIPTION: + * Header file for gui.c. +*/ + +#pragma once + +#include "../../gui/cursor.h" + +#include "gui_bounty.h" +#include "gui_cash.h" +#include "gui_snake.h" +#include "gui_redirect.h" +#include "gui_ready.h" +#include "gui_wave.h" +#include "gui_upgrade.h" + +void gui_init(GUI* _g, Play* _p); +void gui_free(GUI* _g, Play* _p); +void gui_draw(GUI* _g, Play* _p); +void gui_update(GUI* _g, Play* _p); diff --git a/src/game/play/gui/gui_bounty.c b/src/game/play/gui/gui_bounty.c new file mode 100755 index 0000000..8391759 --- /dev/null +++ b/src/game/play/gui/gui_bounty.c @@ -0,0 +1,116 @@ +/* + * DESCRIPTION: + * Displays a bounty icon and the current bountys available. +*/ + +#include "gui_bounty.h" + +/* Sets the gui bounty text. */ +static void +gui_bounty_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_BOUNTY_TEXT_MAX); + + if (_g->bountyText.atlas.texture) + texture_free(_g->bountyText.atlas.texture); + + if (_p->player.isBeingDefeated) + { + snprintf + ( + s, + GUI_BOUNTY_TEXT_MAX, + "%s", + STRING_PLAY_GUI_BOUNTY_DEAD + ); + } + else if (_p->isBetweenWaves) + { + snprintf + ( + s, + GUI_BOUNTY_TEXT_MAX, + "%s", + STRING_PLAY_GUI_BOUNTY_NONE + ); + } + else + { + snprintf + ( + s, + GUI_BOUNTY_TEXT_MAX, + "%i", + _p->enemyMax + ); + } + + text_sprite_init + ( + &_g->bountyText, + _p->g->renderer, + _p->g->fonts[FONT_BIG], + GUI_BOUNTY_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + _g->bountyText.x = _g->bountyBackgroundSprite.x; + _g->bountyText.y = _g->bountyBackgroundSprite.y; + + _g->bountyText.rotation = GUI_BOUNTY_TEXT_ROTATION; + _g->bountyText.color.a = GUI_BOUNTY_ALPHA; + + free(s); +} + +/* Initializes bounty GUI. */ +void +gui_bounty_init(GUI* _g, Play* _p) +{ + sprite_init + ( + &_g->bountyBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_SMALL], + ORIGIN_MIDDLE, + GUI_BOUNTY_BACKGROUND_WIDTH, + GUI_BOUNTY_BACKGROUND_HEIGHT + ); + + _g->bountyBackgroundSprite.x = GUI_BOUNTY_SPRITE_OFFSET_X; + _g->bountyBackgroundSprite.y = GUI_BOUNTY_SPRITE_OFFSET_Y; + + _g->bountyBackgroundSprite.color.a = GUI_BOUNTY_ALPHA; + + gui_bounty_text_set(_g, _p); +} + +/* Frees bounty GUI. */ +void +gui_bounty_free(GUI* _g, Play* _p) +{ + if (_g->bountyText.atlas.texture) + texture_free(_g->bountyText.atlas.texture); +} + +/* Updates bounty GUI. */ +void +gui_bounty_update(GUI* _g, Play* _p) +{ + if (_p->isBountyUpdate) + { + gui_bounty_text_set(_g, _p); + _p->isBountyUpdate = FALSE; + } +} + +/* Draws bounty GUI. */ +void +gui_bounty_draw(GUI* _g, Play* _p) +{ + sprite_draw(&_g->bountyBackgroundSprite, _p->g->renderer); + sprite_draw(&_g->bountyText, _p->g->renderer); +} diff --git a/src/game/play/gui/gui_bounty.h b/src/game/play/gui/gui_bounty.h new file mode 100755 index 0000000..2420804 --- /dev/null +++ b/src/game/play/gui/gui_bounty.h @@ -0,0 +1,25 @@ +/* + * DESCRIPTION: + * Header file for gui_bounty.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "../../GAME_STRINGS.h" + +#define GUI_BOUNTY_TEXT_MAX 15 +#define GUI_BOUNTY_SPRITE_OFFSET_Y 48 +#define GUI_BOUNTY_SPRITE_OFFSET_X 424 +#define GUI_BOUNTY_TEXT_ROTATION -5 +#define GUI_BOUNTY_BACKGROUND_WIDTH 96 +#define GUI_BOUNTY_BACKGROUND_HEIGHT 96 +#define GUI_BOUNTY_ALPHA 196 + +static const SDL_Color GUI_BOUNTY_TEXT_COLOR = {255, 255, 255, 255}; + +void gui_bounty_init(GUI* _g, Play* _p); +void gui_bounty_free(GUI* _g, Play* _p); +void gui_bounty_update(GUI* _g, Play* _p); +void gui_bounty_draw(GUI* _g, Play* _p); + diff --git a/src/game/play/gui/gui_cash.c b/src/game/play/gui/gui_cash.c new file mode 100755 index 0000000..5e685b5 --- /dev/null +++ b/src/game/play/gui/gui_cash.c @@ -0,0 +1,118 @@ +/* + * DESCRIPTION: + * Displays a cash icon and the current cashs available. +*/ + +#include "gui_cash.h" + +/* Sets the gui cash text. */ +static void +gui_cash_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_CASH_TEXT_MAX); + + if (_g->cashText.atlas.texture) + texture_free(_g->cashText.atlas.texture); + + snprintf + ( + s, + GUI_CASH_TEXT_MAX, + "%09i", + _p->player.cash + ); + + text_sprite_init + ( + &_g->cashText, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + GUI_CASH_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + _g->cashText.x = _g->cashTextBackgroundSprite.x; + _g->cashText.y = _g->cashTextBackgroundSprite.y; + + _g->cashText.rotation = GUI_CASH_TEXT_ROTATION; + _g->cashText.color.a = GUI_CASH_ALPHA; + + free(s); +} + +/* Initializes cash GUI. */ +void +gui_cash_init(GUI* _g, Play* _p) +{ + sprite_init + ( + &_g->cashSprite, + _p->g->textures[TEXTURE_GUI_CASH], + ORIGIN_MIDDLE, + GUI_CASH_WIDTH, + GUI_CASH_HEIGHT + ); + + sprite_init + ( + &_g->cashBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_SMALL], + ORIGIN_MIDDLE, + GUI_CASH_BACKGROUND_WIDTH, + GUI_CASH_BACKGROUND_HEIGHT + ); + + sprite_init + ( + &_g->cashTextBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_BIG], + ORIGIN_MIDDLE, + GUI_CASH_TEXT_BACKGROUND_WIDTH, + GUI_CASH_TEXT_BACKGROUND_HEIGHT + ); + + _g->cashBackgroundSprite.x = GUI_CASH_SPRITE_OFFSET_X; + _g->cashBackgroundSprite.y = GUI_CASH_SPRITE_OFFSET_Y; + + _g->cashTextBackgroundSprite.x = GUI_CASH_TEXT_OFFSET_X; + _g->cashTextBackgroundSprite.y = GUI_CASH_TEXT_OFFSET_Y; + + _g->cashSprite.x = _g->cashBackgroundSprite.x; + _g->cashSprite.y = _g->cashBackgroundSprite.y; + + _g->cashBackgroundSprite.color.a = GUI_CASH_ALPHA; + _g->cashTextBackgroundSprite.color.a = GUI_CASH_ALPHA; + _g->cashSprite.color.a = GUI_CASH_ALPHA; + + gui_cash_text_set(_g, _p); +} + +/* Frees cash GUI. */ +void +gui_cash_free(GUI* _g, Play* _p) +{ + if (_g->cashText.atlas.texture) + texture_free(_g->cashText.atlas.texture); +} + +/* Updates cash GUI. */ +void +gui_cash_update(GUI* _g, Play* _p) +{ + if (_p->player.isCashUpdate) + gui_cash_text_set(_g, _p); +} + +/* Draws cash GUI. */ +void +gui_cash_draw(GUI* _g, Play* _p) +{ + sprite_draw(&_g->cashBackgroundSprite, _p->g->renderer); + sprite_draw(&_g->cashTextBackgroundSprite, _p->g->renderer); + sprite_draw(&_g->cashSprite, _p->g->renderer); + sprite_draw(&_g->cashText, _p->g->renderer); +} diff --git a/src/game/play/gui/gui_cash.h b/src/game/play/gui/gui_cash.h new file mode 100755 index 0000000..8264dae --- /dev/null +++ b/src/game/play/gui/gui_cash.h @@ -0,0 +1,31 @@ +/* + * DESCRIPTION: + * Header file for gui_cash.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "../../GAME_STRINGS.h" + +#define GUI_CASH_TEXT_MAX 10 +#define GUI_CASH_WIDTH 64 +#define GUI_CASH_HEIGHT 64 +#define GUI_CASH_BACKGROUND_WIDTH 96 +#define GUI_CASH_BACKGROUND_HEIGHT 96 +#define GUI_CASH_TEXT_BACKGROUND_WIDTH 192 +#define GUI_CASH_TEXT_BACKGROUND_HEIGHT 96 +#define GUI_CASH_SPRITE_OFFSET_X 48 +#define GUI_CASH_SPRITE_OFFSET_Y 64 +#define GUI_CASH_TEXT_OFFSET_X 196 +#define GUI_CASH_TEXT_OFFSET_Y 50 +#define GUI_CASH_TEXT_ROTATION -5 +#define GUI_CASH_ALPHA 196 + +static const SDL_Color GUI_CASH_TEXT_COLOR = {255, 255, 255, 255}; + +void gui_cash_init(GUI* _g, Play* _p); +void gui_cash_free(GUI* _g, Play* _p); +void gui_cash_update(GUI* _g, Play* _p); +void gui_cash_draw(GUI* _g, Play* _p); + diff --git a/src/game/play/gui/gui_ready.c b/src/game/play/gui/gui_ready.c new file mode 100755 index 0000000..16c662f --- /dev/null +++ b/src/game/play/gui/gui_ready.c @@ -0,0 +1,78 @@ +/* + * DESCRIPTION: + * Displays a ready icon and the current readys available. +*/ + +#include "gui_ready.h" + +/* Initializes ready GUI. */ +void +gui_ready_init(GUI* _g, Play* _p) +{ + Sprite sprite; + + sprite_init + ( + &_g->readyBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_SMALL], + ORIGIN_MIDDLE, + GUI_READY_BACKGROUND_WIDTH, + GUI_READY_BACKGROUND_HEIGHT + ); + + _g->readyBackgroundSprite.x = GUI_READY_OFFSET_X; + _g->readyBackgroundSprite.y = GUI_READY_OFFSET_Y; + + text_sprite_init + ( + &sprite, + _p->g->renderer, + _p->g->fonts[FONT_SMALL], + GUI_READY_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_GUI_READY, + GUI_READY_WRAP + ); + + button_init(&_g->readyButton, &sprite); + + _g->readyButton.sprite.x = _g->readyBackgroundSprite.x + GUI_READY_TEXT_OFFSET_X; + _g->readyButton.sprite.y = _g->readyBackgroundSprite.y + GUI_READY_TEXT_OFFSET_Y; + + _g->readyButton.sprite.rotation = GUI_READY_ROTATION; +} + +/* Frees ready GUI. */ +void +gui_ready_free(GUI* _g, Play* _p) +{ + if (_g->readyButton.sprite.atlas.texture) + texture_free(_g->readyButton.sprite.atlas.texture); +} + +/* Updates ready GUI. */ +void +gui_ready_update(GUI* _g, Play* _p) +{ + if (_p->isUpgradesActive) + { + button_update(&_g->readyButton, &_p->g->mouse); + + if (_g->readyButton.state == BUTTON_STATE_PRESS) + { + _p->isReady = TRUE; + sound_play(_p->g->sounds[SOUND_GO], -1); + } + } +} + +/* Draws ready GUI. */ +void +gui_ready_draw(GUI* _g, Play* _p) +{ + if (_p->isUpgradesActive) + { + sprite_draw(&_g->readyBackgroundSprite, _p->g->renderer); + button_draw(&_g->readyButton, _p->g->renderer); + } +} diff --git a/src/game/play/gui/gui_ready.h b/src/game/play/gui/gui_ready.h new file mode 100755 index 0000000..2f0fcc5 --- /dev/null +++ b/src/game/play/gui/gui_ready.h @@ -0,0 +1,27 @@ +/* + * DESCRIPTION: + * Header file for gui_ready.c. +*/ + +#pragma once + +#include "../../gui/button.h" +#include "../../GAME_STRINGS.h" + +#define GUI_READY_TEXT_MAX 30 +#define GUI_READY_OFFSET_X 752 +#define GUI_READY_OFFSET_Y 550 +#define GUI_READY_BACKGROUND_WIDTH 96 +#define GUI_READY_BACKGROUND_HEIGHT 96 +#define GUI_READY_ROTATION -5 +#define GUI_READY_WRAP 100 +#define GUI_READY_TEXT_OFFSET_X 16 +#define GUI_READY_TEXT_OFFSET_Y 0 + +static const SDL_Color GUI_READY_TEXT_COLOR = {255, 255, 255, 255}; + +void gui_ready_init(GUI* _g, Play* _p); +void gui_ready_free(GUI* _g, Play* _p); +void gui_ready_update(GUI* _g, Play* _p); +void gui_ready_draw(GUI* _g, Play* _p); + diff --git a/src/game/play/gui/gui_redirect.c b/src/game/play/gui/gui_redirect.c new file mode 100755 index 0000000..67debbb --- /dev/null +++ b/src/game/play/gui/gui_redirect.c @@ -0,0 +1,94 @@ +/* + * DESCRIPTION: + * Displays a redirect icon and the current redirects available. +*/ + +#include "gui_redirect.h" + +/* Sets the gui redirect text. */ +static void +gui_redirect_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_REDIRECT_TEXT_MAX); + + snprintf + ( + s, + GUI_REDIRECT_TEXT_MAX, + "%i/%i", + _p->player.redirects, + _p->player.redirectMax + ); + + if (_g->redirectText.atlas.texture) + texture_free(_g->redirectText.atlas.texture); + + text_sprite_init + ( + &_g->redirectText, + _p->g->renderer, + _p->g->fonts[FONT_SMALL], + GUI_REDIRECT_TEXT_COLOR, + ORIGIN_TOP_LEFT, + s, + 0 + ); + + free(s); + + _g->redirectText.x = _p->gui.cursor.sprite.x + GUI_REDIRECT_TEXT_OFFSET_X; + _g->redirectText.y = _p->gui.cursor.sprite.y + GUI_REDIRECT_TEXT_OFFSET_Y; +} + +/* Initializes redirect GUI. */ +void +gui_redirect_init(GUI* _g, Play* _p) +{ + sprite_init + ( + &_g->redirectSprite, + _p->g->textures[TEXTURE_GUI_REDIRECT], + ORIGIN_MIDDLE, + GUI_REDIRECT_WIDTH, + GUI_REDIRECT_HEIGHT + ); + + _g->redirectSprite.color.a = GUI_REDIRECT_ALPHA; + + _g->redirectSprite.x = _p->gui.cursor.sprite.x + GUI_REDIRECT_SPRITE_OFFSET_X; + _g->redirectSprite.y = _p->gui.cursor.sprite.y + GUI_REDIRECT_SPRITE_OFFSET_Y; + + gui_redirect_text_set(_g, _p); +} + +/* Frees redirect GUI. */ +void +gui_redirect_free(GUI* _g, Play* _p) +{ + if (_g->redirectText.atlas.texture) + texture_free(_g->redirectText.atlas.texture); +} + +/* Updates redirect GUI. */ +void +gui_redirect_update(GUI* _g, Play* _p) +{ + _g->redirectSprite.x = _p->gui.cursor.sprite.x + GUI_REDIRECT_SPRITE_OFFSET_X; + _g->redirectSprite.y = _p->gui.cursor.sprite.y + GUI_REDIRECT_SPRITE_OFFSET_Y; + + _g->redirectText.x = _p->gui.cursor.sprite.x + GUI_REDIRECT_TEXT_OFFSET_X; + _g->redirectText.y = _p->gui.cursor.sprite.y + GUI_REDIRECT_TEXT_OFFSET_Y; + + if (_p->player.isRedirectUpdate) + gui_redirect_text_set(_g, _p); +} + +/* Draws redirect GUI. */ +void +gui_redirect_draw(GUI* _g, Play* _p) +{ + sprite_draw(&_g->redirectSprite, _p->g->renderer); + sprite_draw(&_g->redirectText, _p->g->renderer); +} diff --git a/src/game/play/gui/gui_redirect.h b/src/game/play/gui/gui_redirect.h new file mode 100755 index 0000000..b2751f2 --- /dev/null +++ b/src/game/play/gui/gui_redirect.h @@ -0,0 +1,25 @@ +/* + * DESCRIPTION: + * Header file for redirect_gui.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" + +#define GUI_REDIRECT_TEXT_MAX 6 +#define GUI_REDIRECT_WIDTH 16 +#define GUI_REDIRECT_HEIGHT 16 +#define GUI_REDIRECT_SPRITE_OFFSET_X -16 +#define GUI_REDIRECT_SPRITE_OFFSET_Y -24 +#define GUI_REDIRECT_TEXT_OFFSET_X -8 +#define GUI_REDIRECT_TEXT_OFFSET_Y -28 +#define GUI_REDIRECT_ALPHA 128 + +static const SDL_Color GUI_REDIRECT_TEXT_COLOR = {0, 0, 0, 128}; + +void gui_redirect_init(GUI* _g, Play* _p); +void gui_redirect_free(GUI* _g, Play* _p); +void gui_redirect_update(GUI* _g, Play* _p); +void gui_redirect_draw(GUI* _g, Play* _p); + diff --git a/src/game/play/gui/gui_snake.c b/src/game/play/gui/gui_snake.c new file mode 100755 index 0000000..c5f054c --- /dev/null +++ b/src/game/play/gui/gui_snake.c @@ -0,0 +1,107 @@ +/* + * DESCRIPTION: + * Displays a snake icon and the current snakes available. +*/ + +#include "gui_snake.h" + +/* Sets the gui snake text. */ +static void +gui_snake_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_SNAKE_TEXT_MAX); + + if (_g->snakeText.atlas.texture) + texture_free(_g->snakeText.atlas.texture); + + if (_p->player.isPowerUp) + { + snprintf + ( + s, + GUI_SNAKE_TEXT_MAX, + "%s", + STRING_PLAY_GUI_SNAKE_POWER_UP + ); + } + else + { + snprintf + ( + s, + GUI_SNAKE_TEXT_MAX, + "%i/%i", + _p->player.snakeReserve, + _p->player.snakeMax + ); + } + + text_sprite_init + ( + &_g->snakeText, + _p->g->renderer, + _p->g->fonts[FONT_SMALL], + GUI_SNAKE_TEXT_COLOR, + ORIGIN_TOP_LEFT, + s, + 0 + ); + + free(s); + + _g->snakeText.x = _p->gui.cursor.sprite.x + GUI_SNAKE_TEXT_OFFSET_X; + _g->snakeText.y = _p->gui.cursor.sprite.y + GUI_SNAKE_TEXT_OFFSET_Y; +} + +/* Initializes snake GUI. */ +void +gui_snake_init(GUI* _g, Play* _p) +{ + sprite_init + ( + &_g->snakeSprite, + _p->g->textures[TEXTURE_GUI_SNAKE], + ORIGIN_MIDDLE, + GUI_SNAKE_WIDTH, + GUI_SNAKE_HEIGHT + ); + + _g->snakeSprite.color.a = GUI_SNAKE_ALPHA; + + _g->snakeSprite.x = _p->gui.cursor.sprite.x + GUI_SNAKE_SPRITE_OFFSET_X; + _g->snakeSprite.y = _p->gui.cursor.sprite.y + GUI_SNAKE_SPRITE_OFFSET_Y; + + gui_snake_text_set(_g, _p); +} + +/* Frees snake GUI. */ +void +gui_snake_free(GUI* _g, Play* _p) +{ + if (_g->snakeText.atlas.texture) + texture_free(_g->snakeText.atlas.texture); +} + +/* Updates snake GUI. */ +void +gui_snake_update(GUI* _g, Play* _p) +{ + _g->snakeSprite.x = _p->gui.cursor.sprite.x + GUI_SNAKE_SPRITE_OFFSET_X; + _g->snakeSprite.y = _p->gui.cursor.sprite.y + GUI_SNAKE_SPRITE_OFFSET_Y; + + _g->snakeText.x = _p->gui.cursor.sprite.x + GUI_SNAKE_TEXT_OFFSET_X; + _g->snakeText.y = _p->gui.cursor.sprite.y + GUI_SNAKE_TEXT_OFFSET_Y; + + if (_p->player.isSnakeUpdate) + gui_snake_text_set(_g, _p); +} + +/* Draws snake GUI. */ +void +gui_snake_draw(GUI* _g, Play* _p) +{ + sprite_draw(&_g->snakeSprite, _p->g->renderer); + sprite_draw(&_g->snakeText, _p->g->renderer); +} diff --git a/src/game/play/gui/gui_snake.h b/src/game/play/gui/gui_snake.h new file mode 100755 index 0000000..5fa37fa --- /dev/null +++ b/src/game/play/gui/gui_snake.h @@ -0,0 +1,26 @@ +/* + * DESCRIPTION: + * Header file for gui_snake.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "../../GAME_STRINGS.h" + +#define GUI_SNAKE_TEXT_MAX 10 +#define GUI_SNAKE_WIDTH 16 +#define GUI_SNAKE_HEIGHT 16 +#define GUI_SNAKE_SPRITE_OFFSET_X -16 +#define GUI_SNAKE_SPRITE_OFFSET_Y -40 +#define GUI_SNAKE_TEXT_OFFSET_X -8 +#define GUI_SNAKE_TEXT_OFFSET_Y -46 +#define GUI_SNAKE_ALPHA 128 + +static const SDL_Color GUI_SNAKE_TEXT_COLOR = {0, 0, 0, 128}; + +void gui_snake_init(GUI* _g, Play* _p); +void gui_snake_free(GUI* _g, Play* _p); +void gui_snake_update(GUI* _g, Play* _p); +void gui_snake_draw(GUI* _g, Play* _p); + diff --git a/src/game/play/gui/gui_upgrade.c b/src/game/play/gui/gui_upgrade.c new file mode 100755 index 0000000..7828820 --- /dev/null +++ b/src/game/play/gui/gui_upgrade.c @@ -0,0 +1,496 @@ +/* + * DESCRIPTION: + * Upgrade display. +*/ + +#include "gui_upgrade.h" + +static void gui_upgrade_no_cash(GUI* _g, Play* _p); +static void gui_upgrade_maxed(GUI* _g, Play* _p); +static void gui_upgrade_snake(GUI* _g, Play* _p); +static void gui_upgrade_lifetime(GUI* _g, Play* _p); +static void gui_upgrade_redirect(GUI* _g, Play* _p); +static void gui_upgrade_snake_text_set(GUI* _g, Play* _p); + + +/* No cash!! */ +static void +gui_upgrade_no_cash(GUI* _g, Play* _p) +{ + message_no_cash_add(_p->player.sprite.x, _p->player.sprite.y, _p); + + sound_play(_p->g->sounds[SOUND_ERROR], -1); +} + +/* Maxed out!! */ +static void +gui_upgrade_maxed(GUI* _g, Play* _p) +{ + message_maxed_add(_p->player.sprite.x, _p->player.sprite.y, _p); + + sound_play(_p->g->sounds[SOUND_ERROR], -1); +} + +/* Upgrade snake level. */ +static void +gui_upgrade_snake(GUI* _g, Play* _p) +{ + if (_p->player.cash >= _p->player.snakeUpgradePrice) + { + _p->player.cash -= _p->player.snakeUpgradePrice; + + message_purchase_add(_p->player.sprite.x, _p->player.sprite.y, _p->player.snakeUpgradePrice, _p); + + _p->player.snakeUpgradeLevel++; + + if (_p->player.snakeUpgradeLevel > GUI_UPGRADE_MAX) + { + _p->player.snakeUpgradeLevel = CEIL(_p->player.snakeUpgradeLevel, GUI_UPGRADE_MAX); + gui_upgrade_maxed(_g, _p); + } + else + { + _p->player.isSnakeUpgradeUpdate = TRUE; + + _p->player.isSnakeUpdate = TRUE; + + sound_play(_p->g->sounds[SOUND_PURCHASE], -1); + } + } + else + gui_upgrade_no_cash(_g, _p); +} + +/* Upgrade snake level. */ +static void +gui_upgrade_lifetime(GUI* _g, Play* _p) +{ + if (_p->player.cash >= _p->player.lifetimeUpgradePrice) + { + _p->player.cash -= _p->player.lifetimeUpgradePrice; + + message_purchase_add(_p->player.sprite.x, _p->player.sprite.y, _p->player.lifetimeUpgradePrice, _p); + + _p->player.lifetimeUpgradeLevel++; + + if (_p->player.snakeUpgradeLevel > GUI_UPGRADE_MAX) + _p->player.lifetimeUpgradeLevel = CEIL(_p->player.lifetimeUpgradeLevel, GUI_UPGRADE_MAX); + else + { + _p->player.isLifetimeUpgradeUpdate = TRUE; + sound_play(_p->g->sounds[SOUND_PURCHASE], -1); + } + } + else + gui_upgrade_no_cash(_g, _p); +} + +/* Upgrade snake level. */ +static void +gui_upgrade_redirect(GUI* _g, Play* _p) +{ + if (_p->player.cash >= _p->player.redirectUpgradePrice) + { + _p->player.cash -= _p->player.redirectUpgradePrice; + + message_purchase_add(_p->player.sprite.x, _p->player.sprite.y, _p->player.redirectUpgradePrice, _p); + + _p->player.redirectUpgradeLevel++; + + if (_p->player.redirectUpgradeLevel > GUI_UPGRADE_MAX) + _p->player.redirectUpgradeLevel = CEIL(_p->player.redirectUpgradeLevel, GUI_UPGRADE_MAX); + else + { + _p->player.isRedirectUpgradeUpdate = TRUE; + + _p->player.isRedirectUpdate = TRUE; + + sound_play(_p->g->sounds[SOUND_PURCHASE], -1); + } + } + else + gui_upgrade_no_cash(_g, _p); +} + +/* Sets the gui upgrade text. */ +static void +gui_upgrade_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_UPGRADE_TEXT_MAX); + + if (_g->upgradeText.atlas.texture) + texture_free(_g->upgradeText.atlas.texture); + + if (_g->snakeUpgradeButton.state == BUTTON_STATE_HOVER) + { + snprintf + ( + s, + GUI_UPGRADE_TEXT_MAX, + "%s", + STRING_PLAY_UPGRADE_SNAKES + ); + } + else if (_g->lifetimeUpgradeButton.state == BUTTON_STATE_HOVER) + { + snprintf + ( + s, + GUI_UPGRADE_TEXT_MAX, + "%s", + STRING_PLAY_UPGRADE_LIFETIME + ); + } + else if (_g->redirectUpgradeButton.state == BUTTON_STATE_HOVER) + { + snprintf + ( + s, + GUI_UPGRADE_TEXT_MAX, + "%s", + STRING_PLAY_UPGRADE_REDIRECT + ); + } + else + { + snprintf + ( + s, + GUI_UPGRADE_TEXT_MAX, + "%s", + STRING_PLAY_UPGRADE + ); + } + + text_sprite_init + ( + &_g->upgradeText, + _p->g->renderer, + _p->g->fonts[FONT_TINY], + GUI_UPGRADE_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + GUI_UPGRADE_WRAP + ); + + free(s); + + _g->upgradeText.x = _g->upgradeBackgroundSprite.x; + _g->upgradeText.y = _g->upgradeBackgroundSprite.y; + + _g->upgradeText.rotation = GUI_UPGRADE_ROTATION; +} + +/* Displays upgrade quantity and price of the snake upgrade. */ +static void +gui_upgrade_snake_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_UPGRADE_INFO_TEXT_MAX); + + if (_g->snakeUpgradeText.atlas.texture) + texture_free(_g->snakeUpgradeText.atlas.texture); + + snprintf + ( + s, + GUI_UPGRADE_INFO_TEXT_MAX, + "(x%i) - $%i", + _p->player.snakeUpgradeLevel, + _p->player.snakeUpgradePrice + ); + + text_sprite_init + ( + &_g->snakeUpgradeText, + _p->g->renderer, + _p->g->fonts[FONT_SMALL], + GUI_UPGRADE_INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + free(s); + + _g->snakeUpgradeText.x = _g->snakeUpgradeButton.sprite.x + GUI_UPGRADE_TEXT_OFFSET_X; + _g->snakeUpgradeText.y = _g->snakeUpgradeButton.sprite.y + GUI_UPGRADE_TEXT_OFFSET_Y; +} + +/* Displays upgrade quantity and price of the lifetime upgrade. */ +static void +gui_upgrade_lifetime_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_UPGRADE_INFO_TEXT_MAX); + + if (_g->lifetimeUpgradeText.atlas.texture) + texture_free(_g->lifetimeUpgradeText.atlas.texture); + + snprintf + ( + s, + GUI_UPGRADE_INFO_TEXT_MAX, + "(x%i) - $%i", + _p->player.lifetimeUpgradeLevel, + _p->player.lifetimeUpgradePrice + ); + + text_sprite_init + ( + &_g->lifetimeUpgradeText, + _p->g->renderer, + _p->g->fonts[FONT_SMALL], + GUI_UPGRADE_INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + free(s); + + _g->lifetimeUpgradeText.x = _g->lifetimeUpgradeButton.sprite.x + GUI_UPGRADE_TEXT_OFFSET_X; + _g->lifetimeUpgradeText.y = _g->lifetimeUpgradeButton.sprite.y + GUI_UPGRADE_TEXT_OFFSET_Y; +} + +/* Displays upgrade quantity and price of the redirect upgrade. */ +static void +gui_upgrade_redirect_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_UPGRADE_INFO_TEXT_MAX); + + if (_g->redirectUpgradeText.atlas.texture) + texture_free(_g->redirectUpgradeText.atlas.texture); + + snprintf + ( + s, + GUI_UPGRADE_INFO_TEXT_MAX, + "(x%i) - $%i", + _p->player.redirectUpgradeLevel, + _p->player.redirectUpgradePrice + ); + + text_sprite_init + ( + &_g->redirectUpgradeText, + _p->g->renderer, + _p->g->fonts[FONT_SMALL], + GUI_UPGRADE_INFO_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + free(s); + + _g->redirectUpgradeText.x = _g->redirectUpgradeButton.sprite.x + GUI_UPGRADE_TEXT_OFFSET_X; + _g->redirectUpgradeText.y = _g->redirectUpgradeButton.sprite.y + GUI_UPGRADE_TEXT_OFFSET_Y; +} + + +/* Initializes upgrade GUI. */ +void +gui_upgrade_init(GUI* _g, Play* _p) +{ + Sprite snakeUpgradeSprite; + Sprite redirectUpgradeSprite; + Sprite lifetimeUpgradeSprite; + + sprite_init + ( + &snakeUpgradeSprite, + _p->g->textures[TEXTURE_GUI_UPGRADE_SNAKE], + ORIGIN_MIDDLE, + GUI_UPGRADE_WIDTH, + GUI_UPGRADE_HEIGHT + ); + + sprite_init + ( + &lifetimeUpgradeSprite, + _p->g->textures[TEXTURE_GUI_UPGRADE_LIFETIME], + ORIGIN_MIDDLE, + GUI_UPGRADE_WIDTH, + GUI_UPGRADE_HEIGHT + ); + + sprite_init + ( + &redirectUpgradeSprite, + _p->g->textures[TEXTURE_GUI_UPGRADE_REDIRECT], + ORIGIN_MIDDLE, + GUI_UPGRADE_WIDTH, + GUI_UPGRADE_HEIGHT + ); + + sprite_init + ( + &_g->upgradeBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_BIG], + ORIGIN_MIDDLE, + GUI_UPGRADE_BACKGROUND_BIG_WIDTH, + GUI_UPGRADE_BACKGROUND_BIG_HEIGHT + ); + + sprite_init + ( + &_g->snakeUpgradeBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_SMALL], + ORIGIN_MIDDLE, + GUI_UPGRADE_BACKGROUND_SMALL_WIDTH, + GUI_UPGRADE_BACKGROUND_SMALL_HEIGHT + ); + + sprite_init + ( + &_g->lifetimeUpgradeBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_SMALL], + ORIGIN_MIDDLE, + GUI_UPGRADE_BACKGROUND_SMALL_WIDTH, + GUI_UPGRADE_BACKGROUND_SMALL_HEIGHT + ); + + sprite_init + ( + &_g->redirectUpgradeBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_SMALL], + ORIGIN_MIDDLE, + GUI_UPGRADE_BACKGROUND_SMALL_WIDTH, + GUI_UPGRADE_BACKGROUND_SMALL_HEIGHT + ); + + sprite_init + ( + &_g->readyBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_SMALL], + ORIGIN_MIDDLE, + GUI_UPGRADE_BACKGROUND_SMALL_WIDTH, + GUI_UPGRADE_BACKGROUND_SMALL_HEIGHT + ); + + _g->upgradeBackgroundSprite.x = GUI_UPGRADE_BACKGROUND_SPRITE_OFFSET_X; + _g->upgradeBackgroundSprite.y = GUI_UPGRADE_BACKGROUND_SPRITE_OFFSET_Y; + + _g->snakeUpgradeBackgroundSprite.x = GUI_UPGRADE_SNAKE_SPRITE_OFFSET_X; + _g->lifetimeUpgradeBackgroundSprite.x = GUI_UPGRADE_LIFETIME_SPRITE_OFFSET_X; + _g->redirectUpgradeBackgroundSprite.x = GUI_UPGRADE_REDIRECT_SPRITE_OFFSET_X; + _g->snakeUpgradeBackgroundSprite.y = GUI_UPGRADE_SNAKE_SPRITE_OFFSET_Y; + _g->lifetimeUpgradeBackgroundSprite.y = GUI_UPGRADE_LIFETIME_SPRITE_OFFSET_Y; + _g->redirectUpgradeBackgroundSprite.y = GUI_UPGRADE_REDIRECT_SPRITE_OFFSET_Y; + + button_init(&_g->snakeUpgradeButton, &snakeUpgradeSprite); + button_init(&_g->lifetimeUpgradeButton, &lifetimeUpgradeSprite); + button_init(&_g->redirectUpgradeButton, &redirectUpgradeSprite); + + _g->snakeUpgradeButton.sprite.x = _g->snakeUpgradeBackgroundSprite.x; + _g->snakeUpgradeButton.sprite.y = _g->snakeUpgradeBackgroundSprite.y; + _g->lifetimeUpgradeButton.sprite.x = _g->lifetimeUpgradeBackgroundSprite.x; + _g->lifetimeUpgradeButton.sprite.y = _g->lifetimeUpgradeBackgroundSprite.y; + _g->redirectUpgradeButton.sprite.x = _g->redirectUpgradeBackgroundSprite.x; + _g->redirectUpgradeButton.sprite.y = _g->redirectUpgradeBackgroundSprite.y; + + gui_upgrade_text_set(_g, _p); + gui_upgrade_snake_text_set(_g, _p); + gui_upgrade_lifetime_text_set(_g, _p); + gui_upgrade_redirect_text_set(_g, _p); +} + +/* Frees upgrade GUI. */ +void +gui_upgrade_free(GUI* _g, Play* _p) +{ + if (_g->upgradeText.atlas.texture) + texture_free(_g->upgradeText.atlas.texture); + + if (_g->snakeUpgradeText.atlas.texture) + texture_free(_g->snakeUpgradeText.atlas.texture); + + if (_g->lifetimeUpgradeText.atlas.texture) + texture_free(_g->lifetimeUpgradeText.atlas.texture); + + if (_g->redirectUpgradeText.atlas.texture) + texture_free(_g->redirectUpgradeText.atlas.texture); +} + +/* Updates upgrade GUI. */ +void +gui_upgrade_update(GUI* _g, Play* _p) +{ + if (_p->isUpgradeUpdate) + _p->isUpgradeUpdate = FALSE; + + if (_p->player.isSnakeUpgradeUpdate) + { + gui_upgrade_snake_text_set(_g, _p); + _p->player.isSnakeUpgradeUpdate = FALSE; + } + + if (_p->player.isLifetimeUpgradeUpdate) + { + gui_upgrade_lifetime_text_set(_g, _p); + _p->player.isLifetimeUpgradeUpdate = FALSE; + } + + if (_p->player.isRedirectUpgradeUpdate) + { + gui_upgrade_redirect_text_set(_g, _p); + _p->player.isRedirectUpgradeUpdate = FALSE; + } + + if (_p->isUpgradesActive) + { + button_update(&_g->snakeUpgradeButton, &_p->g->mouse); + button_update(&_g->lifetimeUpgradeButton, &_p->g->mouse); + button_update(&_g->redirectUpgradeButton, &_p->g->mouse); + + if (_g->snakeUpgradeButton.state == BUTTON_STATE_PRESS) + gui_upgrade_snake(_g, _p); + + if (_g->lifetimeUpgradeButton.state == BUTTON_STATE_PRESS) + gui_upgrade_lifetime(_g, _p); + + if (_g->redirectUpgradeButton.state == BUTTON_STATE_PRESS) + gui_upgrade_redirect(_g, _p); + + if + ( + _g->snakeUpgradeButton.state == BUTTON_STATE_HOVER || + _g->lifetimeUpgradeButton.state == BUTTON_STATE_HOVER || + _g->redirectUpgradeButton.state == BUTTON_STATE_HOVER + ) + _p->isUpgradeUpdate = TRUE; + + if (_p->isUpgradeUpdate) + gui_upgrade_text_set(_g, _p); + } +} + +/* Draws upgrade GUI. */ +void +gui_upgrade_draw(GUI* _g, Play* _p) +{ + if (_p->isUpgradesActive) + { + sprite_draw(&_g->upgradeBackgroundSprite, _p->g->renderer); + sprite_draw(&_g->upgradeText, _p->g->renderer); + + sprite_draw(&_g->snakeUpgradeBackgroundSprite, _p->g->renderer); + sprite_draw(&_g->lifetimeUpgradeBackgroundSprite, _p->g->renderer); + sprite_draw(&_g->redirectUpgradeBackgroundSprite, _p->g->renderer); + + button_draw(&_g->snakeUpgradeButton, _p->g->renderer); + button_draw(&_g->lifetimeUpgradeButton, _p->g->renderer); + button_draw(&_g->redirectUpgradeButton, _p->g->renderer); + + sprite_draw(&_g->snakeUpgradeText, _p->g->renderer); + sprite_draw(&_g->lifetimeUpgradeText, _p->g->renderer); + sprite_draw(&_g->redirectUpgradeText, _p->g->renderer); + } +} diff --git a/src/game/play/gui/gui_upgrade.h b/src/game/play/gui/gui_upgrade.h new file mode 100755 index 0000000..5107034 --- /dev/null +++ b/src/game/play/gui/gui_upgrade.h @@ -0,0 +1,43 @@ +/* + * DESCRIPTION: + * Header file for gui_upgrade.c. +*/ + +#pragma once + +#include "../../gui/button.h" + +#include "../../GAME_STRINGS.h" + +#include "../message/message.h" + +#define GUI_UPGRADE_BACKGROUND_BIG_WIDTH 196 +#define GUI_UPGRADE_BACKGROUND_BIG_HEIGHT 98 +#define GUI_UPGRADE_BACKGROUND_SMALL_WIDTH 96 +#define GUI_UPGRADE_BACKGROUND_SMALL_HEIGHT 98 +#define GUI_UPGRADE_BACKGROUND_SPRITE_OFFSET_X 400 +#define GUI_UPGRADE_BACKGROUND_SPRITE_OFFSET_Y 450 +#define GUI_UPGRADE_SNAKE_SPRITE_OFFSET_Y 550 +#define GUI_UPGRADE_LIFETIME_SPRITE_OFFSET_Y 550 +#define GUI_UPGRADE_REDIRECT_SPRITE_OFFSET_Y 550 +#define GUI_UPGRADE_SNAKE_SPRITE_OFFSET_X 200 +#define GUI_UPGRADE_LIFETIME_SPRITE_OFFSET_X 400 +#define GUI_UPGRADE_REDIRECT_SPRITE_OFFSET_X 600 +#define GUI_UPGRADE_WRAP 150 +#define GUI_UPGRADE_ROTATION -5 +#define GUI_UPGRADE_TEXT_MAX 0xFF +#define GUI_UPGRADE_INFO_TEXT_MAX 0xFF +#define GUI_UPGRADE_TEXT_OFFSET_X 0 +#define GUI_UPGRADE_TEXT_OFFSET_Y -48 +#define GUI_UPGRADE_WIDTH 64 +#define GUI_UPGRADE_HEIGHT 64 +#define GUI_UPGRADE_MAX 99 + +static const SDL_Color GUI_UPGRADE_TEXT_COLOR = {255, 255, 255, 255}; +static const SDL_Color GUI_UPGRADE_INFO_TEXT_COLOR = {0, 0, 0, 255}; + +void gui_upgrade_init(GUI* _g, Play* _p); +void gui_upgrade_free(GUI* _g, Play* _p); +void gui_upgrade_update(GUI* _g, Play* _p); +void gui_upgrade_draw(GUI* _g, Play* _p); + diff --git a/src/game/play/gui/gui_wave.c b/src/game/play/gui/gui_wave.c new file mode 100755 index 0000000..65a4702 --- /dev/null +++ b/src/game/play/gui/gui_wave.c @@ -0,0 +1,121 @@ +/* + * DESCRIPTION: + * Displays a wave icon and the current waves available. +*/ + +#include "gui_wave.h" + +/* Sets the gui wave text. */ +static void +gui_wave_text_set(GUI* _g, Play* _p) +{ + char* s; + + s = malloc(GUI_WAVE_TEXT_MAX); + + if (_g->waveText.atlas.texture) + texture_free(_g->waveText.atlas.texture); + + if (_p->isBetweenWaves) + { + snprintf + ( + s, + GUI_WAVE_TEXT_MAX, + "%s-", + STRING_PLAY_GUI_WAVE + ); + } + else + { + if (_p->game > 1) + { + snprintf + ( + s, + GUI_WAVE_TEXT_MAX, + "%s%i (NG+%i)", + STRING_PLAY_GUI_WAVE, + _p->wave, + _p->game - 1 + ); + } + else + { + snprintf + ( + s, + GUI_WAVE_TEXT_MAX, + "%s%i", + STRING_PLAY_GUI_WAVE, + _p->wave + ); + } + } + + text_sprite_init + ( + &_g->waveText, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + GUI_WAVE_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + _g->waveText.x = _g->waveBackgroundSprite.x; + _g->waveText.y = _g->waveBackgroundSprite.y; + + _g->waveText.rotation = GUI_WAVE_TEXT_ROTATION; + _g->waveText.color.a = GUI_WAVE_ALPHA; + + free(s); +} + +/* Initializes wave GUI. */ +void +gui_wave_init(GUI* _g, Play* _p) +{ + sprite_init + ( + &_g->waveBackgroundSprite, + _p->g->textures[TEXTURE_GUI_SIGN_BIG], + ORIGIN_MIDDLE, + GUI_WAVE_BACKGROUND_WIDTH, + GUI_WAVE_BACKGROUND_HEIGHT + ); + + _g->waveBackgroundSprite.flip = FLIP_HORIZONTAL; + + _g->waveBackgroundSprite.x = GUI_WAVE_SPRITE_OFFSET_X; + _g->waveBackgroundSprite.y = GUI_WAVE_SPRITE_OFFSET_Y; + + _g->waveBackgroundSprite.color.a = GUI_WAVE_ALPHA; + + gui_wave_text_set(_g, _p); +} + +/* Frees wave GUI. */ +void +gui_wave_free(GUI* _g, Play* _p) +{ + if (_g->waveText.atlas.texture) + texture_free(_g->waveText.atlas.texture); +} + +/* Updates wave GUI. */ +void +gui_wave_update(GUI* _g, Play* _p) +{ + if (_p->isWaveUpdate) + gui_wave_text_set(_g, _p); +} + +/* Draws wave GUI. */ +void +gui_wave_draw(GUI* _g, Play* _p) +{ + sprite_draw(&_g->waveBackgroundSprite, _p->g->renderer); + sprite_draw(&_g->waveText, _p->g->renderer); +} diff --git a/src/game/play/gui/gui_wave.h b/src/game/play/gui/gui_wave.h new file mode 100755 index 0000000..cc363e7 --- /dev/null +++ b/src/game/play/gui/gui_wave.h @@ -0,0 +1,25 @@ +/* + * DESCRIPTION: + * Header file for gui_wave.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "../../GAME_STRINGS.h" + +#define GUI_WAVE_TEXT_MAX 30 +#define GUI_WAVE_SPRITE_OFFSET_Y 50 +#define GUI_WAVE_SPRITE_OFFSET_X 704 +#define GUI_WAVE_TEXT_ROTATION 5 +#define GUI_WAVE_BACKGROUND_WIDTH 192 +#define GUI_WAVE_BACKGROUND_HEIGHT 96 +#define GUI_WAVE_ALPHA 196 + +static const SDL_Color GUI_WAVE_TEXT_COLOR = {255, 255, 255, 255}; + +void gui_wave_init(GUI* _g, Play* _p); +void gui_wave_free(GUI* _g, Play* _p); +void gui_wave_update(GUI* _g, Play* _p); +void gui_wave_draw(GUI* _g, Play* _p); + diff --git a/src/game/play/message/message.c b/src/game/play/message/message.c new file mode 100755 index 0000000..7ad75d7 --- /dev/null +++ b/src/game/play/message/message.c @@ -0,0 +1,331 @@ +/* + * DESCRIPTION: + * Messages, or in-game blurbs that display happenings. +*/ + +#include "message.h" + +static Message* message_add(Message* _m, f64 _x, f64 _y, Play* _p); + +/* Adds a message to the list. */ +static Message* +message_add(Message* _m, f64 _x, f64 _y, Play* _p) +{ + _m->sprite.x = _x; + _m->sprite.y = _y; + _m->dR = 0; + _m->dX = 0; + _m->dY = MESSAGE_VELOCITY; + + _m->lifetime = MESSAGE_LIFETIME; + + return (Message*)list_append_new(&_p->messages, (void*)_m, sizeof(Message)); +} + +/* +$X */ +Message* +message_cash_add(f64 _x, f64 _y, s32 _c, Play* _p) +{ + Message m; + char* s; + + s = malloc(MESSAGE_TEXT_MAX); + + snprintf + ( + s, + MESSAGE_TEXT_MAX, + "+$%i", + _c + ); + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_CASH_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + free(s); + + return message_add(&m, _x, _y, _p); +} + +/* -$X */ +Message* +message_purchase_add(f64 _x, f64 _y, s32 _c, Play* _p) +{ + Message m; + char* s; + + s = malloc(MESSAGE_TEXT_MAX); + + snprintf + ( + s, + MESSAGE_TEXT_MAX, + "-$%i", + _c + ); + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_PURCHASE_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + free(s); + + return message_add(&m, _x, _y, _p); +} + +/* "You're broke!!" */ +Message* +message_no_cash_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_PURCHASE_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_PURCHASE_NO_CASH, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + +/* "MAXED!!" */ +Message* +message_maxed_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_PURCHASE_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_PURCHASE_MAXED, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + +/* -X */ +Message* +message_damage_add(f64 _x, f64 _y, s32 _d, Play* _p) +{ + Message m; + char* s; + + s = malloc(MESSAGE_TEXT_MAX); + + snprintf + ( + s, + MESSAGE_TEXT_MAX, + "-%i", + _d + ); + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_DAMAGE_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + free(s); + + return message_add(&m, _x, _y, _p); +} + +/* "Wave cleared!" */ +Message* +message_wave_clear_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_WAVE_CLEAR_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_WAVE_CLEAR, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + +/* "Wave failed..." */ +Message* +message_wave_fail_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_WAVE_FAIL_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_WAVE_FAIL, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + +/* "Game cleared!!" */ +Message* +message_game_clear_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_GAME_CLEAR_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_GAME_CLEAR, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + + +/* "BERSERK!!" */ +Message* +message_power_up_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_POWER_UP_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_POWER_UP, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + + +/* "Berserk over..." */ +Message* +message_power_down_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_POWER_DOWN_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_POWER_DOWN, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + +/* "-1 Fatness!!" */ +Message* +message_miracle_cure_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_MIRACLE_CURE_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_MIRACLE_CURE, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + +/* "KABOOM!!" */ +Message* +message_dynamite_add(f64 _x, f64 _y, Play* _p) +{ + Message m; + + text_sprite_init + ( + &m.sprite, + _p->g->renderer, + _p->g->fonts[FONT_MEDIUM], + MESSAGE_DYNAMITE_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_MESSAGE_DYNAMITE, + 0 + ); + + return message_add(&m, _x, _y, _p); +} + +/* Updates a message. */ +void +message_update(Message* _m, Play* _p) +{ + f64 progress; + + progress = ((f64)_m->lifetime / MESSAGE_LIFETIME); + + _m->lifetime--; + + _m->sprite.x += _m->dX; + _m->sprite.y += _m->dY; + _m->sprite.rotation += _m->dR; + + _m->sprite.color.a = SDL_ALPHA_OPAQUE * progress; + + if (_m->lifetime <= 0) + list_node_from_data_remove(&_p->messages, _m, sizeof(Message)); +} + +/* Draws a message. */ +void +message_draw(Message* _m, Play* _p) +{ + sprite_draw(&_m->sprite, _p->g->renderer); +} diff --git a/src/game/play/message/message.h b/src/game/play/message/message.h new file mode 100755 index 0000000..fd0546c --- /dev/null +++ b/src/game/play/message/message.h @@ -0,0 +1,41 @@ +/* + * DESCRIPTION: + * Header file for message.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "../../GAME_STRINGS.h" + +#define MESSAGE_VELOCITY -1 +#define MESSAGE_LIFETIME 60 +#define MESSAGE_TEXT_MAX 20 + +static const SDL_Color MESSAGE_CASH_COLOR = {64, 255, 64, 255}; +static const SDL_Color MESSAGE_PURCHASE_COLOR = {255, 64, 64, 255}; +static const SDL_Color MESSAGE_MAXED_COLOR = {255, 255, 64, 255}; +static const SDL_Color MESSAGE_DAMAGE_COLOR = {255, 64, 64, 255}; +static const SDL_Color MESSAGE_WAVE_CLEAR_COLOR = {0, 0, 0, 255}; +static const SDL_Color MESSAGE_WAVE_FAIL_COLOR = {255, 64, 64, 255}; +static const SDL_Color MESSAGE_GAME_CLEAR_COLOR = {255, 255, 64, 255}; +static const SDL_Color MESSAGE_POWER_UP_COLOR = {255, 64, 255, 255}; +static const SDL_Color MESSAGE_POWER_DOWN_COLOR = {255, 64, 255, 255}; +static const SDL_Color MESSAGE_MIRACLE_CURE_COLOR = {225, 255, 225, 255}; +static const SDL_Color MESSAGE_DYNAMITE_COLOR = {0, 0, 0, 255}; + +Message* message_cash_add(f64 _x, f64 _y, s32 _c, Play* _p); +Message* message_no_cash_add(f64 _x, f64 _y, Play* _p); +Message* message_maxed_add(f64 _x, f64 _y, Play* _p); +Message* message_broke_add(f64 _x, f64 _y, Play* _p); +Message* message_purchase_add(f64 _x, f64 _y, s32 _c, Play* _p); +Message* message_damage_add(f64 _x, f64 _y, s32 _d, Play* _p); +Message* message_wave_clear_add(f64 _x, f64 _y, Play* _p); +Message* message_wave_fail_add(f64 _x, f64 _y, Play* _p); +Message* message_game_clear_add(f64 _x, f64 _y, Play* _p); +Message* message_power_up_add(f64 _x, f64 _y, Play* _p); +Message* message_power_down_add(f64 _x, f64 _y, Play* _p); +Message* message_miracle_cure_add(f64 _x, f64 _y, Play* _p); +Message* message_dynamite_add(f64 _x, f64 _y, Play* _p); +void message_draw(Message* _m, Play* _p); +void message_update(Message* _m, Play* _p); diff --git a/src/game/play/message/messages.c b/src/game/play/message/messages.c new file mode 100755 index 0000000..d06144e --- /dev/null +++ b/src/game/play/message/messages.c @@ -0,0 +1,60 @@ +/* + * DESCRIPTION: + * Handles play messages. +*/ + +#include "messages.h" + +/* Initializes messages. */ +void +messages_init(Play* _p) +{ + list_init(&_p->messages); +} + +/* Frees messages. */ +void +messages_free(Play* _p) +{ + list_free(&_p->messages); +} + +/* Updates messages. */ +void +messages_update(Play* _p) +{ + Message** messages; + s32 messageCount; + + messages = (Message**)list_data_get(&_p->messages); + messageCount = _p->messages.count; + + for (s32 i = 0; i < messageCount; i++) + { + Message* m; + + m = messages[i]; + + message_update(m, _p); + } +} + +/* Draws messages. */ +void +messages_draw(Play* _p) +{ + Message** messages; + s32 messageCount; + + messages = (Message**)list_data_get(&_p->messages); + messageCount = _p->messages.count; + + for (s32 i = 0; i < messageCount; i++) + { + Message* m; + + m = messages[i]; + + message_draw(m, _p); + } +} diff --git a/src/game/play/message/messages.h b/src/game/play/message/messages.h new file mode 100755 index 0000000..ab65f7d --- /dev/null +++ b/src/game/play/message/messages.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for messages.c. +*/ + +#pragma once + +#include "message.h" + +void messages_init(Play* _p); +void messages_update(Play* _p); +void messages_draw(Play* _p); +void messages_free(Play* _p); diff --git a/src/game/play/pause/pause.c b/src/game/play/pause/pause.c new file mode 100755 index 0000000..5d0bf19 --- /dev/null +++ b/src/game/play/pause/pause.c @@ -0,0 +1,25 @@ +/* + * DESCRIPTION: + * Gameplay pausing code!!! +*/ + +#include "pause.h" + +/* Initializes pause. */ +void +pause_init(Play* _p) +{ + _p->isPaused = FALSE; +} + +/* Updates pause. */ +void +pause_update(Play* _p) +{ + if (keyboard_press(&_p->g->keyboard, KEYBOARD_P)) + { + _p->isPaused = !_p->isPaused; + + sound_play(_p->g->sounds[SOUND_MENU], -1); + } +} diff --git a/src/game/play/pause/pause.h b/src/game/play/pause/pause.h new file mode 100755 index 0000000..047ce71 --- /dev/null +++ b/src/game/play/pause/pause.h @@ -0,0 +1,11 @@ +/* + * DESCRIPTION: + * Header file for pause.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" + +void pause_init(Play* _p); +void pause_update(Play* _p); diff --git a/src/game/play/play.c b/src/game/play/play.c new file mode 100755 index 0000000..8d2d423 --- /dev/null +++ b/src/game/play/play.c @@ -0,0 +1,116 @@ +/* + * DESCRIPTION: + * Main gameplay functions. +*/ + +#include "play.h" + +/* Initializes gameplay resources. */ +void +play_init(Play* _p, Game* _g) +{ + memset(_p, '\0', sizeof(Play)); + + _p->g = _g; + + music_play(_p->g->music[MUSIC_PLAY], TRUE); + + player_init(&_p->player, _p); + enemies_init(&_p->enemies, _p); + collectibles_init(&_p->collectibles, _p); + + wave_init(_p); + spawn_init(_p); + pause_init(_p); + + gui_init(&_p->gui, _p); + messages_init(_p); + + tutorial_init(_p); + + text_sprite_init + ( + &_p->pauseText, + _p->g->renderer, + _p->g->fonts[FONT_MEGA], + PLAY_PAUSE_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_PAUSE, + 0 + ); + + _p->pauseText.x = PLAY_PAUSE_X_OFFSET; + _p->pauseText.y = PLAY_PAUSE_Y_OFFSET; + + sprite_init + ( + &_p->bg, + _p->g->textures[TEXTURE_PLAY_BG], + ORIGIN_TOP_LEFT, + PLAY_BG_WIDTH, + PLAY_BG_HEIGHT + ); + + _p->bg.x = PLAY_BG_OFFSET_X; + _p->bg.y = PLAY_BG_OFFSET_Y; +} + +/* Frees gameplay resources. */ +void +play_free(Play* _p) +{ + enemies_free(&_p->enemies, _p); + collectibles_free(&_p->collectibles, _p); + player_free(&_p->player, _p); + gui_free(&_p->gui, _p); + messages_free(_p); + tutorial_free(_p); +} + +/* Updates gameplay. */ +void +play_update(Play* _p) +{ + gui_update(&_p->gui, _p); + pause_update(_p); + + if (!_p->isPaused) + { + player_update(&_p->player, _p); + enemies_update(&_p->enemies, _p); + collectibles_update(&_p->collectibles, _p); + messages_update(_p); + + wave_update(_p); + spawn_update(_p); + + tutorial_update(_p); + } +} + +/* Draws gameplay. */ +void +play_draw(Play* _p) +{ + sprite_draw(&_p->bg, _p->g->renderer); + enemies_draw(&_p->enemies, _p); + collectibles_draw(&_p->collectibles, _p); + player_draw(&_p->player, _p); + messages_draw(_p); + tutorial_draw(_p); + gui_draw(&_p->gui, _p); + + if (_p->isPaused) + { + SDL_Rect r; + + r.x = 0; + r.y = 0; + r.w = PLAY_PAUSE_RECTANGLE_WIDTH; + r.h = PLAY_PAUSE_RECTANGLE_HEIGHT; + + rectangle_draw(&r, _p->g->renderer, PLAY_PAUSE_BACKGROUND_COLOR); + + sprite_draw(&_p->pauseText, _p->g->renderer); + } +} diff --git a/src/game/play/play.h b/src/game/play/play.h new file mode 100755 index 0000000..2c99bbe --- /dev/null +++ b/src/game/play/play.h @@ -0,0 +1,33 @@ +/* + * DESCRIPTION: + * Header file for play.h. +*/ + +#pragma once + +#include "collectible/collectibles.h" +#include "enemy/enemies.h" +#include "player/player.h" +#include "message/messages.h" +#include "pause/pause.h" +#include "gui/gui.h" +#include "spawn/spawn.h" +#include "tutorial/tutorial.h" +#include "wave/wave.h" + +#define PLAY_BG_OFFSET_X 0 +#define PLAY_BG_OFFSET_Y 0 +#define PLAY_BG_WIDTH 800 +#define PLAY_BG_HEIGHT 600 +#define PLAY_PAUSE_X_OFFSET 400 +#define PLAY_PAUSE_Y_OFFSET 300 +#define PLAY_PAUSE_RECTANGLE_WIDTH 800 +#define PLAY_PAUSE_RECTANGLE_HEIGHT 600 + +static const SDL_Color PLAY_PAUSE_TEXT_COLOR = {255, 255, 255, 255}; +static const SDL_Color PLAY_PAUSE_BACKGROUND_COLOR = {0, 0, 0, 64}; + +void play_draw(Play* _p); +void play_free(Play* _p); +void play_init(Play* _p, Game* _g); +void play_update(Play* _p); diff --git a/src/game/play/player/player.c b/src/game/play/player/player.c new file mode 100755 index 0000000..9f8f81c --- /dev/null +++ b/src/game/play/player/player.c @@ -0,0 +1,92 @@ +/* + * DESCRIPTION: + * Player code. +*/ + +#include "player.h" + +/* Initializes player. */ +void +player_init(Player* _p, Play* _l) +{ + memset(_p, '\0', sizeof(Player)); + + player_fatness_init(_p, _l); + player_upgrades_init(_p, _l); + player_price_init(_p, _l); + player_power_up_init(_p, _l); + player_fire_init(_p, _l); + player_shadow_init(_p, _l); + player_sprite_init(_p, _l); + player_tail_init(_p, _l); + player_snakes_init(_p, _l); + player_direction_init(_p, _l); + player_movement_init(_p, _l); + player_defeat_init(_p, _l); +} + +/* Frees player. */ +void +player_free(Player* _p, Play* _l) +{ + player_sprite_free(_p, _l); + player_snakes_free(_p, _l); +} + +/* Updates player. */ +void +player_update(Player* _p, Play* _l) +{ + player_movement_update(_p, _l); + + player_cash_update(_p, _l); + + player_power_up_update(_p, _l); + + player_upgrades_update(_p, _l); + + player_price_update(_p, _l); + + player_direction_update(_p, _l); + + player_sprite_update(_p, _l); + + player_tail_update(_p, _l); + + player_shadow_update(_p, _l); + + player_fatness_update(_p, _l); + + player_fire_update(_p, _l); + + player_snakes_update(_p, _l); + + player_defeat_update(_p, _l); +} + +/* Draws player. */ +void +player_draw(Player* _p, Play* _l) +{ + player_shadow_draw(_p, _l); + + + switch (_p->direction) + { + case DIRECTION_LEFT: + case DIRECTION_DOWN: + case DIRECTION_RIGHT: + player_sprite_draw(_p, _l); + player_tail_draw(_p, _l); + player_snakes_draw(_p, _l); + break; + case DIRECTION_UP: + player_snakes_draw(_p, _l); + player_sprite_draw(_p, _l); + player_tail_draw(_p, _l); + break; + default: + break; + } + +} diff --git a/src/game/play/player/player.h b/src/game/play/player/player.h new file mode 100755 index 0000000..381388a --- /dev/null +++ b/src/game/play/player/player.h @@ -0,0 +1,24 @@ +/* + * DESCRIPTION: + * Header file for player.c. +*/ + +#pragma once + +#include "player_cash.h" +#include "player_defeat.h" +#include "player_direction.h" +#include "player_fatness.h" +#include "player_fire.h" +#include "player_movement.h" +#include "player_power_up.h" +#include "player_price.h" +#include "player_shadow.h" +#include "player_sprite.h" +#include "player_tail.h" +#include "player_upgrades.h" + +void player_draw(Player* _p, Play* _l); +void player_free(Player* _p, Play* _l); +void player_init(Player* _p, Play* _l); +void player_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_cash.c b/src/game/play/player/player_cash.c new file mode 100755 index 0000000..760b6bd --- /dev/null +++ b/src/game/play/player/player_cash.c @@ -0,0 +1,41 @@ +/* + * DESCRIPTION: + * Player's cash. +*/ + +#include "player_cash.h" + +/* Adds some cash. */ +void +player_cash_acquire(Player* _p, s32 _c, Play* _l) +{ + f64 cash; + + if (_l->game > 1) + cash = _c * ((_l->game - 1) * PLAYER_CASH_GAME_MULTIPLIER); + else + cash = _c; + + _p->cash += (s32)cash; + _p->cashCollected += (s32)cash; + + message_cash_add(_p->sprite.x, _p->sprite.y, (s32)cash, _l); + + _p->isCashUpdate = TRUE; +} + +/* Initializes player cash. */ +void +player_cash_init(Player* _p, Play* _l) +{ + _p->cash = 0; + + _p->isCashUpdate = FALSE; +} + +/* Updates player cash. */ +void +player_cash_update(Player* _p, Play* _l) +{ + _p->cash = CEIL(_p->cash, PLAYER_CASH_MAX); +} diff --git a/src/game/play/player/player_cash.h b/src/game/play/player/player_cash.h new file mode 100755 index 0000000..746ff2e --- /dev/null +++ b/src/game/play/player/player_cash.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for player_cash.c. +*/ + +#include "../message/message.h" + +#define PLAYER_CASH_MAX 999999999 +#define PLAYER_CASH_GAME_MULTIPLIER 1.25 + +void player_cash_acquire(Player* _p, s32 _c, Play* _l); +void player_cash_init(Player* _p, Play* _l); +void player_cash_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_defeat.c b/src/game/play/player/player_defeat.c new file mode 100755 index 0000000..dc572d2 --- /dev/null +++ b/src/game/play/player/player_defeat.c @@ -0,0 +1,60 @@ +/* + * DESCRIPTION: + * Functions concering player defeat. +*/ + +#include "player_defeat.h" + +/* The player is defeated. */ +void +player_defeat(Player* _p, Play* _l) +{ + list_clear(&_p->snakes); + list_clear(&_l->collectibles); + + music_stop(); + + _p->isBeingDefeated = TRUE; + _p->defeatTimer = 0; + _p->isDefeat = TRUE; +} + +/* Start the defeat process. */ +void +player_defeat_begin(Player* _p, Play* _l) +{ + _p->isBeingDefeated = TRUE; + _p->defeatTimer = PLAYER_DEFEAT_TIMER_MAX; + + list_clear(&_p->snakes); + list_clear(&_l->collectibles); + + music_stop(); + + _l->isBountyUpdate = TRUE; +} + +/* Initializes player defeat. */ +void +player_defeat_init(Player* _p, Play* _l) +{ + _p->isDefeat = FALSE; + _p->isBeingDefeated = FALSE; + _p->defeatTimer = 0; +} + +/* Updates the player defeat. */ +void +player_defeat_update(Player* _p, Play* _l) +{ + if (_p->isBeingDefeated) + { + _p->defeatTimer--; + + _p->isAbleToFire = FALSE; + _p->isAbleToMove = FALSE; + + if (_p->defeatTimer <= 0) + player_defeat(_p, _l); + } +} diff --git a/src/game/play/player/player_defeat.h b/src/game/play/player/player_defeat.h new file mode 100755 index 0000000..59f0593 --- /dev/null +++ b/src/game/play/player/player_defeat.h @@ -0,0 +1,15 @@ +/* + * DESCRIPTION: + * Header file for player_defeat.h. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" + +#define PLAYER_DEFEAT_TIMER_MAX 300 + +void player_defeat_begin(Player* _p, Play* _l); +void player_defeat(Player* _p, Play* _l); +void player_defeat_init(Player* _p, Play* _l); +void player_defeat_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_direction.c b/src/game/play/player/player_direction.c new file mode 100755 index 0000000..f36a61a --- /dev/null +++ b/src/game/play/player/player_direction.c @@ -0,0 +1,63 @@ +/* + * DESCRIPTION: + * The player's facing direction. +*/ + +#include "player_direction.h" + +/* Initializes player direction. */ +void +player_direction_init(Player* _p, Play* _l) +{ + _p->direction = DIRECTION_RIGHT; +} + +/* Sets player direction. */ +void +player_direction_set(Player* _p, Play* _l) +{ + f64 angle; + + angle = math_atan + ( + _p->shadow.x, + _p->shadow.y, + _l->gui.cursor.sprite.x, + _l->gui.cursor.sprite.y + ); + + angle *= RAD_TO_DEG; + + if + ( + angle > PLAYER_DIRECTION_DOWN_THRESHOLD && + angle <= PLAYER_DIRECTION_LEFT_THRESHOLD + ) + _p->direction = DIRECTION_LEFT; + else if + ( + angle > PLAYER_DIRECTION_LEFT_THRESHOLD && + angle <= PLAYER_DIRECTION_UP_THRESHOLD + ) + _p->direction = DIRECTION_UP; + else if + ( + angle > PLAYER_DIRECTION_RIGHT_THRESHOLD && + angle <= PLAYER_DIRECTION_DOWN_THRESHOLD + ) + _p->direction = DIRECTION_DOWN; + else + _p->direction = DIRECTION_RIGHT; +} + +/* Updates player direction. */ +void +player_direction_update(Player* _p, Play* _l) +{ + if + ( + _p->isAbleToMove && + !_p->isKeyboardMove + ) + player_direction_set(_p, _l); +} diff --git a/src/game/play/player/player_direction.h b/src/game/play/player/player_direction.h new file mode 100755 index 0000000..66a2251 --- /dev/null +++ b/src/game/play/player/player_direction.h @@ -0,0 +1,17 @@ +/* + * DESCRIPTION: + * Header file for player_direction.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" + +#define PLAYER_DIRECTION_LEFT_THRESHOLD 45 +#define PLAYER_DIRECTION_UP_THRESHOLD 135 +#define PLAYER_DIRECTION_RIGHT_THRESHOLD -135 +#define PLAYER_DIRECTION_DOWN_THRESHOLD -45 + +void player_direction_init(Player* _p, Play* _l); +void player_direction_update(Player* _p, Play* _l); +void player_direction_set(Player* _p, Play* _l); diff --git a/src/game/play/player/player_fatness.c b/src/game/play/player/player_fatness.c new file mode 100755 index 0000000..fc20e33 --- /dev/null +++ b/src/game/play/player/player_fatness.c @@ -0,0 +1,104 @@ +/* + * DESCRIPTION: + * Functions concering player fatness. +*/ + +#include "player_fatness.h" + +static void player_fatness_swallow(Player* _p, Play* _l); +static void player_fatness_gain(Player* _p, Play* _l); + +/* When you hit an enemy, you 'swallow" them, and this happens. */ +static void +player_fatness_swallow(Player* _p, Play* _l) +{ + enemies_defeat(&_l->enemies, _l); + list_clear(&_l->spawnEnemies); + + _l->isWaveFailed = TRUE; + + _p->isAbleToMove = FALSE; + _p->isAbleToFire = FALSE; + + _p->dX = 0; + _p->dY = 0; + + _p->isBeingFattened = TRUE; + _p->fatnessTimer = PLAYER_FATNESS_TIMER_MAX; + + _p->isFatnessUpdate = TRUE; + + _l->enemyMax = 0; + _l->isBountyUpdate = TRUE; + + _p->isStationary = TRUE; + + sound_play(_l->g->sounds[SOUND_SWALLOW], -1); + + if (_p->fatness + 1 > PLAYER_FATNESS_THRESHOLD) + player_defeat(_p, _l); +} + +/* You gain fatness. */ +static void +player_fatness_gain(Player* _p, Play* _l) +{ + _p->isAbleToMove = TRUE; + _p->isAbleToFire = TRUE; + + _p->fatness++; + _p->isBeingFattened = FALSE; + _p->isFatnessUpdate = TRUE; + + sound_play(_l->g->sounds[SOUND_GROWTH], -1); +} + +/* Initializes player fatness. */ +void +player_fatness_init(Player* _p, Play* _l) +{ + _p->fatness = PLAYER_FATNESS_DEFAULT; +} + +/* Updates the player fatness. */ +void +player_fatness_update(Player* _p, Play* _l) +{ + Enemy** enemies; + s32 enemyCount; + + if (_p->isFatnessUpdate) + _p->isFatnessUpdate = FALSE; + + enemies = (Enemy**)list_data_get(&_l->enemies); + enemyCount = _l->enemies.count; + + for (s32 i = 0; i < enemyCount; i++) + { + Enemy* e; + + e = enemies[i]; + + if + ( + circle_collide(&_p->shadow, &e->shadow) && + e->isActive + ) + { + player_fatness_swallow(_p, _l); + break; + } + } + + if (_p->isBeingFattened) + { + _p->fatnessTimer--; + + if (_p->fatnessTimer <= 0) + player_fatness_gain(_p, _l); + } + + free(enemies); + + _p->fatness = CLAMP(_p->fatness, 0, PLAYER_FATNESS_MAX - 1); +} diff --git a/src/game/play/player/player_fatness.h b/src/game/play/player/player_fatness.h new file mode 100755 index 0000000..3353df8 --- /dev/null +++ b/src/game/play/player/player_fatness.h @@ -0,0 +1,15 @@ +/* + * DESCRIPTION: + * Header file for player_fatness.h. +*/ + +#pragma once + +#include "../enemy/enemies.h" +#include "player_defeat.h" + +#define PLAYER_FATNESS_DEFAULT 0 +#define PLAYER_FATNESS_TIMER_MAX 60 + +void player_fatness_init(Player* _p, Play* _l); +void player_fatness_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_fire.c b/src/game/play/player/player_fire.c new file mode 100755 index 0000000..9fc1944 --- /dev/null +++ b/src/game/play/player/player_fire.c @@ -0,0 +1,165 @@ +/* + * DESCRIPTION: + * Player firing code. +*/ + +#include "player_fire.h" + +static void player_fire(Player* _p, Play* _l); +static void player_redirect(Player* _p, Play* _l); + +/* Fires a snake. */ +static void +player_fire(Player* _p, Play* _l) +{ + player_snake_add(_p, _l); + + sound_play(_l->g->sounds[SOUND_SNAKE_FIRE], -1); + + _p->isSnakeUpdate = TRUE; +} + +/* Redirects all snakes. The angle of the snakes must be significant enough to do so. */ +static void +player_redirect(Player* _p, Play* _l) +{ + Snake** snakes; + Snake** snakesAvailable; + s32 snakesAvailableCount; + + snakes = (Snake**)list_data_get(&_p->snakes); + + snakesAvailableCount = 0; + snakesAvailable = NULL; + + for (s32 i = 0; i < _p->snakes.count; i++) + { + f64 playerAngle; + f64 snakeAngle; + f64 angleDifference; + Snake* s; + + s = snakes[i]; + + playerAngle = math_atan + ( + _l->player.shadow.x, + _l->player.shadow.y, + _l->g->mouse.x, + _l->g->mouse.y + ); + + snakeAngle = math_atan + ( + _l->player.shadow.x, + _l->player.shadow.y, + s->sprite.x, + s->sprite.y + ); + + playerAngle *= RAD_TO_DEG; + snakeAngle *= RAD_TO_DEG; + + angleDifference = fabs(playerAngle - snakeAngle); + + if + ( + angleDifference > PLAYER_SNAKE_REDIRECT_ANGLE_DIFFERENCE_MIN && + !s->isReturning + ) + { + snakesAvailableCount++; + + snakesAvailable = realloc(snakesAvailable, sizeof(Snake*) * snakesAvailableCount); + + snakesAvailable[snakesAvailableCount - 1] = s; + } + } + + if (snakesAvailableCount > 0) + _p->redirects--; + + for (s32 i = 0; i < snakesAvailableCount; i++) + { + Snake* s; + + s = snakesAvailable[i]; + + s->redirectCount++; + + s->redirectCount = CEIL(s->redirectCount, PLAYER_REDIRECT_MAX); + + s->redirectTimer = PLAYER_REDIRECT_TIMER_MAX; + s->sprite.frame = SNAKE_REDIRECT; + + s->damage = PLAYER_SNAKE_DAMAGE_DEFAULT + pow(s->redirectCount, 2); + + player_snake_reset(s, _p, _l); + } + + free(snakes); + + _p->isRedirectUpdate = TRUE; + + if (snakesAvailableCount > 0) + sound_play(_l->g->sounds[SOUND_SNAKE_REDIRECT], 7); +} + +/* Initializes player firing. */ +void +player_fire_init(Player* _p, Play* _l) +{ + _p->isAbleToFire = TRUE; + _p->snakeReserve = _p->snakeMax; + _p->redirects = _p->redirectMax; + + _p->redirectRechargeTimer = PLAYER_REDIRECT_RECHARGE_TIMER_MAX; +} + +/* Updates the player's firing. */ +void +player_fire_update(Player* _p, Play* _l) +{ + if (_p->isSnakeUpdate) + _p->isSnakeUpdate = FALSE; + + if (_p->isRedirectUpdate) + _p->isRedirectUpdate = FALSE; + + if (_p->redirectTimer > 0) + _p->redirectTimer--; + + + if (_p->snakeReserve > 0) + { + if (_p->isAbleToFire) + { + if (mouse_press(&_l->g->mouse, MOUSE_LEFT)) + { + player_fire(_p, _l); + _p->snakeReserve--; + } + } + } + if + ( + _p->snakes.count > 0 && + _p->redirects > 0 && + _p->redirectTimer <= 0 + ) + if + ( + mouse_press(&_l->g->mouse, MOUSE_RIGHT) || + keyboard_press(&_l->g->keyboard, KEYBOARD_R) + ) + { + player_redirect(_p, _l); + _p->redirectTimer = PLAYER_REDIRECT_TIMER_MAX; + } + + if (_p->snakes.count == 0) + { + _p->redirects = _p->redirectMax; + _p->isRedirectUpdate = TRUE; + } +} diff --git a/src/game/play/player/player_fire.h b/src/game/play/player/player_fire.h new file mode 100755 index 0000000..42f38b6 --- /dev/null +++ b/src/game/play/player/player_fire.h @@ -0,0 +1,16 @@ +/* + * DESCRIPTION: + * Header file for player_fire.c. +*/ + +#pragma once + +#include "player_snakes.h" +#include "player_direction.h" + +#define PLAYER_REDIRECT_MAX 99 +#define PLAYER_REDIRECT_TIMER_MAX 10 +#define PLAYER_REDIRECT_RECHARGE_TIMER_MAX 60 + +void player_fire_init(Player* _p, Play* _l); +void player_fire_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_movement.c b/src/game/play/player/player_movement.c new file mode 100755 index 0000000..3ecd272 --- /dev/null +++ b/src/game/play/player/player_movement.c @@ -0,0 +1,190 @@ +/* + * DESCRIPTION: + * Player movement code. +*/ + +#include "player_movement.h" + +static void player_velocity_reset(Player* _p); +static bool_u8 player_movement_left_held(Play* _p); +static bool_u8 player_movement_right_held(Play* _p); +static bool_u8 player_movement_up_held(Play* _p); +static bool_u8 player_movement_down_held(Play* _p); + +static bool_u8 +player_movement_left_held(Play* _p) +{ + if + ( + keyboard_held(&_p->g->keyboard, KEYBOARD_LEFT) || + keyboard_held(&_p->g->keyboard, KEYBOARD_A) + ) + return TRUE; + return FALSE; +} + +static bool_u8 +player_movement_right_held(Play* _p) +{ + if + ( + keyboard_held(&_p->g->keyboard, KEYBOARD_RIGHT) || + keyboard_held(&_p->g->keyboard, KEYBOARD_D) + ) + return TRUE; + return FALSE; +} + +static bool_u8 +player_movement_up_held(Play* _p) +{ + if + ( + keyboard_held(&_p->g->keyboard, KEYBOARD_UP) || + keyboard_held(&_p->g->keyboard, KEYBOARD_W) + ) + return TRUE; + return FALSE; +} + +static bool_u8 +player_movement_down_held(Play* _p) +{ + if + ( + keyboard_held(&_p->g->keyboard, KEYBOARD_DOWN) || + keyboard_held(&_p->g->keyboard, KEYBOARD_S) + ) + return TRUE; + return FALSE; +} + +static void +player_velocity_reset(Player* _p) +{ + _p->dX = 0; + _p->dY = 0; + _p->isStationary = TRUE; +} + +void +player_movement_init(Player* _p, Play* _l) +{ + _p->isAbleToMove = TRUE; + _p->isKeyboardMove = TRUE;; +} + +/* Updates player movement. */ +void +player_movement_update(Player* _p, Play* _l) +{ + if (_p->isStationary) + _p->isStationary = FALSE; + + if (_p->isAbleToMove && !_l->isGameCleared) + { + f64 angle; + f64 velocity; + + velocity = PLAYER_VELOCITIES[_p->fatness]; + + if (_p->isKeyboardMove) + { + bool_u8 isMove; + + _p->isStationary = TRUE; + isMove = FALSE; + + player_velocity_reset(_p); + + if (player_movement_left_held(_l)) + { + angle = PLAYER_MOVEMENT_LEFT_ANGLE; + _p->direction = DIRECTION_LEFT; + + if (player_movement_up_held(_l)) + angle = PLAYER_MOVEMENT_LEFT_UP_ANGLE; + else if (player_movement_down_held(_l)) + angle = PLAYER_MOVEMENT_LEFT_DOWN_ANGLE; + + isMove = TRUE; + } + else if (player_movement_right_held(_l)) + { + angle = PLAYER_MOVEMENT_RIGHT_ANGLE; + _p->direction = DIRECTION_RIGHT; + + if (player_movement_up_held(_l)) + angle = PLAYER_MOVEMENT_RIGHT_UP_ANGLE; + else if (player_movement_down_held(_l)) + angle = PLAYER_MOVEMENT_RIGHT_DOWN_ANGLE; + + isMove = TRUE; + } + else if (player_movement_up_held(_l)) + { + angle = PLAYER_MOVEMENT_UP_ANGLE; + //_p->direction = DIRECTION_UP; + isMove = TRUE; + } + else if (player_movement_down_held(_l)) + { + angle = PLAYER_MOVEMENT_DOWN_ANGLE; + //_p->direction = DIRECTION_DOWN; + isMove = TRUE; + } + + _p->dX = -(cos(angle) * velocity); + _p->dY = -(sin(angle) * velocity); + + if (isMove) + _p->isStationary = FALSE; + else + player_velocity_reset(_p); + + } + else + { + if (!mouse_held(&_l->g->mouse, MOUSE_RIGHT)) + player_velocity_reset(_p); + else + { + f64 distance; + + distance = math_distance + ( + _p->sprite.x, + _p->sprite.y, + _l->g->mouse.x, + _l->g->mouse.y + ); + + if (distance > PLAYER_MOVE_MINIMUM) + { + angle = math_atan + ( + _p->sprite.x, + _p->sprite.y, + _l->g->mouse.x, + _l->g->mouse.y + ); + + _p->dX = -(cos(angle) * velocity); + _p->dY = -(sin(angle) * velocity); + + _p->isStationary = FALSE; + } + else + player_velocity_reset(_p); + } + } + } + + _p->sprite.x += _p->dX; + _p->sprite.y += _p->dY; + + _p->sprite.x = _p->sprite.x < PLAYER_MOVEMENT_X_MIN ? PLAYER_MOVEMENT_X_MIN : _p->sprite.x; + _p->sprite.x = _p->sprite.x > PLAYER_MOVEMENT_X_MAX ? PLAYER_MOVEMENT_X_MAX : _p->sprite.x; + _p->sprite.y = _p->sprite.y < PLAYER_MOVEMENT_Y_MIN ? PLAYER_MOVEMENT_Y_MIN : _p->sprite.y; + _p->sprite.y = _p->sprite.y > PLAYER_MOVEMENT_Y_MAX ? PLAYER_MOVEMENT_Y_MAX : _p->sprite.y; +} diff --git a/src/game/play/player/player_movement.h b/src/game/play/player/player_movement.h new file mode 100755 index 0000000..9fd3c4a --- /dev/null +++ b/src/game/play/player/player_movement.h @@ -0,0 +1,30 @@ +#pragma once + +#include "../../GAME_COMMON.h" + +#define PLAYER_MOVE_MINIMUM 32 + +#define PLAYER_MOVEMENT_LEFT_ANGLE 0 +#define PLAYER_MOVEMENT_RIGHT_ANGLE (PI) +#define PLAYER_MOVEMENT_UP_ANGLE (PI / 2) +#define PLAYER_MOVEMENT_DOWN_ANGLE (PI + (PI / 2)) +#define PLAYER_MOVEMENT_LEFT_UP_ANGLE PLAYER_MOVEMENT_LEFT_ANGLE + (PI / 4) +#define PLAYER_MOVEMENT_LEFT_DOWN_ANGLE PLAYER_MOVEMENT_LEFT_ANGLE - (PI / 4) +#define PLAYER_MOVEMENT_RIGHT_UP_ANGLE PLAYER_MOVEMENT_RIGHT_ANGLE - (PI / 4) +#define PLAYER_MOVEMENT_RIGHT_DOWN_ANGLE PLAYER_MOVEMENT_RIGHT_ANGLE + (PI / 4) +#define PLAYER_MOVEMENT_X_MIN -10 +#define PLAYER_MOVEMENT_X_MAX 810 +#define PLAYER_MOVEMENT_Y_MIN -10 +#define PLAYER_MOVEMENT_Y_MAX 610 + +#include "player_direction.h" + +static const s32 PLAYER_VELOCITIES[PLAYER_FATNESS_MAX] = +{ + 5, + 4, + 3 +}; + +void player_movement_init(Player* _p, Play* _l); +void player_movement_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_power_up.c b/src/game/play/player/player_power_up.c new file mode 100755 index 0000000..0801cf1 --- /dev/null +++ b/src/game/play/player/player_power_up.c @@ -0,0 +1,42 @@ +/* + * DESCRIPTION: + * Player power up behavior. Infinite snakes!! +*/ + +#include "player_power_up.h" + +/* No more power up. */ +void +player_power_up_end(Player* _p, Play* _l) +{ + _p->powerUpTimer = 0; + _p->isPowerUp = FALSE; + + player_snakes_clear(_p, _l); + _p->snakeReserve = _p->snakeMax; + + message_power_down_add(_p->sprite.x, _p->sprite.y, _l); +} + +/* Initializes player power_up. */ +void +player_power_up_init(Player* _p, Play* _l) +{ + _p->powerUpTimer = 0; + _p->isPowerUp = 0; +} + +/* Updates player power_up. */ +void +player_power_up_update(Player* _p, Play* _l) +{ + if (_p->isPowerUp) + { + _p->powerUpTimer--; + + _p->snakeReserve = _p->snakeMax; + + if (_p->powerUpTimer <= 0) + player_power_up_end(_p, _l); + } +} diff --git a/src/game/play/player/player_power_up.h b/src/game/play/player/player_power_up.h new file mode 100755 index 0000000..4422f54 --- /dev/null +++ b/src/game/play/player/player_power_up.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for player_power_up.c. +*/ + +#pragma once + +#include "../message/message.h" +#include "player_snakes.h" + +void player_power_up_end(Player* _p, Play* _l); +void player_power_up_init(Player* _p, Play* _l); +void player_power_up_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_price.c b/src/game/play/player/player_price.c new file mode 100755 index 0000000..235ab21 --- /dev/null +++ b/src/game/play/player/player_price.c @@ -0,0 +1,89 @@ +/* + * DESCRIPTION: + * Determines prices for player upgrades. +*/ + +#include "player_price.h" + +static s32 player_price_snake_get(Player* _p, Play* _l); +static s32 player_price_redirect_get(Player* _p, Play* _l); +static s32 player_price_lifetime_get(Player* _p, Play* _l); + +/* Gets the snake upgrade price. */ +static s32 +player_price_snake_get(Player* _p, Play* _l) +{ + s32 price; + + price = (s32) + ( + pow + ( + PLAYER_PRICE_BASE, + PLAYER_PRICE_EXPONENT_BASE + ((_p->snakeUpgradeLevel - 1) * PLAYER_PRICE_EXPONENT_INCREMENT) + ) + ); + + return price; +} + +/* Gets the lifetime upgrade price. */ +static s32 +player_price_lifetime_get(Player* _p, Play* _l) +{ + s32 price; + + price = (s32) + ( + pow + ( + PLAYER_PRICE_BASE, + PLAYER_PRICE_EXPONENT_BASE + ((_p->lifetimeUpgradeLevel - 1) * PLAYER_PRICE_EXPONENT_INCREMENT) + ) + ); + + return price; +} + +/* Gets the redirect upgrade price. */ +static s32 +player_price_redirect_get(Player* _p, Play* _l) +{ + s32 price; + + price = (s32) + ( + pow + ( + PLAYER_PRICE_BASE, + PLAYER_PRICE_EXPONENT_BASE + ((_p->redirectUpgradeLevel - 1) * PLAYER_PRICE_EXPONENT_INCREMENT) + ) + ); + + return price; +} + +/* Initializes player prices. */ +void +player_price_init(Player* _p, Play* _l) +{ + _p->snakeUpgradePrice = player_price_snake_get(_p, _l); + _p->lifetimeUpgradePrice = player_price_lifetime_get(_p, _l); + _p->redirectUpgradePrice = player_price_redirect_get(_p, _l); +} + +/* Updates player prices. */ +void +player_price_update(Player* _p, Play* _l) +{ + if (_p->isSnakeUpgradeUpdate) + _p->snakeUpgradePrice = player_price_snake_get(_p, _l); + + if (_p->isLifetimeUpgradeUpdate) + _p->lifetimeUpgradePrice = player_price_lifetime_get(_p, _l); + + if (_p->isRedirectUpgradeUpdate) + _p->redirectUpgradePrice = player_price_redirect_get(_p, _l); + + +} diff --git a/src/game/play/player/player_price.h b/src/game/play/player/player_price.h new file mode 100755 index 0000000..1db9eb3 --- /dev/null +++ b/src/game/play/player/player_price.h @@ -0,0 +1,15 @@ +/* + * DESCRIPTION: + * Header file for player_price.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" + +#define PLAYER_PRICE_BASE 250 +#define PLAYER_PRICE_EXPONENT_INCREMENT 0.1 +#define PLAYER_PRICE_EXPONENT_BASE 1.0 + +void player_price_init(Player* _p, Play* _l); +void player_price_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_shadow.c b/src/game/play/player/player_shadow.c new file mode 100755 index 0000000..bf0c368 --- /dev/null +++ b/src/game/play/player/player_shadow.c @@ -0,0 +1,34 @@ +/* + * DESCRIPTION: + * Functions concering player shadow. +*/ + +#include "player_shadow.h" + +/* Initializes player shadow. */ +void +player_shadow_init(Player* _p, Play* _l) +{ + _p->shadow.r = PLAYER_SHADOW_RADIUS[_p->fatness]; + + _p->shadow.x = _p->sprite.x + PLAYER_SHADOW_OFFSET_X; + _p->shadow.y = _p->sprite.y + PLAYER_SHADOW_OFFSET_Y; +} + +/* Updates the player shadow. */ +void +player_shadow_update(Player* _p, Play* _l) +{ + if (_p->isFatnessUpdate) + _p->shadow.r = PLAYER_SHADOW_RADIUS[_p->fatness]; + + _p->shadow.x = _p->sprite.x + PLAYER_SHADOW_OFFSET_X; + _p->shadow.y = _p->sprite.y + PLAYER_SHADOW_OFFSET_Y; +} + +/* Draws the player shadow. */ +void +player_shadow_draw(Player* _p, Play* _l) +{ + shadow_draw(&_p->shadow, _l); +} diff --git a/src/game/play/player/player_shadow.h b/src/game/play/player/player_shadow.h new file mode 100755 index 0000000..146ae5b --- /dev/null +++ b/src/game/play/player/player_shadow.h @@ -0,0 +1,22 @@ +/* + * DESCRIPTION: + * Header file for player_shadow.h. +*/ + +#pragma once + +#include "../shadow/shadow.h" + +#define PLAYER_SHADOW_OFFSET_X 0 +#define PLAYER_SHADOW_OFFSET_Y 32 + +static const s32 PLAYER_SHADOW_RADIUS[PLAYER_FATNESS_MAX] = +{ + 16, + 24, + 32 +}; + +void player_shadow_init(Player* _p, Play* _l); +void player_shadow_update(Player* _p, Play* _l); +void player_shadow_draw(Player* _p, Play* _l); diff --git a/src/game/play/player/player_snakes.c b/src/game/play/player/player_snakes.c new file mode 100755 index 0000000..ce0bc59 --- /dev/null +++ b/src/game/play/player/player_snakes.c @@ -0,0 +1,401 @@ +/* + * DESCRIPTION: + * Player snake code. +*/ + +#include "player_snakes.h" + +static Sprite snakeSprite; +static Sprite arcSprite; + +static void player_snake_remove(Snake* _s, Player* _p, Play* _l); +static void player_snake_update(Snake* _s, Player* _p, Play* _l); +static void player_snake_draw(Snake* _s, Player* _p, Play* _l); + +/* Removes a snake. */ +static void +player_snake_remove(Snake* _s, Player* _p, Play* _l) +{ + Enemy** enemies; + Collectible** collectibles; + s32 enemyCount; + s32 collectibleCount; + + enemies = (Enemy**)list_data_get(&_l->enemies); + enemyCount = _l->enemies.count; + + /* + * If you're looking for it, this is the most inefficent code in this entire thing. + * And that's saying a lot! + */ + for (s32 i = 0; i < enemyCount; i++) + { + Enemy* e; + + e = enemies[i]; + + list_node_from_data_remove(&e->snakesHit, &_s, sizeof(Snake*)); + list_node_from_data_remove(&e->returnSnakesHit, &_s, sizeof(Snake*)); + } + + free(enemies); + + collectibles = (Collectible**)list_data_get(&_l->collectibles); + collectibleCount = _l->collectibles.count; + + for (s32 i = 0; i < collectibleCount; i++) + { + Collectible* c; + + c = collectibles[i]; + + if (c->attachedSnake == _s) + { + c->attachedSnake = NULL; + c->isAttached = FALSE; + collectible_collect(c, &_l->collectibles, _l); + } + } + + free(collectibles); + + list_node_from_data_remove(&_p->snakes, _s, sizeof(Snake)); + + _p->snakeReserve++; + + _p->snakeReserve = CEIL(_p->snakeReserve, _p->snakeMax); + + _p->isSnakeUpdate = TRUE; + + if (_p->redirects < _p->redirectMax) + { + _p->redirects++; + _p->isRedirectUpdate = TRUE; + } +} + +/* Updates a given snake. */ +static void +player_snake_update(Snake* _s, Player* _p, Play* _l) +{ + Collectible** collectibles; + Enemy** enemies; + s32 enemyCount; + s32 collectibleCount; + + _s->sprite.x += _s->dX; + _s->sprite.y += _s->dY; + + _s->shadow.x = _s->sprite.x + PLAYER_SNAKE_SHADOW_OFFSET_X; + _s->shadow.y = _s->sprite.y + PLAYER_SNAKE_SHADOW_OFFSET_Y; + + _s->animationTimer--; + + enemies = (Enemy**)list_data_get(&_l->enemies); + enemyCount = _l->enemies.count; + + if (_s->animationTimer <= 0) + { + if (_s->sprite.flip == FLIP_VERTICAL) + _s->sprite.flip = FLIP_NONE; + else + _s->sprite.flip = FLIP_VERTICAL; + + _s->animationTimer = PLAYER_SNAKE_ANIMATION_TIMER_MAX; + } + + for (s32 i = 0; i < enemyCount; i++) + { + Enemy* e; + + e = enemies[i]; + + if + ( + circle_collide(&_s->shadow, &e->shadow) && + e->isActive + ) + { + Node* returnSnake; + Node* snake; + + snake = list_node_from_data_get(&e->snakesHit, &_s, sizeof(Snake*)); + returnSnake = list_node_from_data_get(&e->returnSnakesHit, &_s, sizeof(Snake*)); + + if + ( + _s->isReturning && + !returnSnake + + ) + { + list_append_new(&e->returnSnakesHit, &_s, sizeof(Snake*)); + enemy_damage(e, _s->damage, &_l->enemies, _l); + } + else if + ( + !_s->isReturning && + !snake + ) + { + list_append_new(&e->snakesHit, &_s, sizeof(Snake*)); + enemy_damage(e, _s->damage, &_l->enemies, _l); + } + } + } + + free(enemies); + + collectibles = (Collectible**)list_data_get(&_l->collectibles); + collectibleCount = _l->collectibles.count; + + for (s32 i = 0; i < collectibleCount; i++) + { + Collectible* c; + + c = collectibles[i]; + + if + ( + circle_collide(&_s->shadow, &c->shadow) && + c->isActive + ) + { + c->attachedSnake = _s; + c->isAttached = TRUE; + } + } + + free(collectibles); + + if (_s->isReturning) + { + f64 angle; + + angle = math_atan + ( + _p->shadow.x, + _p->shadow.y, + _s->shadow.x, + _s->shadow.y + ); + + _s->sprite.rotation = RAD_TO_DEG * (angle + PI); + + _s->returnTimer++; + _s->returnTimer = CEIL(_s->returnTimer, PLAYER_SNAKE_RETURN_TIMER_MAX); + + _s->dX = cos(angle) * (PLAYER_SNAKE_VELOCITY * (_s->returnTimer * PLAYER_SNAKE_RETURN_VELOCITY_MULTIPLIER)); + _s->dY = sin(angle) * (PLAYER_SNAKE_VELOCITY * (_s->returnTimer * PLAYER_SNAKE_RETURN_VELOCITY_MULTIPLIER)); + + if (circle_collide(&_p->shadow, &_s->shadow)) + player_snake_remove(_s, _p, _l); + + } + else + { + _s->lifetime--; + _s->redirectTimer--; + + if (_s->redirectTimer <= 0) + _s->sprite.frame = SNAKE_NONE; + + if (_s->lifetime <= 0) + { + _s->dX = 0; + _s->dY = 0; + _s->isReturning = TRUE; + _s->sprite.frame = SNAKE_RETURN; + + sound_play(_l->g->sounds[SOUND_SNAKE_RETURN], 7); + } + + } +} + +/* Draws a given snake. */ +static void +player_snake_draw(Snake* _s, Player* _p, Play* _l) +{ + if (!_s->isReturning) + { + f64 angle; + + angle = _s->sprite.rotation; + _s->arc.rotation = angle; + + _s->arc.x = _s->sprite.x - (cos(DEG_TO_RAD * angle) * PLAYER_SNAKE_ARC_OFFSET); + _s->arc.y = _s->sprite.y - (sin(DEG_TO_RAD * angle) * PLAYER_SNAKE_ARC_OFFSET); + + sprite_draw(&_s->arc, _l->g->renderer); + } + + shadow_draw(&_s->shadow, _l); + sprite_draw(&_s->sprite, _l->g->renderer); +} + +/* Adds a snake. */ +void +player_snake_add(Player* _p, Play* _l) +{ + Snake snake; + f64 angle; + + memset(&snake, '\0', sizeof(Snake)); + + snake.sprite = snakeSprite; + snake.arc = arcSprite; + + angle = math_atan + ( + _p->sprite.x, + _p->sprite.y, + _l->g->mouse.x, + _l->g->mouse.y + ); + + snake.shadow.r = PLAYER_SNAKE_SHADOW_RADIUS; + snake.sprite.x = _p->sprite.x - (cos(angle) * PLAYER_SNAKE_OFFSET); + snake.sprite.y = _p->sprite.y - (sin(angle) * PLAYER_SNAKE_OFFSET); + + snake.damage = PLAYER_SNAKE_DAMAGE_DEFAULT; + snake.redirectCount = 0; + snake.isReturning = FALSE; + snake.returnTimer = 0; + snake.animationTimer = PLAYER_SNAKE_ANIMATION_TIMER_MAX; + snake.sprite.rotation = RAD_TO_DEG * angle; + snake.dX = -cos(angle) * PLAYER_SNAKE_VELOCITY; + snake.dY = -sin(angle) * PLAYER_SNAKE_VELOCITY; + snake.lifetime = _p->lifetime; + + list_append_new(&_p->snakes, (void*)&snake, sizeof(Snake)); +} + +/* Resets the snake to a new angle/velocity (redirect). */ +void +player_snake_reset(Snake* _s, Player* _p, Play* _l) +{ + f64 angle; + + angle = math_atan + ( + _s->shadow.x, + _s->shadow.y, + _l->g->mouse.x, + _l->g->mouse.y + ); + + _s->animationTimer = PLAYER_SNAKE_ANIMATION_TIMER_MAX; + _s->sprite.rotation = RAD_TO_DEG * angle; + _s->dX = -cos(angle) * (PLAYER_SNAKE_VELOCITY + (_s->redirectCount * PLAYER_SNAKE_REDIRECT_VELOCITY_MULTIPLIER)); + _s->dY = -sin(angle) * (PLAYER_SNAKE_VELOCITY + (_s->redirectCount * PLAYER_SNAKE_REDIRECT_VELOCITY_MULTIPLIER)); + + _s->lifetime = (s32)(_p->lifetime * PLAYER_SNAKE_REDIRECT_LIFETIME_MULTIPLIER); +} + +/* Initializes player snakes. */ +void +player_snakes_init(Player* _p, Play* _l) +{ + Atlas atlas; + + atlas_init + ( + &atlas, + _l->g->textures[TEXTURE_PLAYER_SNAKE], + PLAYER_SNAKE_ROWS, + PLAYER_SNAKE_COLUMNS, + PLAYER_SNAKE_FRAME_WIDTH, + PLAYER_SNAKE_FRAME_HEIGHT + ); + + list_init(&_p->snakes); + + sprite_atlas_init + ( + &snakeSprite, + &atlas, + ORIGIN_MIDDLE, + PLAYER_SNAKE_WIDTH, + PLAYER_SNAKE_HEIGHT + ); + + snakeSprite.frame = SNAKE_NONE; + + sprite_init + ( + &arcSprite, + _l->g->textures[TEXTURE_ARC], + ORIGIN_MIDDLE, + PLAYER_SNAKE_ARC_WIDTH, + PLAYER_SNAKE_ARC_HEIGHT + ); +} + +/* Clears all player snakes. */ +void +player_snakes_clear(Player* _p, Play* _l) +{ + Snake** snakes; + s32 snakeCount; + + snakes = (Snake**)list_data_get(&_p->snakes); + snakeCount = _p->snakes.count; + + for (s32 i = 0; i < snakeCount; i++) + { + Snake* s; + + s = snakes[i]; + + player_snake_remove(s, _p, _l); + } +} + +/* Frees snake list. */ +void +player_snakes_free(Player* _p, Play* _l) +{ + list_free(&_p->snakes); +} + +/* Updates all player snakes. */ +void +player_snakes_update(Player* _p, Play* _l) +{ + Snake** snakes; + + snakes = (Snake**)list_data_get(&_p->snakes); + + for (s32 i = 0; i < _p->snakes.count; i++) + { + Snake* s; + + s = snakes[i]; + + player_snake_update(s, _p, _l); + } + + free(snakes); +} + +/* Draws all player snakes. */ +void +player_snakes_draw(Player* _p, Play* _l) +{ + Snake** snakes; + + snakes = (Snake**)list_data_get(&_p->snakes); + + for (s32 i = 0; i < _p->snakes.count; i++) + { + Snake* s; + + s = snakes[i]; + + player_snake_draw(s, _p, _l); + } + + free(snakes); +} + diff --git a/src/game/play/player/player_snakes.h b/src/game/play/player/player_snakes.h new file mode 100755 index 0000000..e1c1c93 --- /dev/null +++ b/src/game/play/player/player_snakes.h @@ -0,0 +1,41 @@ +/* + * DESCRIPTION: + * Header file for player_snakes.h. +*/ + +#pragma once + +#include "../enemy/enemy.h" + +#define PLAYER_SNAKE_ANIMATION_TIMER_MAX 5 +#define PLAYER_SNAKE_SHADOW_OFFSET_X 0 +#define PLAYER_SNAKE_SHADOW_OFFSET_Y 32 +#define PLAYER_SNAKE_SHADOW_RADIUS 8 +#define PLAYER_SNAKE_COLUMNS 3 +#define PLAYER_SNAKE_DAMAGE_DEFAULT 1 +#define PLAYER_SNAKE_FRAME_HEIGHT 32 +#define PLAYER_SNAKE_FRAME_WIDTH 48 +#define PLAYER_SNAKE_HEIGHT 32 +#define PLAYER_SNAKE_OFFSET 32 +#define PLAYER_SNAKE_REDIRECT_VELOCITY_MULTIPLIER 0.25 +#define PLAYER_SNAKE_REDIRECT_LIFETIME_MULTIPLIER 1.0 +#define PLAYER_SNAKE_RETURN_TIMER_MAX 30 +#define PLAYER_SNAKE_RETURN_VELOCITY_MULTIPLIER 0.05 +#define PLAYER_SNAKE_ROWS 1 +#define PLAYER_SNAKE_VELOCITY 7.5 +#define PLAYER_SNAKE_WIDTH 48 +#define PLAYER_SNAKE_ENEMY_PIERCE_TIMER_MAX 60 +#define PLAYER_SNAKE_REDIRECT_ANGLE_DIFFERENCE_MIN 15 +#define PLAYER_SNAKE_ARC_WIDTH 96 +#define PLAYER_SNAKE_ARC_HEIGHT 96 +#define PLAYER_SNAKE_ARC_OFFSET 64 + +static const SDL_Color PLAYER_SNAKE_SHADOW_COLOR = {0, 0, 0, 64}; + +void player_snake_add(Player* _p, Play* _l); +void player_snake_reset(Snake* _s, Player* _p, Play* _l); +void player_snakes_clear(Player* _p, Play* _l); +void player_snakes_draw(Player* _p, Play* _l); +void player_snakes_free(Player* _p, Play* _l); +void player_snakes_init(Player* _p, Play* _l); +void player_snakes_update(Player* _p, Play* _l); diff --git a/src/game/play/player/player_sprite.c b/src/game/play/player/player_sprite.c new file mode 100755 index 0000000..a71b195 --- /dev/null +++ b/src/game/play/player/player_sprite.c @@ -0,0 +1,112 @@ +/* + * DESCRIPTION: + * Functions concering player sprite. +*/ + +#include "player_sprite.h" + +/* Initializes player sprite. */ +void +player_sprite_init(Player* _p, Play* _l) +{ + Atlas atlas; + + atlas_init + ( + &atlas, + _l->g->textures[TEXTURE_PLAYER_STAGE_ONE], + PLAYER_SPRITE_ROWS, + PLAYER_SPRITE_COLUMNS, + PLAYER_SPRITE_WIDTH, + PLAYER_SPRITE_HEIGHT + ); + + sprite_atlas_init + ( + &_p->sprite, + &atlas, + ORIGIN_MIDDLE, + PLAYER_SPRITE_WIDTH, + PLAYER_SPRITE_HEIGHT + ); + + _p->sprite.x = PLAYER_SPRITE_X_DEFAULT; + _p->sprite.y = PLAYER_SPRITE_Y_DEFAULT; +} + +/* Frees player sprite. */ +void +player_sprite_free(Player* _p, Play* _l) +{ + + +} + +/* Updates the player sprite. */ +void +player_sprite_update(Player* _p, Play* _l) +{ + if (_p->direction == DIRECTION_LEFT) + _p->sprite.frame = 0; + else if (_p->direction == DIRECTION_RIGHT) + _p->sprite.frame = 1; + + if (_p->isFatnessUpdate) + { + /* + if (_p->isBeingFattened) + { + //_p->sprite.atlas.rows = ATLAS_ROWS_DEFAULT; + //_p->sprite.atlas.cols = ATLAS_COLUMNS_DEFAULT; + _p->sprite.frame = 0; + + switch (_p->fatness) + { + case 0: + _p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_STAGE_ONE]; + //_p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_SWALLOW_ONE]; + break; + case 1: + _p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_STAGE_ONE]; + //_p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_SWALLOW_TWO]; + break; + case 2: + _p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_STAGE_ONE]; + //_p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_SWALLOW_THREE]; + break; + default: + break; + } + } + else + { + */ + _p->sprite.atlas.rows = PLAYER_SPRITE_ROWS; + _p->sprite.atlas.cols = PLAYER_SPRITE_COLUMNS; + + switch (_p->fatness) + { + case 0: + _p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_STAGE_ONE]; + break; + case 1: + _p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_STAGE_TWO]; + break; + case 2: + _p->sprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_STAGE_THREE]; + break; + default: + break; + } + /* + } + */ + } +} + +/* Draws the player sprite. */ +void +player_sprite_draw(Player* _p, Play* _l) +{ + sprite_draw(&_p->sprite, _l->g->renderer); +} diff --git a/src/game/play/player/player_sprite.h b/src/game/play/player/player_sprite.h new file mode 100755 index 0000000..f6c9c0c --- /dev/null +++ b/src/game/play/player/player_sprite.h @@ -0,0 +1,24 @@ +/* + * DESCRIPTION: + * Header file for player_sprite.h. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" + +#define PLAYER_SPRITE_HEIGHT 96 +#define PLAYER_SPRITE_WIDTH 96 +#define PLAYER_SPRITE_STAGE_FOUR_WIDTH 128 +#define PLAYER_SPRITE_STAGE_FOUR_HEIGHT 128 +#define PLAYER_SPRITE_ROWS 1 +#define PLAYER_SPRITE_COLUMNS 2 +#define PLAYER_SPRITE_X_DEFAULT 400 +#define PLAYER_SPRITE_Y_DEFAULT 300 + +static const SDL_Color PLAYER_SPRITE_ELLIPSE_COLOR = {0, 0, 0, 128}; + +void player_sprite_init(Player* _p, Play* _l); +void player_sprite_update(Player* _p, Play* _l); +void player_sprite_draw(Player* _p, Play* _l); +void player_sprite_free(Player* _p, Play* _l); diff --git a/src/game/play/player/player_tail.c b/src/game/play/player/player_tail.c new file mode 100755 index 0000000..dc318c5 --- /dev/null +++ b/src/game/play/player/player_tail.c @@ -0,0 +1,129 @@ +/* + * DESCRIPTION: + * Functions concering player tail. +*/ + +#include "player_tail.h" + +static void player_tail_set(Player* _p, Play* _l); + +/* Sets tail's position/dimensions behind player. */ +void +player_tail_set(Player* _p, Play* _l) +{ + switch (_p->direction) + { + case DIRECTION_LEFT: + _p->tailSprite.x = _p->sprite.x + PLAYER_TAIL_LEFT_OFFSET_X; + _p->tailSprite.y = _p->sprite.y + PLAYER_TAIL_LEFT_OFFSET_Y; + break; + case DIRECTION_RIGHT: + _p->tailSprite.x = _p->sprite.x + PLAYER_TAIL_RIGHT_OFFSET_X; + _p->tailSprite.y = _p->sprite.y + PLAYER_TAIL_RIGHT_OFFSET_Y; + break; + /* + case DIRECTION_UP: + _p->tailSprite.x = _p->sprite.x + PLAYER_TAIL_UP_OFFSET_X; + _p->tailSprite.y = _p->sprite.y + PLAYER_TAIL_UP_OFFSET_Y; + break; + case DIRECTION_DOWN: + _p->tailSprite.x = _p->sprite.x + PLAYER_TAIL_DOWN_OFFSET_X; + _p->tailSprite.y = _p->sprite.y + PLAYER_TAIL_DOWN_OFFSET_Y; + break; + */ + default: + break; + } +} + +/* Initializes player tail. */ +void +player_tail_init(Player* _p, Play* _l) +{ + Atlas atlas; + + atlas_init + ( + &atlas, + _l->g->textures[TEXTURE_PLAYER_TAIL_STAGE_ONE], + PLAYER_TAIL_ROWS, + PLAYER_TAIL_COLUMNS, + PLAYER_TAIL_WIDTH, + PLAYER_TAIL_HEIGHT + ); + + sprite_atlas_init + ( + &_p->tailSprite, + &atlas, + ORIGIN_MIDDLE, + PLAYER_TAIL_WIDTH, + PLAYER_TAIL_HEIGHT + ); + + player_tail_set(_p, _l); +} + +/* Frees player tail. */ +void +player_tail_free(Player* _p, Play* _l) +{ + + +} + +/* Updates the player tail. */ +void +player_tail_update(Player* _p, Play* _l) +{ + s32 frame; + + if (_p->isAbleToMove) + { + frame = _p->direction * PLAYER_TAIL_ANIMATE_COUNT; + + if (_p->isTailAnimate) + frame += 1; + + _p->tailSprite.frame = frame; + + if (!_p->isStationary) + { + _p->tailTimer--; + + if (_p->tailTimer <= 0) + { + _p->tailTimer = PLAYER_TAIL_TIMER_MAX * (_p->fatness + 1); + _p->isTailAnimate = !_p->isTailAnimate; + } + } + } + + if (_p->isFatnessUpdate) + { + switch (_p->fatness) + { + case 0: + _p->tailSprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_TAIL_STAGE_ONE]; + break; + case 1: + _p->tailSprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_TAIL_STAGE_TWO]; + break; + case 2: + _p->tailSprite.atlas.texture = _l->g->textures[TEXTURE_PLAYER_TAIL_STAGE_THREE]; + break; + default: + break; + } + } + + player_tail_set(_p, _l); +} + +/* Draws the player tail. */ +void +player_tail_draw(Player* _p, Play* _l) +{ + //if (!_p->isBeingFattened && !_p->isBeingDefeated) + sprite_draw(&_p->tailSprite, _l->g->renderer); +} diff --git a/src/game/play/player/player_tail.h b/src/game/play/player/player_tail.h new file mode 100755 index 0000000..30b4e4a --- /dev/null +++ b/src/game/play/player/player_tail.h @@ -0,0 +1,28 @@ +/* + * DESCRIPTION: + * Header file for player_tail.h. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" + +#define PLAYER_TAIL_HEIGHT 96 +#define PLAYER_TAIL_WIDTH 96 +#define PLAYER_TAIL_LEFT_OFFSET_X 96 +#define PLAYER_TAIL_RIGHT_OFFSET_X -96 +#define PLAYER_TAIL_UP_OFFSET_X 0 +#define PLAYER_TAIL_DOWN_OFFSET_X 0 +#define PLAYER_TAIL_LEFT_OFFSET_Y 0 +#define PLAYER_TAIL_RIGHT_OFFSET_Y 0 +#define PLAYER_TAIL_UP_OFFSET_Y 96 +#define PLAYER_TAIL_DOWN_OFFSET_Y -96 +#define PLAYER_TAIL_ROWS 1 +#define PLAYER_TAIL_COLUMNS 4 +#define PLAYER_TAIL_ANIMATE_COUNT 2 +#define PLAYER_TAIL_TIMER_MAX 5 + +void player_tail_init(Player* _p, Play* _l); +void player_tail_update(Player* _p, Play* _l); +void player_tail_draw(Player* _p, Play* _l); +void player_tail_free(Player* _p, Play* _l); diff --git a/src/game/play/player/player_upgrades.c b/src/game/play/player/player_upgrades.c new file mode 100755 index 0000000..2afb927 --- /dev/null +++ b/src/game/play/player/player_upgrades.c @@ -0,0 +1,85 @@ +/* + * DESCRIPTION: + * Dictates player upgrades (redirect, lifetime, snakes) +*/ + +#include "player_upgrades.h" + +static s32 player_upgrades_snake_get(Player* _p, Play* _l); +static s32 player_upgrades_lifetime_get(Player* _p, Play* _l); +static s32 player_upgrades_redirect_get(Player* _p, Play* _l); + +/* Gets the snakes the player can have. */ +static s32 +player_upgrades_snake_get(Player* _p, Play* _l) +{ + s32 snakes; + s32 upgradeLevel; + + upgradeLevel = _p->snakeUpgradeLevel; + + snakes = upgradeLevel; + + return snakes; +} + +/* Gets the lifetime of the snakes the player has. */ +static s32 +player_upgrades_lifetime_get(Player* _p, Play* _l) +{ + s32 lifetime; + s32 upgradeLevel; + + upgradeLevel = _p->lifetimeUpgradeLevel; + + lifetime = ((upgradeLevel - 1) * PLAYER_UPGRADES_LIFETIME_MULTIPLIER) + PLAYER_UPGRADES_LIFETIME_BASE; + + return lifetime; +} + +/* Gets the redirects the player can have. */ +static s32 +player_upgrades_redirect_get(Player* _p, Play* _l) +{ + s32 redirects; + s32 upgradeLevel; + + upgradeLevel = _p->redirectUpgradeLevel; + + redirects = upgradeLevel; + + return redirects; +} + +/* Initializes player upgrades. */ +void +player_upgrades_init(Player* _p, Play* _l) +{ + _p->snakeUpgradeLevel = PLAYER_UPGRADE_LEVEL_BASE; + _p->redirectUpgradeLevel = PLAYER_UPGRADE_LEVEL_BASE; + _p->lifetimeUpgradeLevel = PLAYER_UPGRADE_LEVEL_BASE; + + _p->snakeMax = player_upgrades_snake_get(_p, _l); + _p->redirectMax = player_upgrades_redirect_get(_p, _l); + _p->lifetime = player_upgrades_lifetime_get(_p, _l); +} + +/* Updates player upgrades. */ +void +player_upgrades_update(Player* _p, Play* _l) +{ + if (_p->isSnakeUpgradeUpdate) + { + _p->snakeMax = player_upgrades_snake_get(_p, _l); + _p->snakeReserve = _p->snakeMax; + } + + if (_p->isRedirectUpgradeUpdate) + { + _p->redirectMax = player_upgrades_redirect_get(_p, _l); + _p->redirects = _p->redirectMax; + } + + if (_p->isLifetimeUpgradeUpdate) + _p->lifetime = player_upgrades_lifetime_get(_p, _l); +} diff --git a/src/game/play/player/player_upgrades.h b/src/game/play/player/player_upgrades.h new file mode 100755 index 0000000..c1c4b34 --- /dev/null +++ b/src/game/play/player/player_upgrades.h @@ -0,0 +1,18 @@ +/* + * DESCRIPTION: + * Header file for player_upgrades.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" + +#define PLAYER_UPGRADE_LEVEL_BASE 1 +#define PLAYER_UPGRADES_SNAKE_BASE 1 +#define PLAYER_UPGRADES_LIFETIME_BASE 30 +#define PLAYER_UPGRADES_REDIRECT_BASE 1 +#define PLAYER_UPGRADES_POWER_UP_MULTIPLIER 2 +#define PLAYER_UPGRADES_LIFETIME_MULTIPLIER 3 + +void player_upgrades_init(Player* _p, Play* _l); +void player_upgrades_update(Player* _p, Play* _l); diff --git a/src/game/play/shadow/shadow.c b/src/game/play/shadow/shadow.c new file mode 100755 index 0000000..b82c7fe --- /dev/null +++ b/src/game/play/shadow/shadow.c @@ -0,0 +1,27 @@ +/* + * DESCRIPTION: + * Entity shadows. +*/ + +#include "shadow.h" + +/* Draws a shadow. */ +void +shadow_draw(Circle* _c, Play* _p) +{ + Sprite sprite; + + sprite_init + ( + &sprite, + _p->g->textures[TEXTURE_SHADOW], + ORIGIN_MIDDLE, + _c->r * SHADOW_SIZE_MULTIPLIER, + _c->r * SHADOW_SIZE_MULTIPLIER + ); + + sprite.x = _c->x; + sprite.y = _c->y; + + sprite_draw(&sprite, _p->g->renderer); +} diff --git a/src/game/play/shadow/shadow.h b/src/game/play/shadow/shadow.h new file mode 100755 index 0000000..8cc8a68 --- /dev/null +++ b/src/game/play/shadow/shadow.h @@ -0,0 +1,10 @@ +/* + * DESCRIPTION: + * Header file for shadow.c. +*/ + +#include "../../GAME_COMMON.h" + +#define SHADOW_SIZE_MULTIPLIER 1.5 + +void shadow_draw(Circle* _c, Play* _p); diff --git a/src/game/play/spawn/spawn.c b/src/game/play/spawn/spawn.c new file mode 100755 index 0000000..70c85e2 --- /dev/null +++ b/src/game/play/spawn/spawn.c @@ -0,0 +1,166 @@ +/* + * DESCRIPTION: + * Spawns enemies. +*/ + +#include "spawn.h" + +static s32 spawn_timer_max_get(Play* _p); + +/* Gets a new spawn timer max, influenced by the wave. */ +static s32 +spawn_timer_max_get(Play* _p) +{ + s32 spawnTimerBase; + s32 spawnTimerMax; + + spawnTimerBase = SPAWN_TIMER_MAX_DEFAULT - ((_p->wave * _p->game) * SPAWN_TIMER_WAVE_MULTIPLIER); + + spawnTimerBase = FLOOR(spawnTimerBase, SPAWN_TIMER_BASE_MIN); + + spawnTimerMax = random_s32_in_range_get + ( + (s32)(spawnTimerBase * SPAWN_TIMER_MIN_MULTIPLIER), + (s32)(spawnTimerBase * SPAWN_TIMER_MAX_MULTIPLIER) + ); + + return spawnTimerMax; +} + +static void +spawn_enemy_set(Enemy* _e, Play* _p) +{ + f64 x; + f64 y; + Direction direction; + + direction = (Direction)random_s32_in_range_get(DIRECTION_LEFT, DIRECTION_DOWN); + + x = 0; + y = 0; + + switch (_e->type) + { + case ENEMY_CUPPER: + switch (direction) + { + case DIRECTION_LEFT: + x = -CUPPER_WIDTH; + y = random_f64_in_range_get(-CUPPER_HEIGHT, SPAWN_Y_MAX + CUPPER_HEIGHT); + break; + case DIRECTION_RIGHT: + x = SPAWN_X_MAX + CUPPER_WIDTH; + y = random_f64_in_range_get(-CUPPER_HEIGHT, SPAWN_Y_MAX + CUPPER_HEIGHT); + break; + case DIRECTION_UP: + x = random_f64_in_range_get(-CUPPER_WIDTH, SPAWN_X_MAX + CUPPER_WIDTH); + y = -CUPPER_HEIGHT; + break; + case DIRECTION_DOWN: + x = random_f64_in_range_get(-CUPPER_WIDTH, SPAWN_X_MAX + CUPPER_WIDTH); + y = SPAWN_Y_MAX + CUPPER_HEIGHT; + break; + default: + break; + } + break; + case ENEMY_CHIPPER: + switch (direction) + { + case DIRECTION_LEFT: + x = -CHIPPER_WIDTH; + y = random_f64_in_range_get(-CHIPPER_HEIGHT, SPAWN_Y_MAX + CHIPPER_HEIGHT); + break; + case DIRECTION_RIGHT: + x = SPAWN_X_MAX + CHIPPER_WIDTH; + y = random_f64_in_range_get(-CHIPPER_HEIGHT, SPAWN_Y_MAX + CHIPPER_HEIGHT); + break; + case DIRECTION_UP: + x = random_f64_in_range_get(-CHIPPER_WIDTH, SPAWN_X_MAX + CHIPPER_WIDTH); + y = -CHIPPER_HEIGHT; + break; + case DIRECTION_DOWN: + x = random_f64_in_range_get(-CHIPPER_WIDTH, SPAWN_X_MAX + CHIPPER_WIDTH); + y = SPAWN_Y_MAX + CHIPPER_HEIGHT; + break; + default: + break; + } + break; + case ENEMY_CHIP: + switch (direction) + { + case DIRECTION_LEFT: + x = -CHIP_WIDTH; + y = random_f64_in_range_get(-CHIP_HEIGHT, SPAWN_Y_MAX + CHIP_HEIGHT); + break; + case DIRECTION_RIGHT: + x = SPAWN_X_MAX + CHIP_WIDTH; + y = random_f64_in_range_get(-CHIP_HEIGHT, SPAWN_Y_MAX + CHIP_HEIGHT); + break; + case DIRECTION_UP: + x = random_f64_in_range_get(-CHIP_WIDTH, SPAWN_X_MAX + CHIP_WIDTH); + y = -CHIP_HEIGHT; + break; + case DIRECTION_DOWN: + x = random_f64_in_range_get(-CHIP_WIDTH, SPAWN_X_MAX + CHIP_WIDTH); + y = SPAWN_Y_MAX + CHIP_HEIGHT; + break; + default: + break; + } + break; + case ENEMY_CRUMBLER: + x = random_f64_in_range_get(CRUMBLER_WIDTH, SPAWN_X_MAX - CRUMBLER_WIDTH); + y = random_f64_in_range_get(CRUMBLER_HEIGHT, SPAWN_Y_MAX - CRUMBLER_HEIGHT); + break; + case ENEMY_NONE: + default: + break; + } + + enemy_set(&_p->enemies, _e, x, y, _p); +} + +/* Initializes spawning. */ +void +spawn_init(Play* _p) +{ + _p->spawnTimer = SPAWN_TIMER_MAX_DEFAULT; +} + +/* Updates spawning. */ +void +spawn_update(Play* _p) +{ + _p->spawnTimer--; + + if (!_p->isGracePeriod) + { + if (_p->enemies.count == 0) + _p->spawnTimer = 0; + } + + if (_p->spawnTimer <= 0) + { + Node* node; + + node = list_node_from_index_get(&_p->spawnEnemies, 0); + + if (node) + { + Enemy* e; + EnemyType type; + + type = *(EnemyType*)node->data; + + list_remove(&_p->spawnEnemies, node); + + e = enemy_add(&_p->enemies, type, _p); + + spawn_enemy_set(e, _p); + } + + _p->spawnTimer = spawn_timer_max_get(_p); + } +} diff --git a/src/game/play/spawn/spawn.h b/src/game/play/spawn/spawn.h new file mode 100755 index 0000000..c8a1c24 --- /dev/null +++ b/src/game/play/spawn/spawn.h @@ -0,0 +1,19 @@ +/* + * DESCRIPTION: + * Header file for spawn.c. +*/ + +#pragma once + +#include "../enemy/enemy.h" + +#define SPAWN_TIMER_MAX_DEFAULT 150 +#define SPAWN_TIMER_BASE_MIN 30 +#define SPAWN_TIMER_WAVE_MULTIPLIER 5 +#define SPAWN_TIMER_MIN_MULTIPLIER 0.75 +#define SPAWN_TIMER_MAX_MULTIPLIER 1.25 +#define SPAWN_X_MAX 800 +#define SPAWN_Y_MAX 600 + +void spawn_init(Play* _p); +void spawn_update(Play* _p); diff --git a/src/game/play/tutorial/tutorial.c b/src/game/play/tutorial/tutorial.c new file mode 100755 index 0000000..504e25d --- /dev/null +++ b/src/game/play/tutorial/tutorial.c @@ -0,0 +1,79 @@ +/* + * DESCRIPTION: + * Displays some helpful tutorial text at the start of the game. +*/ + +#include "tutorial.h" + +/* Initializes tutorial stuff. */ +void +tutorial_init(Play* _p) +{ + if (_p->player.isKeyboardMove) + { + text_sprite_init + ( + &_p->tutorialText, + _p->g->renderer, + _p->g->fonts[FONT_SMALL], + TUTORIAL_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_TUTORIAL_KEYBOARD, + TUTORIAL_TEXT_WRAP + ); + } + else + { + text_sprite_init + ( + &_p->tutorialText, + _p->g->renderer, + _p->g->fonts[FONT_SMALL], + TUTORIAL_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_PLAY_TUTORIAL_MOUSE, + TUTORIAL_TEXT_WRAP + ); + } + + _p->tutorialText.x = TUTORIAL_TEXT_OFFSET_X; + _p->tutorialText.y = TUTORIAL_TEXT_OFFSET_Y; + + _p->isTutorial = TRUE; + _p->tutorialTimer = TUTORIAL_TIMER_MAX; +} + +/* Frees tutorial stuff. */ +void +tutorial_free(Play* _p) +{ + if (_p->tutorialText.atlas.texture) + texture_free(_p->tutorialText.atlas.texture); +} + +/* Updates tutorial stuff. */ +void +tutorial_update(Play* _p) +{ + if (_p->isTutorial) + { + f64 progress; + + _p->tutorialTimer--; + + progress = (f64)_p->tutorialTimer / TUTORIAL_TIMER_MAX; + + _p->tutorialText.color.a = SDL_ALPHA_OPAQUE * progress; + + if (_p->tutorialTimer <= 0) + _p->isTutorial = FALSE; + } +} + +/* Draws tutorial stuff. */ +void +tutorial_draw(Play* _p) +{ + if (_p->isTutorial) + sprite_draw(&_p->tutorialText, _p->g->renderer); +} diff --git a/src/game/play/tutorial/tutorial.h b/src/game/play/tutorial/tutorial.h new file mode 100755 index 0000000..a55ece0 --- /dev/null +++ b/src/game/play/tutorial/tutorial.h @@ -0,0 +1,21 @@ +/* + * DESCRIPTION: + * Header file for tutorial.c. +*/ + +#pragma once + +#include "../../GAME_COMMON.h" +#include "../../GAME_STRINGS.h" + +#define TUTORIAL_TEXT_WRAP 600 +#define TUTORIAL_TEXT_OFFSET_X 400 +#define TUTORIAL_TEXT_OFFSET_Y 500 +#define TUTORIAL_TIMER_MAX 600 + +static const SDL_Color TUTORIAL_TEXT_COLOR = {0, 0, 0, 255}; + +void tutorial_draw(Play* _p); +void tutorial_free(Play* _p); +void tutorial_init(Play* _p); +void tutorial_update(Play* _p); diff --git a/src/game/play/wave/wave.c b/src/game/play/wave/wave.c new file mode 100755 index 0000000..56bdb4a --- /dev/null +++ b/src/game/play/wave/wave.c @@ -0,0 +1,359 @@ +/* + * DESCRIPTION: + * Enemy waves. +*/ + +#include "wave.h" + +static void wave_set(Play* _p); +static void wave_next(Play* _p); +static void wave_clear(Play* _p); +static void wave_fail(Play* _p); +static void wave_reward(Play* _p); +static void wave_game_clear_begin(Play* _p); +static void wave_game_clear_begin(Play* _p); + +/* Returns the cash reward you'll get at the end of a wave. */ +static s32 +wave_reward_get(Play* _p) +{ + f64 cash; + + cash = _p->player.waveEnemiesKilled * WAVE_REWARD_MULTIPLIER; + + if (_p->game > 1) + cash *= ((_p->game - 1) * WAVE_REWARD_GAME_MULTIPLIER); + + return (s32)cash; +} + +/* Sets the current wave of enemies. */ +static void +wave_set(Play* _p) +{ + EnemyType type; + s32 index; + s32 enemyCountBase; + s32 enemyCount; + + enemyCountBase = WAVE_ENEMY_COUNT_BASE; + enemyCountBase *= (s32)(_p->wave * WAVE_ENEMY_COUNT_MULTIPLIER); + enemyCountBase *= (s32)(_p->game * WAVE_ENEMY_COUNT_GAME_MULTIPLIER); + + enemyCount = random_s32_in_range_get + ( + (s32)(enemyCountBase * WAVE_ENEMY_COUNT_BASE_MIN_MULTIPLIER), + (s32)(enemyCountBase * WAVE_ENEMY_COUNT_BASE_MAX_MULTIPLIER) + ); + + list_clear(&_p->spawnEnemies); + + _p->player.waveEnemiesKilled = 0; + _p->enemyMax = 0; + + for (s32 i = 0; i < enemyCount; i++) + { + s32 index; + EnemyType type; + + switch (_p->wave) + { + case 1: + index = random_s32_in_range_get(0, WAVE_ONE_ENEMY_TYPE_COUNT - 1); + type = WAVE_ONE[index]; + break; + case 2: + index = random_s32_in_range_get(0, WAVE_TWO_ENEMY_TYPE_COUNT - 1); + type = WAVE_TWO[index]; + break; + case 3: + index = random_s32_in_range_get(0, WAVE_THREE_ENEMY_TYPE_COUNT - 1); + type = WAVE_THREE[index]; + break; + case 4: + index = random_s32_in_range_get(0, WAVE_FOUR_ENEMY_TYPE_COUNT - 1); + type = WAVE_FOUR[index]; + break; + case 5: + index = random_s32_in_range_get(0, WAVE_FIVE_ENEMY_TYPE_COUNT - 1); + type = WAVE_FIVE[index]; + break; + case 6: + index = random_s32_in_range_get(0, WAVE_SIX_ENEMY_TYPE_COUNT - 1); + type = WAVE_SIX[index]; + break; + case 7: + index = random_s32_in_range_get(0, WAVE_SEVEN_ENEMY_TYPE_COUNT - 1); + type = WAVE_SEVEN[index]; + break; + case 8: + index = random_s32_in_range_get(0, WAVE_EIGHT_ENEMY_TYPE_COUNT - 1); + type = WAVE_EIGHT[index]; + break; + case 9: + index = random_s32_in_range_get(0, WAVE_NINE_ENEMY_TYPE_COUNT - 1); + type = WAVE_NINE[index]; + break; + case 10: + index = random_s32_in_range_get(0, WAVE_TEN_ENEMY_TYPE_COUNT - 1); + type = WAVE_TEN[index]; + break; + default: + break; + } + + _p->enemyMax++; + + list_append_new(&_p->spawnEnemies, (void*)&type, sizeof(type)); + } + + _p->isBountyUpdate = TRUE; + + _p->spawnTimer = WAVE_GRACE_PERIOD_TIMER_MAX; + _p->gracePeriodTimer = WAVE_GRACE_PERIOD_TIMER_MAX; + _p->isGracePeriod = TRUE; +} + +/* Entering between waves... */ +static void +wave_between(Play* _p) +{ + wave_reward(_p); + + _p->player.waveEnemiesKilled = 0; + _p->player.isSnakeUpdate = TRUE; + + _p->isBetweenWaves = TRUE; + _p->isReady = FALSE; + _p->player.isAbleToFire = FALSE; + _p->isWaveUpdate = TRUE; + _p->isUpgradesActive = TRUE; + _p->isUpgradeUpdate = TRUE; + _p->isBountyUpdate = TRUE; + + if (_p->player.isPowerUp) + player_power_up_end(&_p->player, _p); +} + +/* Grants a cash reward on wave end. */ +static void +wave_reward(Play* _p) +{ + s32 cash; + + cash = wave_reward_get(_p); + + player_cash_acquire(&_p->player, cash, _p); + + sound_play(_p->g->sounds[SOUND_BULLIONS], -1); +} + +/* Cleared the wave! */ +static void +wave_clear(Play* _p) +{ + _p->isWaveCleared = TRUE; + + if (_p->wave + 1 > WAVE_MAX) + wave_game_clear_begin(_p); + else + { + message_wave_clear_add(_p->player.sprite.x, _p->player.sprite.y, _p); + + music_play(_p->g->music[MUSIC_WAVE_CLEAR], FALSE); + + _p->countdownTimer = WAVE_BETWEEN_TIMER_MAX; + + _p->isWaveUpdate = TRUE; + _p->isBetweenWaves = TRUE; + } +} + +/* Failed the wave. */ +static void +wave_fail(Play* _p) +{ + _p->isWaveFailed = TRUE; + + if (!_p->player.isBeingDefeated) + { + if (_p->wave + 1 > WAVE_MAX) + wave_game_clear_begin(_p); + else + { + message_wave_fail_add(_p->player.sprite.x, _p->player.sprite.y, _p); + + music_play(_p->g->music[MUSIC_WAVE_FAIL], FALSE); + + _p->countdownTimer = WAVE_BETWEEN_TIMER_MAX; + + _p->isWaveUpdate = TRUE; + _p->isBetweenWaves = TRUE; + + } + + } +} + +/* Goes to the next wave. */ +static void +wave_next(Play* _p) +{ + _p->isUpgradesActive = FALSE; + + _p->player.isAbleToFire = TRUE; + + _p->isBetweenWaves = FALSE; + + _p->wave++; + + _p->spawnTimer = WAVE_GRACE_PERIOD_TIMER_MAX; + + _p->isWaveCleared = FALSE; + _p->isWaveFailed = FALSE; + _p->isWaveUpdate = TRUE; + + wave_set(_p); +} + +/* You just beat the game! */ +static void +wave_game_clear_begin(Play* _p) +{ + _p->isBetweenWaves = TRUE; + _p->isWaveFailed = FALSE; + _p->isWaveCleared = FALSE; + _p->isGameClearing = TRUE; + _p->gameClearTimer = WAVE_GAME_CLEAR_TIMER_MAX; + + list_clear(&_p->enemies); + + player_snakes_clear(&_p->player, _p); + + message_game_clear_add(_p->player.sprite.x, _p->player.sprite.y, _p); + + music_play(_p->g->music[MUSIC_WIND], TRUE); +} + +/* You beat the game! */ +static void +wave_game_clear(Play* _p) +{ + _p->isGameCleared = TRUE; +} + +/* Initializes waves. */ +void +wave_init(Play* _p) +{ + list_init(&_p->spawnEnemies); + + _p->wave = WAVE_DEFAULT; + _p->game = GAME_DEFAULT; + _p->isWaveCleared = FALSE; + _p->isGameCleared = FALSE; + + wave_set(_p); +} + +/* New Game+! */ +void +wave_new_game_plus(Play* _p) +{ + list_clear(&_p->collectibles); + + _p->game++; + _p->wave = WAVE_DEFAULT; + _p->isWaveCleared = FALSE; + _p->isGameCleared = FALSE; + _p->isGameClearing = FALSE; + _p->isWaveFailed = FALSE; + _p->isWaveUpdate = TRUE; + _p->isReady = FALSE; + _p->isBetweenWaves = FALSE; + _p->isUpgradesActive = FALSE; + + _p->player.sprite.x = PLAYER_SPRITE_X_DEFAULT; + _p->player.sprite.y = PLAYER_SPRITE_Y_DEFAULT; + + _p->player.isAbleToFire = TRUE; + _p->player.isAbleToMove = TRUE; + + _p->player.snakeReserve = _p->player.snakeMax; + _p->player.redirects = _p->player.redirectMax; + + music_play(_p->g->music[MUSIC_PLAY], TRUE); + + wave_set(_p); +} + + +/* Updates waves. */ +void +wave_update(Play* _p) +{ + if (!_p->isGameClearing) + { + if (_p->isGracePeriod) + { + _p->gracePeriodTimer--; + + if (_p->gracePeriodTimer <= 0) + _p->isGracePeriod = FALSE; + } + + if (_p->isWaveUpdate) + _p->isWaveUpdate = FALSE; + + if (_p->isBetweenWaves) + { + if (!_p->isWaveClearMusicComplete) + { + if (!music_playing()) + { + _p->isWaveClearMusicComplete = TRUE; + music_play(_p->g->music[MUSIC_WIND], TRUE); + wave_between(_p); + } + } + else + { + if (_p->isReady) + { + _p->isWaveClearMusicComplete = FALSE; + + music_play(_p->g->music[MUSIC_PLAY], TRUE); + + _p->isReady = FALSE; + _p->isBetweenWaves = FALSE; + + wave_next(_p); + } + } + } + else + { + if + ( + _p->enemies.count <= 0 && + _p->spawnEnemies.count <= 0 + ) + _p->isWaveCleared = TRUE; + + if (_p->isWaveFailed) + wave_fail(_p); + else if (_p->isWaveCleared) + wave_clear(_p); + } + } + else + { + _p->gameClearTimer--; + + _p->player.isAbleToMove = FALSE; + _p->player.isAbleToFire = FALSE; + + if (_p->gameClearTimer <= 0) + wave_game_clear(_p); + } +} diff --git a/src/game/play/wave/wave.h b/src/game/play/wave/wave.h new file mode 100755 index 0000000..7ad204d --- /dev/null +++ b/src/game/play/wave/wave.h @@ -0,0 +1,100 @@ +/* + * DESCRIPTION: + * Header file for wave.c. +*/ + +#pragma once + +#include "../player/player_cash.h" +#include "../player/player_snakes.h" +#include "../player/player_sprite.h" +#include "../player/player_power_up.h" + +#define GAME_DEFAULT 1 +#define WAVE_GRACE_PERIOD_TIMER_MAX 300 +#define WAVE_DEFAULT 1 +#define WAVE_ENEMY_COUNT_BASE_MAX_MULTIPLIER 1.25 +#define WAVE_ENEMY_COUNT_BASE_MIN_MULTIPLIER 0.75 +#define WAVE_ENEMY_COUNT_GAME_MULTIPLIER 1.25 +#define WAVE_ENEMY_COUNT_MULTIPLIER 1.25 +#define WAVE_MAX 10 +#define WAVE_ENEMY_COUNT_BASE 5 +#define WAVE_REWARD_GAME_MULTIPLIER 2 +#define WAVE_REWARD_MULTIPLIER 10 +#define WAVE_BETWEEN_TIMER_MAX 180 +#define WAVE_GAME_CLEAR_TIMER_MAX 300 + +#define WAVE_ONE_ENEMY_TYPE_COUNT 1 +#define WAVE_TWO_ENEMY_TYPE_COUNT 1 +#define WAVE_THREE_ENEMY_TYPE_COUNT 1 +#define WAVE_FOUR_ENEMY_TYPE_COUNT 2 +#define WAVE_FIVE_ENEMY_TYPE_COUNT 2 +#define WAVE_SIX_ENEMY_TYPE_COUNT 2 +#define WAVE_SEVEN_ENEMY_TYPE_COUNT 3 +#define WAVE_EIGHT_ENEMY_TYPE_COUNT 3 +#define WAVE_NINE_ENEMY_TYPE_COUNT 1 +#define WAVE_TEN_ENEMY_TYPE_COUNT 4 + +static const EnemyType WAVE_ONE[WAVE_ONE_ENEMY_TYPE_COUNT] = +{ + ENEMY_CUPPER +}; + +static const EnemyType WAVE_TWO[WAVE_TWO_ENEMY_TYPE_COUNT] = +{ + ENEMY_CHIPPER +}; + +static const EnemyType WAVE_THREE[WAVE_THREE_ENEMY_TYPE_COUNT] = +{ + ENEMY_CRUMBLER +}; + +static const EnemyType WAVE_FOUR[WAVE_FOUR_ENEMY_TYPE_COUNT] = +{ + ENEMY_CUPPER, + ENEMY_CHIPPER +}; + +static const EnemyType WAVE_FIVE[WAVE_FIVE_ENEMY_TYPE_COUNT] = +{ + ENEMY_CUPPER, + ENEMY_CRUMBLER +}; + +static const EnemyType WAVE_SIX[WAVE_SIX_ENEMY_TYPE_COUNT] = +{ + ENEMY_CHIPPER, + ENEMY_CRUMBLER, +}; + +static const EnemyType WAVE_SEVEN[WAVE_SEVEN_ENEMY_TYPE_COUNT] = +{ + ENEMY_CUPPER, + ENEMY_CHIPPER, + ENEMY_CRUMBLER +}; + +static const EnemyType WAVE_EIGHT[WAVE_EIGHT_ENEMY_TYPE_COUNT] = +{ + ENEMY_CUPPER, + ENEMY_CHIPPER, + ENEMY_CRUMBLER +}; + +static const EnemyType WAVE_NINE[WAVE_NINE_ENEMY_TYPE_COUNT] = +{ + ENEMY_CHIP +}; + +static const EnemyType WAVE_TEN[WAVE_TEN_ENEMY_TYPE_COUNT] = +{ + ENEMY_CUPPER, + ENEMY_CHIPPER, + ENEMY_CRUMBLER, + ENEMY_CHIP +}; + +void wave_init(Play* _p); +void wave_update(Play* _p); +void wave_new_game_plus(Play* _p); diff --git a/src/game/results/results.c b/src/game/results/results.c new file mode 100755 index 0000000..19ac7f3 --- /dev/null +++ b/src/game/results/results.c @@ -0,0 +1,279 @@ +/* + * DESCRIPTION: + * Results code. +*/ + +#include "results.h" + +static void results_highest_wave_set(Results* _r); +static void results_cash_held_set(Results* _r); +static void results_cash_collected_set(Results* _r); +static void results_bounties_hunted_set(Results* _r); + +/* Highest Wave: X (NG+Y)*/ +static void +results_highest_wave_set(Results* _r) +{ + char* s; + + s = malloc(RESULTS_TEXT_MAX); + + if (_r->g->play.game > 1) + { + snprintf + ( + s, + RESULTS_TEXT_MAX, + "%s%i(NG+%i)", + STRING_RESULTS_HIGHEST_WAVE, + _r->g->play.wave, + _r->g->play.game + ); + + } + else + { + snprintf + ( + s, + RESULTS_TEXT_MAX, + "%s%i", + STRING_RESULTS_HIGHEST_WAVE, + _r->g->play.wave + ); + } + + text_sprite_init + ( + &_r->highestWave, + _r->g->renderer, + _r->g->fonts[FONT_BIG], + RESULTS_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + _r->highestWave.x = RESULTS_HIGHEST_WAVE_OFFSET_X; + _r->highestWave.y = RESULTS_HIGHEST_WAVE_OFFSET_Y; + + free(s); +} + +/* Bounties Hunted: X */ +static void +results_bounties_hunted_set(Results* _r) +{ + char* s; + + s = malloc(RESULTS_TEXT_MAX); + + snprintf + ( + s, + RESULTS_TEXT_MAX, + "%s%i", + STRING_RESULTS_BOUNTIES_HUNTED, + _r->g->play.player.enemiesKilled + ); + + text_sprite_init + ( + &_r->bountiesHunted, + _r->g->renderer, + _r->g->fonts[FONT_BIG], + RESULTS_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + _r->bountiesHunted.x = RESULTS_BOUNTIES_HUNTED_OFFSET_X; + _r->bountiesHunted.y = RESULTS_BOUNTIES_HUNTED_OFFSET_Y; + + free(s); +} + +/* Cash Collected: $X */ +static void +results_cash_collected_set(Results* _r) +{ + char* s; + + s = malloc(RESULTS_TEXT_MAX); + + snprintf + ( + s, + RESULTS_TEXT_MAX, + "%s$%i", + STRING_RESULTS_CASH_COLLECTED, + _r->g->play.player.cashCollected + ); + + text_sprite_init + ( + &_r->cashCollected, + _r->g->renderer, + _r->g->fonts[FONT_BIG], + RESULTS_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + _r->cashCollected.x = RESULTS_CASH_COLLECTED_OFFSET_X; + _r->cashCollected.y = RESULTS_CASH_COLLECTED_OFFSET_Y; + + free(s); +} + +/* Cash Held (On Defeat): $X */ +static void +results_cash_held_set(Results* _r) +{ + char* s; + + s = malloc(RESULTS_TEXT_MAX); + + snprintf + ( + s, + RESULTS_TEXT_MAX, + "%s$%i", + STRING_RESULTS_CASH_HELD, + _r->g->play.player.cash + ); + + text_sprite_init + ( + &_r->cashHeld, + _r->g->renderer, + _r->g->fonts[FONT_BIG], + RESULTS_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + _r->cashHeld.x = RESULTS_CASH_HELD_OFFSET_X; + _r->cashHeld.y = RESULTS_CASH_HELD_OFFSET_Y; + + free(s); +} + +/* Initializes results stuff. */ +void +results_init(Results* _r, Game* _g) +{ + memset(_r, '\0', sizeof(Results)); + + _r->g = _g; + + results_highest_wave_set(_r); + results_bounties_hunted_set(_r); + results_cash_collected_set(_r); + results_cash_held_set(_r); + + text_sprite_init + ( + &_r->resultsText, + _r->g->renderer, + _r->g->fonts[FONT_MEGA], + RESULTS_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_RESULTS, + 0 + ); + + _r->resultsText.x = RESULTS_TEXT_OFFSET_X; + _r->resultsText.y = RESULTS_TEXT_OFFSET_Y; + + text_sprite_init + ( + &_r->thanksText, + _r->g->renderer, + _r->g->fonts[FONT_MEGA], + RESULTS_TEXT_COLOR, + ORIGIN_MIDDLE, + STRING_RESULTS_THANKS, + 0 + ); + + _r->thanksText.x = RESULTS_THANKS_TEXT_OFFSET_X; + _r->thanksText.y = RESULTS_THANKS_TEXT_OFFSET_Y; + + sprite_init + ( + &_r->sprite, + _r->g->textures[TEXTURE_RESULTS], + ORIGIN_MIDDLE, + RESULTS_SPRITE_WIDTH, + RESULTS_SPRITE_HEIGHT + ); + + _r->sprite.x = RESULTS_SPRITE_OFFSET_X; + _r->sprite.y = RESULTS_SPRITE_OFFSET_Y; + + music_play(_r->g->music[MUSIC_RESULTS], FALSE); +} + +/* Frees results stuff. */ +void +results_free(Results* _r) +{ + if (_r->highestWave.atlas.texture) + texture_free(_r->highestWave.atlas.texture); + + if (_r->bountiesHunted.atlas.texture) + texture_free(_r->bountiesHunted.atlas.texture); + + if (_r->cashHeld.atlas.texture) + texture_free(_r->cashHeld.atlas.texture); + + if (_r->cashCollected.atlas.texture) + texture_free(_r->cashCollected.atlas.texture); + + if (_r->thanksText.atlas.texture) + texture_free(_r->thanksText.atlas.texture); + + if (_r->resultsText.atlas.texture) + texture_free(_r->thanksText.atlas.texture); +} + +/* Updates results stuff. */ +void +results_update(Results* _r) +{ + if (mouse_press(&_r->g->mouse, MOUSE_LEFT)) + { + _r->isEnd = TRUE; + + sound_play(_r->g->sounds[SOUND_MENU], -1); + } +} + +/* Draws results stuff. */ +void +results_draw(Results* _r) +{ + SDL_Rect r; + + r.x = 0; + r.y = 0; + r.w = RESULTS_RECTANGLE_WIDTH; + r.h = RESULTS_RECTANGLE_HEIGHT; + + rectangle_draw(&r, _r->g->renderer, RESULTS_BACKGROUND_COLOR); + + sprite_draw(&_r->resultsText, _r->g->renderer); + + sprite_draw(&_r->sprite, _r->g->renderer); + + sprite_draw(&_r->highestWave, _r->g->renderer); + sprite_draw(&_r->bountiesHunted, _r->g->renderer); + sprite_draw(&_r->cashCollected, _r->g->renderer); + sprite_draw(&_r->cashHeld, _r->g->renderer); + + sprite_draw(&_r->thanksText, _r->g->renderer); +} diff --git a/src/game/results/results.h b/src/game/results/results.h new file mode 100755 index 0000000..72bb348 --- /dev/null +++ b/src/game/results/results.h @@ -0,0 +1,37 @@ +/* + * DESCRIPTION: + * Header file for results.c. +*/ + +#pragma once + +#include "../GAME_COMMON.h" +#include "../GAME_STRINGS.h" + +#define RESULTS_BOUNTIES_HUNTED_OFFSET_X 400 +#define RESULTS_BOUNTIES_HUNTED_OFFSET_Y 400 +#define RESULTS_CASH_COLLECTED_OFFSET_X 400 +#define RESULTS_CASH_COLLECTED_OFFSET_Y 425 +#define RESULTS_CASH_HELD_OFFSET_X 400 +#define RESULTS_CASH_HELD_OFFSET_Y 450 +#define RESULTS_HIGHEST_WAVE_OFFSET_X 400 +#define RESULTS_HIGHEST_WAVE_OFFSET_Y 375 +#define RESULTS_TEXT_MAX 0xFF +#define RESULTS_TEXT_OFFSET_X 400 +#define RESULTS_TEXT_OFFSET_Y 50 +#define RESULTS_SPRITE_OFFSET_X 400 +#define RESULTS_SPRITE_OFFSET_Y 200 +#define RESULTS_SPRITE_WIDTH 400 +#define RESULTS_SPRITE_HEIGHT 200 +#define RESULTS_THANKS_TEXT_OFFSET_X 400 +#define RESULTS_THANKS_TEXT_OFFSET_Y 500 +#define RESULTS_RECTANGLE_WIDTH 800 +#define RESULTS_RECTANGLE_HEIGHT 600 + +static const SDL_Color RESULTS_TEXT_COLOR = {255, 255, 255, 255}; +static const SDL_Color RESULTS_BACKGROUND_COLOR = {0, 0, 0, 255}; + +void results_init(Results* _r, Game* _g); +void results_free(Results* _r); +void results_update(Results* _r); +void results_draw(Results* _r); diff --git a/src/game/scene/scene.c b/src/game/scene/scene.c new file mode 100755 index 0000000..327700b --- /dev/null +++ b/src/game/scene/scene.c @@ -0,0 +1,177 @@ +/* + * DESCRIPTION: + * Handles cutscenes. +*/ + +#include "scene.h" + +static void scene_text_sprite_set(SceneHandler* _h, Game* _g); +static void scene_text_set(SceneHandler* _h, Game* _g); +static void scene_text_end_set(SceneHandler* _h, Game* _g); +static void scene_sprite_set(SceneHandler* _h, Game* _g); + +static void +scene_sprite_set(SceneHandler* _h, Game* _g) +{ + sprite_init + ( + &_h->sprite, + _g->textures[_h->scenes[_h->sceneIndex].textureType], + ORIGIN_TOP_LEFT, + SCENE_SPRITE_WIDTH, + SCENE_SPRITE_HEIGHT + ); + + _h->sprite.x = SCENE_SPRITE_OFFSET_X; + _h->sprite.y = SCENE_SPRITE_OFFSET_Y; +} + +/* Sets the current text sprite. */ +static void +scene_text_sprite_set(SceneHandler* _h, Game* _g) +{ + char* s; + + s = malloc(_h->textIndex + 1); + memset(s, '\0', _h->textIndex + 1); + + memcpy(s, _h->scenes[_h->sceneIndex].text, _h->textIndex); + + if (_h->text.atlas.texture) + texture_free(_h->text.atlas.texture); + + text_sprite_init + ( + &_h->text, + _g->renderer, + _g->fonts[FONT_SMALL], + SCENE_TEXT_COLOR, + ORIGIN_TOP_LEFT, + s, + SCENE_WRAP + ); + + _h->text.x = SCENE_TEXT_OFFSET_X; + _h->text.y = SCENE_TEXT_OFFSET_Y; + + free(s); +} + +/* Sets new text. */ +static void +scene_text_set(SceneHandler* _h, Game* _g) +{ + _h->textIndex = 0; + _h->textLength = strlen(_h->scenes[_h->sceneIndex].text); + + _h->isTextEnd = FALSE; + + scene_text_sprite_set(_h, _g); +} + +/* Sets the text of the scene to its last index. */ +static void +scene_text_end_set(SceneHandler* _h, Game* _g) +{ + _h->textIndex = _h->textLength; + _h->isTextEnd = TRUE; + _h->updateTimer = 0; + + scene_text_sprite_set(_h, _g); +} + +/* Initializes the scene. */ +void +scene_init(SceneHandler* _h, Game* _g, Scene* _s, s32 _c) +{ + _h->scenes = _s; + _h->sceneCount = _c; + + _h->sceneIndex = 0; + _h->isTextEnd = FALSE; + _h->updateTimer = SCENE_UPDATE_TIMER_MAX; + + scene_sprite_set(_h, _g); + scene_text_set(_h, _g); +} + +/* Frees the scene. */ +void +scene_free(SceneHandler* _h, Game* _g) +{ + if (_h->text.atlas.texture) + texture_free(_h->text.atlas.texture); +} + +/* Updates the scene. */ +void +scene_update(SceneHandler* _h, Game* _g) +{ + if (!_h->isEnd) + { + if (!_h->isTextEnd) + { + _h->updateTimer--; + + if (_h->updateTimer <= 0) + { + _h->textIndex++; + + scene_text_sprite_set(_h, _g); + + _h->updateTimer = SCENE_UPDATE_TIMER_MAX; + + sound_play(_g->sounds[SOUND_BLIP], 0); + } + + if (mouse_press(&_g->mouse, MOUSE_LEFT)) + scene_text_end_set(_h, _g); + + if (_h->textIndex == _h->textLength) + _h->isTextEnd = TRUE; + } + else + { + if (mouse_press(&_g->mouse, MOUSE_LEFT)) + { + _h->sceneIndex++; + + if (_h->sceneIndex >= _h->sceneCount) + _h->isEnd = TRUE; + else + { + scene_sprite_set(_h, _g); + scene_text_set(_h, _g); + } + + sound_play(_g->sounds[SOUND_MENU], 1); + } + } + + if (mouse_press(&_g->mouse, MOUSE_RIGHT)) + { + scene_text_end_set(_h, _g); + _h->isEnd = TRUE; + } + } +} + +/* Draws the scene. */ +void +scene_draw(SceneHandler* _h, Game* _g) +{ + SDL_Rect r; + + r.x = 0; + r.y = 0; + r.w = SCENE_RECTANGLE_WIDTH; + r.h = SCENE_RECTANGLE_HEIGHT; + + rectangle_draw(&r, _g->renderer, SCENE_BACKGROUND_COLOR); + + sprite_draw(&_h->sprite, _g->renderer); + + sprite_draw(&_h->text, _g->renderer); +} + + diff --git a/src/game/scene/scene.h b/src/game/scene/scene.h new file mode 100755 index 0000000..8f658e0 --- /dev/null +++ b/src/game/scene/scene.h @@ -0,0 +1,22 @@ +#pragma once + +#include "../GAME_COMMON.h" + +#define SCENE_UPDATE_TIMER_MAX 4 +#define SCENE_WRAP 600 +#define SCENE_TEXT_OFFSET_X 100 +#define SCENE_TEXT_OFFSET_Y 400 +#define SCENE_SPRITE_WIDTH 800 +#define SCENE_SPRITE_HEIGHT 300 +#define SCENE_SPRITE_OFFSET_X 0 +#define SCENE_SPRITE_OFFSET_Y 50 +#define SCENE_RECTANGLE_WIDTH 800 +#define SCENE_RECTANGLE_HEIGHT 800 + +static const SDL_Color SCENE_BACKGROUND_COLOR = {0, 0, 0, 255}; +static const SDL_Color SCENE_TEXT_COLOR = {255, 255, 255, 255}; + +void scene_init(SceneHandler* _h, Game* _g, Scene* _s, s32 _c); +void scene_update(SceneHandler* _h, Game* _g); +void scene_draw(SceneHandler* _h, Game* _g); +void scene_free(SceneHandler* _h, Game* _g); diff --git a/src/game/title/title.c b/src/game/title/title.c new file mode 100755 index 0000000..f83fc78 --- /dev/null +++ b/src/game/title/title.c @@ -0,0 +1,177 @@ +/* + * DESCRIPTION: + * Title code. +*/ + +#include "title.h" + +/* Initializes title. */ +void +title_init(Title* _t, Game* _g) +{ + Sprite startButtonText; + Sprite infoButtonText; + char* s; + + memset(_t, '\0', sizeof(Title)); + + _t->g = _g; + + cursor_init(&_t->cursor, &_t->g->mouse, _t->g->textures[TEXTURE_GUI_CURSOR]); + + sprite_init + ( + &_t->startBackgroundSprite, + _t->g->textures[TEXTURE_GUI_SIGN_BIG], + ORIGIN_MIDDLE, + TITLE_BUTTON_BACKGROUND_WIDTH, + TITLE_BUTTON_BACKGROUND_HEIGHT + ); + + sprite_init + ( + &_t->infoBackgroundSprite, + _t->g->textures[TEXTURE_GUI_SIGN_BIG], + ORIGIN_MIDDLE, + TITLE_BUTTON_BACKGROUND_WIDTH, + TITLE_BUTTON_BACKGROUND_HEIGHT + ); + + _t->startBackgroundSprite.x = TITLE_START_BUTTON_OFFSET_X; + _t->startBackgroundSprite.y = TITLE_START_BUTTON_OFFSET_Y; + + _t->infoBackgroundSprite.x = TITLE_INFO_BUTTON_OFFSET_X; + _t->infoBackgroundSprite.y = TITLE_INFO_BUTTON_OFFSET_Y; + + s = malloc(TITLE_TEXT_MAX); + + snprintf + ( + s, + TITLE_TEXT_MAX, + "%s", + STRING_TITLE_START + ); + + text_sprite_init + ( + &startButtonText, + _t->g->renderer, + _t->g->fonts[FONT_BIG], + TITLE_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + snprintf + ( + s, + TITLE_TEXT_MAX, + "%s", + STRING_TITLE_INFO + ); + + text_sprite_init + ( + &infoButtonText, + _t->g->renderer, + _t->g->fonts[FONT_MEDIUM], + TITLE_TEXT_COLOR, + ORIGIN_MIDDLE, + s, + 0 + ); + + free(s); + + startButtonText.rotation = TITLE_TEXT_ROTATION; + infoButtonText.rotation = TITLE_TEXT_ROTATION; + + button_init(&_t->startButton, &startButtonText); + button_init(&_t->infoButton, &infoButtonText); + + _t->startButton.sprite.x = _t->startBackgroundSprite.x; + _t->startButton.sprite.y = _t->startBackgroundSprite.y; + + _t->infoButton.sprite.x = _t->infoBackgroundSprite.x; + _t->infoButton.sprite.y = _t->infoBackgroundSprite.y; + + sprite_init + ( + &_t->backgroundSprite, + _t->g->textures[TEXTURE_TITLE_BACKGROUND], + ORIGIN_TOP_LEFT, + TITLE_BACKGROUND_WIDTH, + TITLE_BACKGROUND_HEIGHT + ); + + _t->backgroundSprite.x = TITLE_BACKGROUND_OFFSET_X; + _t->backgroundSprite.y = TITLE_BACKGROUND_OFFSET_Y; + + sprite_init + ( + &_t->logoSprite, + _t->g->textures[TEXTURE_LOGO], + ORIGIN_MIDDLE, + TITLE_LOGO_WIDTH, + TITLE_LOGO_HEIGHT + ); + + _t->logoSprite.x = TITLE_LOGO_OFFSET_X; + _t->logoSprite.y = TITLE_LOGO_OFFSET_Y; + + music_play(_t->g->music[MUSIC_TITLE], TRUE); +} + +/* Frees title. */ +void +title_free(Title* _t) +{ + cursor_free(&_t->cursor); +} + +/* Updates title. */ +void +title_update(Title* _t) +{ + if (_t->isStartPressed) + _t->isStartPressed = FALSE; + + if (_t->isInfoPressed) + _t->isInfoPressed = FALSE; + + button_update(&_t->startButton, &_t->g->mouse); + button_update(&_t->infoButton, &_t->g->mouse); + + if (_t->startButton.state == BUTTON_STATE_PRESS) + { + sound_play(_t->g->sounds[SOUND_MENU], -1); + _t->isStartPressed = TRUE; + } + + if (_t->infoButton.state == BUTTON_STATE_PRESS) + { + sound_play(_t->g->sounds[SOUND_MENU], -1); + _t->isInfoPressed = TRUE; + } + + cursor_update(&_t->cursor, &_t->g->mouse); +} + +/* Draws title. */ +void +title_draw(Title* _t) +{ + sprite_draw(&_t->backgroundSprite, _t->g->renderer); + + sprite_draw(&_t->logoSprite, _t->g->renderer); + + sprite_draw(&_t->startBackgroundSprite, _t->g->renderer); + sprite_draw(&_t->infoBackgroundSprite, _t->g->renderer); + + button_draw(&_t->startButton, _t->g->renderer); + button_draw(&_t->infoButton, _t->g->renderer); + + cursor_draw(&_t->cursor, _t->g->renderer); +} diff --git a/src/game/title/title.h b/src/game/title/title.h new file mode 100755 index 0000000..3998ae0 --- /dev/null +++ b/src/game/title/title.h @@ -0,0 +1,34 @@ +/* + * DESCRIPTION: + * Header file for title.c. +*/ + +#pragma once + +#include "../gui/button.h" +#include "../gui/cursor.h" +#include "../GAME_STRINGS.h" + +#define TITLE_BUTTON_BACKGROUND_WIDTH 192 +#define TITLE_BUTTON_BACKGROUND_HEIGHT 96 +#define TITLE_START_BUTTON_OFFSET_X 600 +#define TITLE_START_BUTTON_OFFSET_Y 400 +#define TITLE_INFO_BUTTON_OFFSET_X 600 +#define TITLE_INFO_BUTTON_OFFSET_Y 500 +#define TITLE_TEXT_MAX 15 +#define TITLE_TEXT_ROTATION -5 +#define TITLE_BACKGROUND_OFFSET_X 0 +#define TITLE_BACKGROUND_OFFSET_Y 0 +#define TITLE_LOGO_WIDTH 250 +#define TITLE_LOGO_HEIGHT 200 +#define TITLE_LOGO_OFFSET_X 600 +#define TITLE_LOGO_OFFSET_Y 150 +#define TITLE_BACKGROUND_WIDTH 800 +#define TITLE_BACKGROUND_HEIGHT 600 + +static const SDL_Color TITLE_TEXT_COLOR = {255, 255, 255, 255}; + +void title_draw(Title* _t); +void title_free(Title* _t); +void title_init(Title* _t, Game* _g); +void title_update(Title* _t); diff --git a/src/main.c b/src/main.c new file mode 100755 index 0000000..dd8a9c5 --- /dev/null +++ b/src/main.c @@ -0,0 +1,56 @@ +#include "main.h" + +static void loop(void); +static void init(void); +static void quit(void); + +static Game g; + +static void +init(void) +{ + sdl_init(); + + random_seed_set(); + + game_init(&g); +} + +static void +quit(void) +{ + /* + if (g.isFullScreen) + { + window_fullscreen_exit(g.window); + g.isFullScreen = FALSE; + } + */ + + game_free(&g); + sdl_quit(); + exit(1); +} + +static void +loop(void) +{ + game_loop(&g); + + if (event_press(&g.event, EVENT_QUIT)) + quit(); +} + +s32 +main(s32 argc, char* argv[]) +{ + init(); + +#ifdef EMSCRIPTEN + emscripten_set_main_loop(loop, FPS, TRUE); +#else + while (TRUE) + loop(); +#endif + return 1; +} diff --git a/src/main.h b/src/main.h new file mode 100755 index 0000000..39cfc04 --- /dev/null +++ b/src/main.h @@ -0,0 +1,9 @@ +#pragma once + +#include "game/game.h" +#include "sdl/sdl.h" +#include "util/random.h" + +#ifdef EMSCRIPTEN +#include +#endif diff --git a/src/sdl/SDL_COMMON.h b/src/sdl/SDL_COMMON.h new file mode 100755 index 0000000..4f45dfd --- /dev/null +++ b/src/sdl/SDL_COMMON.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include +#include + +#include "../COMMON.h" + +typedef enum SDLErrorType +{ + SDL_ERROR_VIDEO, + SDL_ERROR_TIMER, + SDL_ERROR_MIXER, + SDL_ERROR_TTF, + SDL_ERROR_IMAGE +} SDLErrorType; diff --git a/src/sdl/sdl.c b/src/sdl/sdl.c new file mode 100755 index 0000000..7223afe --- /dev/null +++ b/src/sdl/sdl.c @@ -0,0 +1,56 @@ +#include "sdl.h" + +static void sdl_error(enum SDLErrorType _x); + +static void +sdl_error(enum SDLErrorType _x) +{ + switch (_x) + { + /* FALLTHROUGH */ + case SDL_ERROR_VIDEO: + case SDL_ERROR_TIMER: + printf("%s\n", SDL_GetError()); + break; + case SDL_ERROR_MIXER: + printf("%s\n", Mix_GetError()); + break; + case SDL_ERROR_TTF: + printf("%s\n", TTF_GetError()); + break; + case SDL_ERROR_IMAGE: + printf("%s\n", IMG_GetError()); + break; + default: + break; + } + + sdl_quit(); + exit(1); +} + +/* Initializes SDL, SDL_ttf, and SDL_mixer. */ +void +sdl_init(void) +{ + if (SDL_INIT_VIDEO < 0) + sdl_error(SDL_ERROR_VIDEO); + + if (SDL_INIT_TIMER < 0) + sdl_error(SDL_ERROR_TIMER); + + if (TTF_Init() < 0) + sdl_error(SDL_ERROR_TTF); + + if (Mix_OpenAudio(0, MIX_DEFAULT_FORMAT, MIX_CHANNELS, 0) < 0) + sdl_error(SDL_ERROR_MIXER); +} + +/* Quits SDL, SDL_ttf, and SDL_mixer. */ +void +sdl_quit(void) +{ + Mix_CloseAudio(); + TTF_Quit(); + SDL_Quit(); +} diff --git a/src/sdl/sdl.h b/src/sdl/sdl.h new file mode 100755 index 0000000..c8ed313 --- /dev/null +++ b/src/sdl/sdl.h @@ -0,0 +1,6 @@ +#pragma once + +#include "SDL_COMMON.h" + +void sdl_init(void); +void sdl_quit(void); diff --git a/src/util/list.c b/src/util/list.c new file mode 100755 index 0000000..133eef8 --- /dev/null +++ b/src/util/list.c @@ -0,0 +1,191 @@ +#include "list.h" + +/* Initialize a list. */ +void +list_init(List* _l) +{ + memset(_l, '\0', sizeof(List)); + + _l->head = node_get(NULL, 0); + _l->tail = node_get(NULL, 0); + + _l->head->next = _l->tail; + _l->head->prev = NULL; + + _l->tail->next = NULL; + _l->tail->prev = _l->head; + + _l->count = 0; +} + +/* Removes a node from the list. */ +void +list_remove(List* _l, Node* _n) +{ + Node* next; + Node* prev; + + next = _n->next; + prev = _n->prev; + + next->prev = prev; + prev->next = next; + + node_free(_n); + + _l->count--; +} + +/* Removes all the nodes in a given list, but doesn't free the head/tail. */ +void +list_clear(List* _l) +{ + Node* node; + + if (_l->count == 0) + return; + + node = _l->head->next; + + while (node != _l->tail) + { + Node* next; + + next = node->next; + + list_remove(_l, node); + + node = next; + } + + _l->count = 0; +} + +/* Frees a given list. */ +void +list_free(List* _l) +{ + list_clear(_l); + + node_free(_l->head); + node_free(_l->tail); + + _l->count = 0; +} + +/* Adds a node to the left of another in the list. */ +void +list_left_add(List* _l, Node* _a, Node* _b) +{ + _a->next = _b; + _a->prev = _b->prev; + + _b->prev->next = _a; + _b->prev = _a; + + _l->count++; +} + +/* Adds a node to the right of another in the list. */ +void +list_right_add(List* _l, Node* _a, Node* _b) +{ + _a->next = _b->next; + _a->prev = _b; + + _b->next->prev = _a; + _b->next = _a; + + _l->count++; +} + +/* Appends a new node to the head of a list, and returns its data. */ +void* +list_append_new(List* _l, void* _d, size_t _s) +{ + Node* n; + + n = node_get(_d, _s); + + list_right_add(_l, n, _l->head); + + return n->data; +} + +/* Gets a malloc'd array of all the data in a list. */ +void** +list_data_get(List* _l) +{ + void** data; + Node* n; + s32 i; + + if (_l->count == 0) + return NULL; + + i = 0; + data = malloc(sizeof(void*) * _l->count); + + n = _l->head->next; + + while (n != _l->tail) + { + data[i] = (void*)n->data; + n = n->next; + i++; + } + + return data; +} + +/* Returns a node from an index, if it exists. */ +Node* +list_node_from_index_get(List* _l, s32 _i) +{ + Node* n; + s32 i; + + i = 0; + n = _l->head->next; + + while (n != _l->tail) + { + if (i == _i) + return n; + n = n->next; + + i++; + } + + return NULL; +} + +/* Gets a node, given the data and if it exists. */ +Node* +list_node_from_data_get(List* _l, void* _d, size_t _s) +{ + Node* n; + + n = _l->head->next; + + while (n != _l->tail) + { + if (memcmp(n->data, _d, _s) == 0) + return n; + n = n->next; + } + + return NULL; +} + +/* Removes a node from the list, given the data. */ +void +list_node_from_data_remove(List* _l, void* _d, size_t _s) +{ + Node* n; + + n = list_node_from_data_get(_l, _d, _s); + + if (n) + list_remove(_l, n); +} diff --git a/src/util/list.h b/src/util/list.h new file mode 100755 index 0000000..00e4266 --- /dev/null +++ b/src/util/list.h @@ -0,0 +1,22 @@ +#pragma once + +#include "node.h" + +typedef struct List +{ + Node* head; + Node* tail; + s32 count; +} List; + +void list_init(List* _l); +void list_remove(List* _l, Node* _n); +void list_clear(List* _l); +void list_free(List* _l); +void list_left_add(List* _l, Node* _a, Node* _b); +void list_right_add(List* _l, Node* _a, Node* _b); +void* list_append_new(List* _l, void* _data, size_t _size); +void** list_data_get(List* _l); +Node* list_node_from_data_get(List* _l, void* _d, size_t _s); +Node* list_node_from_index_get(List* _l, s32 _i); +void list_node_from_data_remove(List* _l, void* _d, size_t _s); diff --git a/src/util/math.c b/src/util/math.c new file mode 100755 index 0000000..0aedc25 --- /dev/null +++ b/src/util/math.c @@ -0,0 +1,34 @@ +#include "math.h" + +/* Given two points, gets the inverse tangent in radians. */ +f64 +math_atan(f64 _aX, f64 _aY, f64 _bX, f64 _bY) +{ + f64 angle; + f64 x; + f64 y; + + x = _aX - _bX; + y = _aY - _bY; + + angle = atan2(y, x); + + return angle; +} + +/* Distance formula. */ +f64 +math_distance(f64 _aX, f64 _aY, f64 _bX, f64 _bY) +{ + f64 x; + f64 y; + f64 distance; + + x = _aX - _bX; + y = _aY - _bY; + + distance = sqrt(pow(x, 2) + pow(y, 2)); + + return distance; +} + diff --git a/src/util/math.h b/src/util/math.h new file mode 100755 index 0000000..ae6386e --- /dev/null +++ b/src/util/math.h @@ -0,0 +1,13 @@ +/* + * DESCRIPTION: + * Header file for math.c. +*/ + +#pragma once + +#include + +#include "../COMMON.h" + +f64 math_atan(f64 _aX, f64 _aY, f64 _bX, f64 _bY); +f64 math_distance(f64 _aX, f64 _aY, f64 _bX, f64 _bY); diff --git a/src/util/node.c b/src/util/node.c new file mode 100755 index 0000000..16d283b --- /dev/null +++ b/src/util/node.c @@ -0,0 +1,28 @@ +#include "node.h" + +/* Mallocs a node for a linked list, given data and size. */ +Node* +node_get(void* _d, size_t _s) +{ + Node* n; + + n = malloc(sizeof(Node)); + memset(n, '\0', sizeof(Node)); + + if (_d) + { + n->data = malloc(_s); + memcpy(n->data, _d, _s); + } + + return n; +} + +/* Frees a node. */ +void +node_free(Node* _x) +{ + if (_x->data) + free(_x->data); + free(_x); +} diff --git a/src/util/node.h b/src/util/node.h new file mode 100755 index 0000000..43735d2 --- /dev/null +++ b/src/util/node.h @@ -0,0 +1,13 @@ +#pragma once + +#include "../COMMON.h" + +typedef struct Node +{ + struct Node* next; + struct Node* prev; + void* data; +} Node; + +Node* node_get(void* _d, size_t _s); +void node_free(Node* _x); diff --git a/src/util/random.c b/src/util/random.c new file mode 100755 index 0000000..b469aed --- /dev/null +++ b/src/util/random.c @@ -0,0 +1,29 @@ +#include "random.h" + +/* Sets random seed. */ +void +random_seed_set(void) +{ + srand(time(NULL)); +} + +/* Generates a random f64 through [0, 1). */ +f64 +random_f64_get(void) +{ + return ((f64)rand() / (f64)RAND_MAX); +} + +/* Generates a random s32 through [_x, _y]. */ +s32 +random_s32_in_range_get(s32 _x, s32 _y) +{ + return (s32)((random_f64_get() * (_y - _x + 1)) + _x); +} + +/* Generates a random f64 through [_x, _y]. */ +f64 +random_f64_in_range_get(f64 _x, f64 _y) +{ + return (f64)((random_f64_get() * (_y - _x + 1)) + _x); +} diff --git a/src/util/random.h b/src/util/random.h new file mode 100755 index 0000000..adb53bb --- /dev/null +++ b/src/util/random.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +#include "../COMMON.h" + +void random_seed_set(void); +f64 random_f64_get(void); +s32 random_s32_in_range_get(s32 _x, s32 _y); +f64 random_f64_in_range_get(f64 _x, f64 _y);